CustomerRegistry.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Customer\Model\Data\CustomerSecure;
  8. use Magento\Customer\Model\Data\CustomerSecureFactory;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. /**
  12. * Registry for \Magento\Customer\Model\Customer
  13. */
  14. class CustomerRegistry
  15. {
  16. const REGISTRY_SEPARATOR = ':';
  17. /**
  18. * @var CustomerFactory
  19. */
  20. private $customerFactory;
  21. /**
  22. * @var CustomerSecureFactory
  23. */
  24. private $customerSecureFactory;
  25. /**
  26. * @var array
  27. */
  28. private $customerRegistryById = [];
  29. /**
  30. * @var array
  31. */
  32. private $customerRegistryByEmail = [];
  33. /**
  34. * @var array
  35. */
  36. private $customerSecureRegistryById = [];
  37. /**
  38. * @var \Magento\Store\Model\StoreManagerInterface
  39. */
  40. private $storeManager;
  41. /**
  42. * Constructor
  43. *
  44. * @param CustomerFactory $customerFactory
  45. * @param CustomerSecureFactory $customerSecureFactory
  46. * @param StoreManagerInterface $storeManager
  47. */
  48. public function __construct(
  49. CustomerFactory $customerFactory,
  50. CustomerSecureFactory $customerSecureFactory,
  51. StoreManagerInterface $storeManager
  52. ) {
  53. $this->customerFactory = $customerFactory;
  54. $this->customerSecureFactory = $customerSecureFactory;
  55. $this->storeManager = $storeManager;
  56. }
  57. /**
  58. * Retrieve Customer Model from registry given an id
  59. *
  60. * @param string $customerId
  61. * @return Customer
  62. * @throws NoSuchEntityException
  63. */
  64. public function retrieve($customerId)
  65. {
  66. if (isset($this->customerRegistryById[$customerId])) {
  67. return $this->customerRegistryById[$customerId];
  68. }
  69. /** @var Customer $customer */
  70. $customer = $this->customerFactory->create()->load($customerId);
  71. if (!$customer->getId()) {
  72. // customer does not exist
  73. throw NoSuchEntityException::singleField('customerId', $customerId);
  74. } else {
  75. $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId());
  76. $this->customerRegistryById[$customerId] = $customer;
  77. $this->customerRegistryByEmail[$emailKey] = $customer;
  78. return $customer;
  79. }
  80. }
  81. /**
  82. * Retrieve Customer Model from registry given an email
  83. *
  84. * @param string $customerEmail Customers email address
  85. * @param string|null $websiteId Optional website ID, if not set, will use the current websiteId
  86. * @return Customer
  87. * @throws NoSuchEntityException
  88. */
  89. public function retrieveByEmail($customerEmail, $websiteId = null)
  90. {
  91. if ($websiteId === null) {
  92. $websiteId = $this->storeManager->getStore()->getWebsiteId();
  93. }
  94. $emailKey = $this->getEmailKey($customerEmail, $websiteId);
  95. if (isset($this->customerRegistryByEmail[$emailKey])) {
  96. return $this->customerRegistryByEmail[$emailKey];
  97. }
  98. /** @var Customer $customer */
  99. $customer = $this->customerFactory->create();
  100. if (isset($websiteId)) {
  101. $customer->setWebsiteId($websiteId);
  102. }
  103. $customer->loadByEmail($customerEmail);
  104. if (!$customer->getEmail()) {
  105. // customer does not exist
  106. throw new NoSuchEntityException(
  107. __(
  108. 'No such entity with %fieldName = %fieldValue, %field2Name = %field2Value',
  109. [
  110. 'fieldName' => 'email',
  111. 'fieldValue' => $customerEmail,
  112. 'field2Name' => 'websiteId',
  113. 'field2Value' => $websiteId
  114. ]
  115. )
  116. );
  117. } else {
  118. $this->customerRegistryById[$customer->getId()] = $customer;
  119. $this->customerRegistryByEmail[$emailKey] = $customer;
  120. return $customer;
  121. }
  122. }
  123. /**
  124. * Retrieve CustomerSecure Model from registry given an id
  125. *
  126. * @param int $customerId
  127. * @return CustomerSecure
  128. * @throws NoSuchEntityException
  129. */
  130. public function retrieveSecureData($customerId)
  131. {
  132. if (isset($this->customerSecureRegistryById[$customerId])) {
  133. return $this->customerSecureRegistryById[$customerId];
  134. }
  135. /** @var Customer $customer */
  136. $customer = $this->retrieve($customerId);
  137. /** @var $customerSecure CustomerSecure*/
  138. $customerSecure = $this->customerSecureFactory->create();
  139. $customerSecure->setPasswordHash($customer->getPasswordHash());
  140. $customerSecure->setRpToken($customer->getRpToken());
  141. $customerSecure->setRpTokenCreatedAt($customer->getRpTokenCreatedAt());
  142. $customerSecure->setDeleteable($customer->isDeleteable());
  143. $customerSecure->setFailuresNum($customer->getFailuresNum());
  144. $customerSecure->setFirstFailure($customer->getFirstFailure());
  145. $customerSecure->setLockExpires($customer->getLockExpires());
  146. $this->customerSecureRegistryById[$customer->getId()] = $customerSecure;
  147. return $customerSecure;
  148. }
  149. /**
  150. * Remove instance of the Customer Model from registry given an id
  151. *
  152. * @param int $customerId
  153. * @return void
  154. */
  155. public function remove($customerId)
  156. {
  157. if (isset($this->customerRegistryById[$customerId])) {
  158. /** @var Customer $customer */
  159. $customer = $this->customerRegistryById[$customerId];
  160. $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId());
  161. unset($this->customerRegistryByEmail[$emailKey]);
  162. unset($this->customerRegistryById[$customerId]);
  163. unset($this->customerSecureRegistryById[$customerId]);
  164. }
  165. }
  166. /**
  167. * Remove instance of the Customer Model from registry given an email
  168. *
  169. * @param string $customerEmail Customers email address
  170. * @param string|null $websiteId Optional website ID, if not set, will use the current websiteId
  171. * @return void
  172. */
  173. public function removeByEmail($customerEmail, $websiteId = null)
  174. {
  175. if ($websiteId === null) {
  176. $websiteId = $this->storeManager->getStore()->getWebsiteId();
  177. }
  178. $emailKey = $this->getEmailKey($customerEmail, $websiteId);
  179. if ($emailKey) {
  180. /** @var Customer $customer */
  181. $customer = $this->customerRegistryByEmail[$emailKey];
  182. unset($this->customerRegistryByEmail[$emailKey]);
  183. unset($this->customerRegistryById[$customer->getId()]);
  184. unset($this->customerSecureRegistryById[$customer->getId()]);
  185. }
  186. }
  187. /**
  188. * Create registry key
  189. *
  190. * @param string $customerEmail
  191. * @param string $websiteId
  192. * @return string
  193. */
  194. protected function getEmailKey($customerEmail, $websiteId)
  195. {
  196. return $customerEmail . self::REGISTRY_SEPARATOR . $websiteId;
  197. }
  198. /**
  199. * Replace existing customer model with a new one.
  200. *
  201. * @param Customer $customer
  202. * @return $this
  203. */
  204. public function push(Customer $customer)
  205. {
  206. $this->customerRegistryById[$customer->getId()] = $customer;
  207. $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId());
  208. $this->customerRegistryByEmail[$emailKey] = $customer;
  209. return $this;
  210. }
  211. }