88use Magento \Customer \Api \CustomerRepositoryInterface as CustomerRepository ;
99use Magento \Customer \Api \Data \CustomerInterface ;
1010use Magento \Newsletter \Model \SubscriberFactory ;
11+ use Magento \Framework \Api \ExtensionAttributesFactory ;
12+ use Magento \Newsletter \Model \ResourceModel \Subscriber ;
13+ use Magento \Customer \Api \Data \CustomerExtensionInterface ;
14+ use Magento \Framework \App \ObjectManager ;
1115
1216class CustomerPlugin
1317{
@@ -18,14 +22,37 @@ class CustomerPlugin
1822 */
1923 private $ subscriberFactory ;
2024
25+ /**
26+ * @var ExtensionAttributesFactory
27+ */
28+ private $ extensionFactory ;
29+
30+ /**
31+ * @var Subscriber
32+ */
33+ private $ subscriberResource ;
34+
35+ /**
36+ * @var array
37+ */
38+ private $ customerSubscriptionStatus = [];
39+
2140 /**
2241 * Initialize dependencies.
2342 *
2443 * @param SubscriberFactory $subscriberFactory
44+ * @param ExtensionAttributesFactory|null $extensionFactory
45+ * @param Subscriber|null $subscriberResource
2546 */
26- public function __construct (SubscriberFactory $ subscriberFactory )
27- {
47+ public function __construct (
48+ SubscriberFactory $ subscriberFactory ,
49+ ExtensionAttributesFactory $ extensionFactory = null ,
50+ Subscriber $ subscriberResource = null
51+ ) {
2852 $ this ->subscriberFactory = $ subscriberFactory ;
53+ $ this ->extensionFactory = $ extensionFactory
54+ ?: ObjectManager::getInstance ()->get (ExtensionAttributesFactory::class);
55+ $ this ->subscriberResource = $ subscriberResource ?: ObjectManager::getInstance ()->get (Subscriber::class);
2956 }
3057
3158 /**
@@ -41,14 +68,30 @@ public function __construct(SubscriberFactory $subscriberFactory)
4168 */
4269 public function afterSave (CustomerRepository $ subject , CustomerInterface $ result , CustomerInterface $ customer )
4370 {
44- $ this ->subscriberFactory ->create ()->updateSubscription ($ result ->getId ());
45- if ($ result ->getId () && $ customer ->getExtensionAttributes ()) {
46- if ($ customer ->getExtensionAttributes ()->getIsSubscribed () === true ) {
47- $ this ->subscriberFactory ->create ()->subscribeCustomerById ($ result ->getId ());
48- } elseif ($ customer ->getExtensionAttributes ()->getIsSubscribed () === false ) {
49- $ this ->subscriberFactory ->create ()->unsubscribeCustomerById ($ result ->getId ());
71+ $ resultId = $ result ->getId ();
72+ /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
73+ $ subscriber = $ this ->subscriberFactory ->create ();
74+ $ subscriber ->updateSubscription ($ resultId );
75+ // update the result only if the original customer instance had different value.
76+ $ initialExtensionAttributes = $ result ->getExtensionAttributes ();
77+ if ($ initialExtensionAttributes === null ) {
78+ /** @var CustomerExtensionInterface $initialExtensionAttributes */
79+ $ initialExtensionAttributes = $ this ->extensionFactory ->create (CustomerInterface::class);
80+ $ result ->setExtensionAttributes ($ initialExtensionAttributes );
81+ }
82+ $ newExtensionAttributes = $ customer ->getExtensionAttributes ();
83+ if ($ newExtensionAttributes
84+ && $ initialExtensionAttributes ->getIsSubscribed () !== $ newExtensionAttributes ->getIsSubscribed ()
85+ ) {
86+ if ($ newExtensionAttributes ->getIsSubscribed () === true ) {
87+ $ subscriber ->subscribeCustomerById ($ resultId );
88+ } elseif ($ newExtensionAttributes ->getIsSubscribed () === false ) {
89+ $ subscriber ->unsubscribeCustomerById ($ resultId );
5090 }
5191 }
92+ $ isSubscribed = $ subscriber ->isSubscribed ();
93+ $ this ->customerSubscriptionStatus [$ resultId ] = $ isSubscribed ;
94+ $ initialExtensionAttributes ->setIsSubscribed ($ isSubscribed );
5295 return $ result ;
5396 }
5497
@@ -94,4 +137,44 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
94137 }
95138 return $ result ;
96139 }
140+
141+ /**
142+ * Plugin after getById customer that obtains newsletter subscription status for given customer.
143+ *
144+ * @param CustomerRepository $subject
145+ * @param CustomerInterface $customer
146+ * @return CustomerInterface
147+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
148+ */
149+ public function afterGetById (CustomerRepository $ subject , CustomerInterface $ customer )
150+ {
151+ $ extensionAttributes = $ customer ->getExtensionAttributes ();
152+ if ($ extensionAttributes === null ) {
153+ /** @var CustomerExtensionInterface $extensionAttributes */
154+ $ extensionAttributes = $ this ->extensionFactory ->create (CustomerInterface::class);
155+ $ customer ->setExtensionAttributes ($ extensionAttributes );
156+ }
157+ if ($ extensionAttributes ->getIsSubscribed () === null ) {
158+ $ isSubscribed = $ this ->isSubscribed ($ customer );
159+ $ extensionAttributes ->setIsSubscribed ($ isSubscribed );
160+ }
161+ return $ customer ;
162+ }
163+
164+ /**
165+ * This method returns newsletters subscription status for given customer.
166+ *
167+ * @param CustomerInterface $customer
168+ * @return mixed
169+ */
170+ private function isSubscribed (CustomerInterface $ customer )
171+ {
172+ $ customerId = $ customer ->getId ();
173+ if (!isset ($ this ->customerSubscriptionStatus [$ customerId ])) {
174+ $ subscriber = $ this ->subscriberResource ->loadByCustomerData ($ customer );
175+ $ this ->customerSubscriptionStatus [$ customerId ] = isset ($ subscriber ['subscriber_status ' ])
176+ && $ subscriber ['subscriber_status ' ] == 1 ;
177+ }
178+ return $ this ->customerSubscriptionStatus [$ customerId ];
179+ }
97180}
0 commit comments