Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Storage.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
13 use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
15 
16 class Storage
17 {
30  protected $_customerIds = [];
31 
37  protected $_pageSize;
38 
44  protected $_byPagesIterator;
45 
49  private $customerCollectionFactory;
50 
56  public function __construct(
57  CustomerCollectionFactory $collectionFactory,
58  CollectionByPagesIteratorFactory $colIteratorFactory,
59  array $data = []
60  ) {
61  $this->_customerCollection = isset(
62  $data['customer_collection']
63  ) ? $data['customer_collection'] : $collectionFactory->create();
64  $this->_pageSize = isset($data['page_size']) ? $data['page_size'] : 0;
65  $this->_byPagesIterator = isset(
66  $data['collection_by_pages_iterator']
67  ) ? $data['collection_by_pages_iterator'] : $colIteratorFactory->create();
68  $this->customerCollectionFactory = $collectionFactory;
69  }
70 
78  private function prepareCollection(array $customerIdentifiers): CustomerCollection
79  {
81  $collection = $this->customerCollectionFactory->create();
82  $collection->removeAttributeToSelect();
83  $select = $collection->getSelect();
84  $customerTableId = array_keys($select->getPart(Select::FROM))[0];
85  $select->where(
86  $customerTableId .'.email in (?)',
87  array_map(
88  function (array $customer) {
89  return $customer['email'];
90  },
91  $customerIdentifiers
92  )
93  );
94 
95  return $collection;
96  }
97 
105  private function loadCustomersData(array $customerIdentifiers)
106  {
107  $this->_byPagesIterator->iterate(
108  $this->prepareCollection($customerIdentifiers),
109  $this->_pageSize,
110  [[$this, 'addCustomer']]
111  );
112  }
113 
118  public function addCustomerByArray(array $customer): Storage
119  {
120  $email = strtolower(trim($customer['email']));
121  if (!isset($this->_customerIds[$email])) {
122  $this->_customerIds[$email] = [];
123  }
124  $this->_customerIds[$email][$customer['website_id']] = $customer['entity_id'];
125 
126  return $this;
127  }
128 
137  {
138  $customerData = $customer->toArray();
139  if (!isset($customerData['entity_id']) && isset($customer['id'])) {
140  $customerData['entity_id'] = $customerData['id'];
141  }
143 
144  return $this;
145  }
146 
154  public function getCustomerId(string $email, int $websiteId)
155  {
156  $email = mb_strtolower($email);
157  //Trying to load the customer.
158  if (!array_key_exists($email, $this->_customerIds) || !array_key_exists($websiteId, $this->_customerIds[$email])
159  ) {
160  $this->loadCustomersData([['email' => $email, 'website_id' => $websiteId]]);
161  }
162 
163  if (isset($this->_customerIds[$email][$websiteId])) {
164  return $this->_customerIds[$email][$websiteId];
165  }
166 
167  return false;
168  }
169 
176  public function prepareCustomers(array $customersToFind): void
177  {
178  $identifiers = [];
179  foreach ($customersToFind as $customerToFind) {
180  $email = mb_strtolower($customerToFind['email']);
181  $websiteId = $customerToFind['website_id'];
182  if (!array_key_exists($email, $this->_customerIds)
183  || !array_key_exists($websiteId, $this->_customerIds[$email])
184  ) {
185  //Only looking for customers we don't already have ID for.
186  //We need unique identifiers.
187  $uniqueKey = $email .'_' .$websiteId;
188  $identifiers[$uniqueKey] = [
189  'email' => $email,
190  'website_id' => $websiteId,
191  ];
192  //Recording that we've searched for a customer.
193  if (!array_key_exists($email, $this->_customerIds)) {
194  $this->_customerIds[$email] = [];
195  }
196  $this->_customerIds[$email][$websiteId] = null;
197  }
198  }
199  if (!$identifiers) {
200  return;
201  }
202 
203  //Loading customers data.
204  $this->loadCustomersData($identifiers);
205  }
206 }
$customerData
__construct(CustomerCollectionFactory $collectionFactory, CollectionByPagesIteratorFactory $colIteratorFactory, array $data=[])
Definition: Storage.php:56
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
const FROM
Definition: Select.php:49