CustomerRepositoryPlugin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Plugin;
  7. use Magento\Customer\Api\CustomerRepositoryInterface;
  8. use Magento\Customer\Api\Data\CustomerExtensionInterface;
  9. use Magento\Customer\Api\Data\CustomerExtensionInterfaceFactory;
  10. use Magento\Customer\Api\Data\CustomerInterface;
  11. use Magento\Customer\Api\Data\CustomerSearchResultsInterface;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Vertex\Tax\Model\Config;
  14. use Vertex\Tax\Model\Data\CustomerCode;
  15. use Vertex\Tax\Model\Data\CustomerCodeFactory;
  16. use Vertex\Tax\Model\ExceptionLogger;
  17. use Vertex\Tax\Model\Repository\CustomerCodeRepository;
  18. /**
  19. * Adds CustomerCode extension attribute to Customer Repository
  20. *
  21. * @see CustomerRepositoryInterface
  22. */
  23. class CustomerRepositoryPlugin
  24. {
  25. /** @var CustomerCodeFactory */
  26. private $codeFactory;
  27. /** @var Config */
  28. private $config;
  29. /** @var CustomerExtensionInterfaceFactory */
  30. private $extensionFactory;
  31. /** @var CustomerCodeRepository */
  32. private $repository;
  33. /** @var ExceptionLogger */
  34. private $logger;
  35. /** @var bool[] */
  36. private $currentlySaving = [];
  37. /**
  38. * @param CustomerCodeRepository $repository
  39. * @param CustomerExtensionInterfaceFactory $extensionFactory
  40. * @param CustomerCodeFactory $codeFactory
  41. * @param ExceptionLogger $logger
  42. * @param Config $config
  43. */
  44. public function __construct(
  45. CustomerCodeRepository $repository,
  46. CustomerExtensionInterfaceFactory $extensionFactory,
  47. CustomerCodeFactory $codeFactory,
  48. ExceptionLogger $logger,
  49. Config $config
  50. ) {
  51. $this->repository = $repository;
  52. $this->extensionFactory = $extensionFactory;
  53. $this->codeFactory = $codeFactory;
  54. $this->logger = $logger;
  55. $this->config = $config;
  56. }
  57. /**
  58. * Add the Vertex Customer Code to the Customer extension attribute when customers are retrieved from the repository
  59. *
  60. * @param CustomerRepositoryInterface $subject
  61. * @param CustomerSearchResultsInterface $results
  62. * @see CustomerRepositoryInterface::getList()
  63. * @return CustomerSearchResultsInterface
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. */
  66. public function afterGetList(CustomerRepositoryInterface $subject, $results)
  67. {
  68. if (!$this->config->isVertexActive() || $results->getTotalCount() <= 0) {
  69. return $results;
  70. }
  71. $customerIds = array_map(
  72. function (CustomerInterface $customer) {
  73. return $customer->getId();
  74. },
  75. $results->getItems()
  76. );
  77. $customerCodes = $this->repository->getListByCustomerIds($customerIds);
  78. foreach ($results->getItems() as $customer) {
  79. if (!isset($customerCodes[$customer->getId()])) {
  80. continue;
  81. }
  82. $extensionAttributes = $this->getExtensionAttributes($customer);
  83. $extensionAttributes->setVertexCustomerCode($customerCodes[$customer->getId()]->getCustomerCode());
  84. }
  85. return $results;
  86. }
  87. /**
  88. * Add the Vertex Customer Code to the Customer extension attribute when a customer is retrieved from the repository
  89. *
  90. * @see CustomerRepositoryInterface::getById()
  91. *
  92. * @param CustomerRepositoryInterface $subject
  93. * @param CustomerInterface $result
  94. * @return CustomerInterface
  95. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  96. */
  97. public function afterGetById(CustomerRepositoryInterface $subject, CustomerInterface $result)
  98. {
  99. if (!$this->config->isVertexActive($result->getStoreId()) || $this->isCurrentlySaving($result)) {
  100. return $result;
  101. }
  102. $extensionAttributes = $this->getExtensionAttributes($result);
  103. try {
  104. $customerCode = $this->repository->getByCustomerId($result->getId());
  105. $extensionAttributes->setVertexCustomerCode($customerCode->getCustomerCode());
  106. } catch (NoSuchEntityException $exception) {
  107. $extensionAttributes->setVertexCustomerCode(null);
  108. } catch (\Exception $exception) {
  109. $this->logger->critical($exception);
  110. }
  111. return $result;
  112. }
  113. /**
  114. * Add the Vertex Customer Code to the Customer extension attribute when a customer is retrieved from the repository
  115. *
  116. * @see CustomerRepositoryInterface::get()
  117. *
  118. * @param CustomerRepositoryInterface $subject
  119. * @param CustomerInterface $result
  120. * @return CustomerInterface
  121. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  122. */
  123. public function afterGet(CustomerRepositoryInterface $subject, CustomerInterface $result)
  124. {
  125. return $this->afterGetById($subject, $result);
  126. }
  127. /**
  128. * Save the Vertex Customer Code when the Customer is saved
  129. *
  130. * @see CustomerRepositoryInterface::save()
  131. * @todo Convert to afterSave once we only support Magento 2.2+
  132. *
  133. * @param CustomerRepositoryInterface $subject
  134. * @param callable $proceed {@see CustomerRepositoryInterface::save()}
  135. * @param CustomerInterface $customer
  136. * @param string|null $passwordHash
  137. * @return CustomerInterface
  138. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  139. */
  140. public function aroundSave(
  141. CustomerRepositoryInterface $subject,
  142. callable $proceed,
  143. CustomerInterface $customer,
  144. $passwordHash = null
  145. ) {
  146. if (!$this->config->isVertexActive($customer->getStoreId())) {
  147. return $proceed($customer, $passwordHash);
  148. }
  149. $this->setCurrentlySaving($customer);
  150. if ($customer->getExtensionAttributes()) {
  151. $customerCode = $customer->getExtensionAttributes()->getVertexCustomerCode();
  152. /** @var CustomerInterface $result */
  153. $result = $proceed($customer, $passwordHash);
  154. if ($customerCode) {
  155. $codeModel = $this->getCodeModel($result->getId());
  156. $codeModel->setCustomerCode($customerCode);
  157. try {
  158. $this->repository->save($codeModel);
  159. } catch (\Exception $e) {
  160. $this->logger->critical($e);
  161. }
  162. } else {
  163. $this->deleteByCustomerId($result->getId());
  164. }
  165. $extensionAttributes = $this->getExtensionAttributes($result);
  166. $extensionAttributes->setVertexCustomerCode($customerCode);
  167. } else {
  168. $result = $proceed($customer, $passwordHash);
  169. }
  170. $this->unsetCurrentlySaving($result);
  171. return $result;
  172. }
  173. /**
  174. * Delete the Vertex Customer Code when the customer is deleted
  175. *
  176. * @see CustomerRepositoryInterface::delete()
  177. * @todo Convert to afterDelete once we only support Magento 2.2+
  178. *
  179. * @param CustomerRepositoryInterface $subject
  180. * @param callable $proceed {@see CustomerRepositoryInterface::delete()}
  181. * @param CustomerInterface $customer
  182. * @return bool
  183. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  184. */
  185. public function aroundDelete(CustomerRepositoryInterface $subject, callable $proceed, CustomerInterface $customer)
  186. {
  187. $customerId = $customer->getId();
  188. $result = $proceed($customer);
  189. if (!$this->config->isVertexActive() && $result) {
  190. $this->deleteByCustomerId($customerId);
  191. }
  192. return $result;
  193. }
  194. /**
  195. * Delete the Vertex Customer code when the customer is deleted
  196. *
  197. * @see CustomerRepositoryInterface::deleteById()
  198. * @todo Convert to afterDeleteById once we only support Magento 2.2+
  199. *
  200. * @param CustomerRepositoryInterface $subject
  201. * @param callable $proceed {@see CustomerRepositoryInterface::deleteById()}
  202. * @param int $customerId
  203. * @return bool
  204. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  205. */
  206. public function aroundDeleteById(CustomerRepositoryInterface $subject, callable $proceed, $customerId)
  207. {
  208. $result = $proceed($customerId);
  209. if (!$this->config->isVertexActive() && $result) {
  210. $this->deleteByCustomerId($customerId);
  211. }
  212. return $result;
  213. }
  214. /**
  215. * Retrieve the Customer Code by Customer ID
  216. *
  217. * @param int $customerId
  218. * @return \Vertex\Tax\Model\Data\CustomerCode
  219. */
  220. private function getCodeModel($customerId)
  221. {
  222. try {
  223. $customerCode = $this->repository->getByCustomerId($customerId);
  224. } catch (NoSuchEntityException $e) {
  225. /** @var CustomerCode $customerCode */
  226. $customerCode = $this->codeFactory->create();
  227. $customerCode->setCustomerId($customerId);
  228. }
  229. return $customerCode;
  230. }
  231. /**
  232. * Delete a Customer Code given a Customer ID
  233. *
  234. * @param int $customerId
  235. * @return void
  236. */
  237. private function deleteByCustomerId($customerId)
  238. {
  239. try {
  240. $this->repository->deleteByCustomerId($customerId);
  241. } catch (\Exception $exception) {
  242. $this->logger->critical($exception);
  243. }
  244. }
  245. /**
  246. * Get a CustomerExtensionInterface object, creating it if it is not yet created
  247. *
  248. * @param CustomerInterface $customer
  249. * @return CustomerExtensionInterface
  250. */
  251. private function getExtensionAttributes(CustomerInterface $customer)
  252. {
  253. $extensionAttributes = $customer->getExtensionAttributes();
  254. if (!$extensionAttributes) {
  255. $extensionAttributes = $this->extensionFactory->create();
  256. $customer->setExtensionAttributes($extensionAttributes);
  257. }
  258. return $extensionAttributes;
  259. }
  260. /**
  261. * Set whether or not we are currently saving a specific customer
  262. *
  263. * This is used to prevent loading the attribute during a save procedure
  264. *
  265. * @param CustomerInterface $customer
  266. * @return void
  267. */
  268. private function setCurrentlySaving(CustomerInterface $customer)
  269. {
  270. if ($customer->getId()) {
  271. $this->currentlySaving[$customer->getId()] = true;
  272. }
  273. }
  274. /**
  275. * Determine whether or not we are currently saving a specific customer
  276. *
  277. * This is used to prevent loading the attribute during a save procedure
  278. *
  279. * @param CustomerInterface $customer
  280. * @return bool
  281. */
  282. private function isCurrentlySaving(CustomerInterface $customer)
  283. {
  284. return isset($this->currentlySaving[$customer->getId()]);
  285. }
  286. /**
  287. * Declare that we are no longer currently saving a specific customer
  288. *
  289. * @param CustomerInterface $customer
  290. * @return void
  291. */
  292. private function unsetCurrentlySaving(CustomerInterface $customer)
  293. {
  294. unset($this->currentlySaving[$customer->getId()]);
  295. }
  296. }