CustomerPlugin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model\Plugin;
  7. use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
  8. use Magento\Customer\Api\Data\CustomerInterface;
  9. use Magento\Newsletter\Model\SubscriberFactory;
  10. use Magento\Framework\Api\ExtensionAttributesFactory;
  11. use Magento\Newsletter\Model\ResourceModel\Subscriber;
  12. use Magento\Customer\Api\Data\CustomerExtensionInterface;
  13. /**
  14. * Newsletter Plugin for customer
  15. */
  16. class CustomerPlugin
  17. {
  18. /**
  19. * Factory used for manipulating newsletter subscriptions
  20. *
  21. * @var SubscriberFactory
  22. */
  23. private $subscriberFactory;
  24. /**
  25. * @var ExtensionAttributesFactory
  26. */
  27. private $extensionFactory;
  28. /**
  29. * @var Subscriber
  30. */
  31. private $subscriberResource;
  32. /**
  33. * @var array
  34. */
  35. private $customerSubscriptionStatus = [];
  36. /**
  37. * Initialize dependencies.
  38. *
  39. * @param SubscriberFactory $subscriberFactory
  40. * @param ExtensionAttributesFactory $extensionFactory
  41. * @param Subscriber $subscriberResource
  42. */
  43. public function __construct(
  44. SubscriberFactory $subscriberFactory,
  45. ExtensionAttributesFactory $extensionFactory,
  46. Subscriber $subscriberResource
  47. ) {
  48. $this->subscriberFactory = $subscriberFactory;
  49. $this->extensionFactory = $extensionFactory;
  50. $this->subscriberResource = $subscriberResource;
  51. }
  52. /**
  53. * Plugin after create customer that updates any newsletter subscription that may have existed.
  54. *
  55. * If we have extension attribute (is_subscribed) we need to subscribe that customer
  56. *
  57. * @param CustomerRepository $subject
  58. * @param CustomerInterface $result
  59. * @param CustomerInterface $customer
  60. * @return CustomerInterface
  61. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  62. */
  63. public function afterSave(CustomerRepository $subject, CustomerInterface $result, CustomerInterface $customer)
  64. {
  65. $resultId = $result->getId();
  66. /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
  67. $subscriber = $this->subscriberFactory->create();
  68. $subscriber->updateSubscription($resultId);
  69. // update the result only if the original customer instance had different value.
  70. $initialExtensionAttributes = $result->getExtensionAttributes();
  71. if ($initialExtensionAttributes === null) {
  72. /** @var CustomerExtensionInterface $initialExtensionAttributes */
  73. $initialExtensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
  74. $result->setExtensionAttributes($initialExtensionAttributes);
  75. }
  76. $newExtensionAttributes = $customer->getExtensionAttributes();
  77. if ($newExtensionAttributes
  78. && $initialExtensionAttributes->getIsSubscribed() !== $newExtensionAttributes->getIsSubscribed()
  79. ) {
  80. if ($newExtensionAttributes->getIsSubscribed()) {
  81. $subscriber->subscribeCustomerById($resultId);
  82. } else {
  83. $subscriber->unsubscribeCustomerById($resultId);
  84. }
  85. }
  86. $isSubscribed = $subscriber->isSubscribed();
  87. $this->customerSubscriptionStatus[$resultId] = $isSubscribed;
  88. $initialExtensionAttributes->setIsSubscribed($isSubscribed);
  89. return $result;
  90. }
  91. /**
  92. * Plugin around delete customer that updates any newsletter subscription that may have existed.
  93. *
  94. * @param CustomerRepository $subject
  95. * @param callable $deleteCustomerById Function we are wrapping around
  96. * @param int $customerId Input to the function
  97. * @return bool
  98. */
  99. public function aroundDeleteById(
  100. CustomerRepository $subject,
  101. callable $deleteCustomerById,
  102. $customerId
  103. ) {
  104. $customer = $subject->getById($customerId);
  105. $result = $deleteCustomerById($customerId);
  106. /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
  107. $subscriber = $this->subscriberFactory->create();
  108. $subscriber->loadByEmail($customer->getEmail());
  109. if ($subscriber->getId()) {
  110. $subscriber->delete();
  111. }
  112. return $result;
  113. }
  114. /**
  115. * Plugin after delete customer that updates any newsletter subscription that may have existed.
  116. *
  117. * @param CustomerRepository $subject
  118. * @param bool $result
  119. * @param CustomerInterface $customer
  120. * @return bool
  121. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  122. */
  123. public function afterDelete(CustomerRepository $subject, $result, CustomerInterface $customer)
  124. {
  125. $subscriber = $this->subscriberFactory->create();
  126. $subscriber->loadByEmail($customer->getEmail());
  127. if ($subscriber->getId()) {
  128. $subscriber->delete();
  129. }
  130. return $result;
  131. }
  132. /**
  133. * Plugin after getById customer that obtains newsletter subscription status for given customer.
  134. *
  135. * @param CustomerRepository $subject
  136. * @param CustomerInterface $customer
  137. * @return CustomerInterface
  138. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  139. */
  140. public function afterGetById(CustomerRepository $subject, CustomerInterface $customer)
  141. {
  142. $extensionAttributes = $customer->getExtensionAttributes();
  143. if ($extensionAttributes === null) {
  144. /** @var CustomerExtensionInterface $extensionAttributes */
  145. $extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
  146. $customer->setExtensionAttributes($extensionAttributes);
  147. }
  148. if ($extensionAttributes->getIsSubscribed() === null) {
  149. $isSubscribed = $this->isSubscribed($customer);
  150. $extensionAttributes->setIsSubscribed($isSubscribed);
  151. }
  152. return $customer;
  153. }
  154. /**
  155. * This method returns newsletters subscription status for given customer.
  156. *
  157. * @param CustomerInterface $customer
  158. * @return bool
  159. */
  160. private function isSubscribed(CustomerInterface $customer)
  161. {
  162. $customerId = $customer->getId();
  163. if (!isset($this->customerSubscriptionStatus[$customerId])) {
  164. $subscriber = $this->subscriberResource->loadByCustomerData($customer);
  165. $this->customerSubscriptionStatus[$customerId] = isset($subscriber['subscriber_status'])
  166. && $subscriber['subscriber_status'] == 1;
  167. }
  168. return $this->customerSubscriptionStatus[$customerId];
  169. }
  170. }