Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerRegistry.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Customer\Model;
8 
10 use Magento\Customer\Model\Data\CustomerSecureFactory;
13 
18 {
19  const REGISTRY_SEPARATOR = ':';
20 
24  private $customerFactory;
25 
29  private $customerSecureFactory;
30 
34  private $customerRegistryById = [];
35 
39  private $customerRegistryByEmail = [];
40 
44  private $customerSecureRegistryById = [];
45 
49  private $storeManager;
50 
58  public function __construct(
59  CustomerFactory $customerFactory,
60  CustomerSecureFactory $customerSecureFactory,
61  StoreManagerInterface $storeManager
62  ) {
63  $this->customerFactory = $customerFactory;
64  $this->customerSecureFactory = $customerSecureFactory;
65  $this->storeManager = $storeManager;
66  }
67 
75  public function retrieve($customerId)
76  {
77  if (isset($this->customerRegistryById[$customerId])) {
78  return $this->customerRegistryById[$customerId];
79  }
81  $customer = $this->customerFactory->create()->load($customerId);
82  if (!$customer->getId()) {
83  // customer does not exist
85  } else {
86  $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId());
87  $this->customerRegistryById[$customerId] = $customer;
88  $this->customerRegistryByEmail[$emailKey] = $customer;
89  return $customer;
90  }
91  }
92 
101  public function retrieveByEmail($customerEmail, $websiteId = null)
102  {
103  if ($websiteId === null) {
104  $websiteId = $this->storeManager->getStore()->getWebsiteId();
105  }
106  $emailKey = $this->getEmailKey($customerEmail, $websiteId);
107  if (isset($this->customerRegistryByEmail[$emailKey])) {
108  return $this->customerRegistryByEmail[$emailKey];
109  }
110 
112  $customer = $this->customerFactory->create();
113 
114  if (isset($websiteId)) {
115  $customer->setWebsiteId($websiteId);
116  }
117 
118  $customer->loadByEmail($customerEmail);
119  if (!$customer->getEmail()) {
120  // customer does not exist
121  throw new NoSuchEntityException(
122  __(
123  'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
124  [
125  'fieldName' => 'email',
126  'fieldValue' => $customerEmail,
127  'field2Name' => 'websiteId',
128  'field2Value' => $websiteId
129  ]
130  )
131  );
132  } else {
133  $this->customerRegistryById[$customer->getId()] = $customer;
134  $this->customerRegistryByEmail[$emailKey] = $customer;
135  return $customer;
136  }
137  }
138 
146  public function retrieveSecureData($customerId)
147  {
148  if (isset($this->customerSecureRegistryById[$customerId])) {
149  return $this->customerSecureRegistryById[$customerId];
150  }
152  $customer = $this->retrieve($customerId);
154  $customerSecure = $this->customerSecureFactory->create();
155  $customerSecure->setPasswordHash($customer->getPasswordHash());
156  $customerSecure->setRpToken($customer->getRpToken());
157  $customerSecure->setRpTokenCreatedAt($customer->getRpTokenCreatedAt());
158  $customerSecure->setDeleteable($customer->isDeleteable());
159  $customerSecure->setFailuresNum($customer->getFailuresNum());
160  $customerSecure->setFirstFailure($customer->getFirstFailure());
161  $customerSecure->setLockExpires($customer->getLockExpires());
162  $this->customerSecureRegistryById[$customer->getId()] = $customerSecure;
163 
164  return $customerSecure;
165  }
166 
173  public function remove($customerId)
174  {
175  if (isset($this->customerRegistryById[$customerId])) {
177  $customer = $this->customerRegistryById[$customerId];
178  $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId());
179  unset($this->customerRegistryByEmail[$emailKey]);
180  unset($this->customerRegistryById[$customerId]);
181  unset($this->customerSecureRegistryById[$customerId]);
182  }
183  }
184 
192  public function removeByEmail($customerEmail, $websiteId = null)
193  {
194  if ($websiteId === null) {
195  $websiteId = $this->storeManager->getStore()->getWebsiteId();
196  }
197  $emailKey = $this->getEmailKey($customerEmail, $websiteId);
198  if ($emailKey) {
200  $customer = $this->customerRegistryByEmail[$emailKey];
201  unset($this->customerRegistryByEmail[$emailKey]);
202  unset($this->customerRegistryById[$customer->getId()]);
203  unset($this->customerSecureRegistryById[$customer->getId()]);
204  }
205  }
206 
214  protected function getEmailKey($customerEmail, $websiteId)
215  {
216  return $customerEmail . self::REGISTRY_SEPARATOR . $websiteId;
217  }
218 
225  public function push(Customer $customer)
226  {
227  $this->customerRegistryById[$customer->getId()] = $customer;
228  $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId());
229  $this->customerRegistryByEmail[$emailKey] = $customer;
230  return $this;
231  }
232 }
$customer
Definition: customers.php:11
$storeManager
__()
Definition: __.php:13
__construct(CustomerFactory $customerFactory, CustomerSecureFactory $customerSecureFactory, StoreManagerInterface $storeManager)
getEmailKey($customerEmail, $websiteId)