33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Newsletter \Model \Plugin ;
78
89use Magento \Customer \Api \CustomerRepositoryInterface ;
10+ use Magento \Customer \Api \Data \CustomerExtensionInterface ;
911use Magento \Customer \Api \Data \CustomerInterface ;
1012use Magento \Customer \Model \Config \Share ;
1113use Magento \Framework \Api \ExtensionAttributesFactory ;
14+ use Magento \Framework \Api \SearchResults ;
15+ use Magento \Framework \App \ObjectManager ;
1216use Magento \Framework \Exception \NoSuchEntityException ;
13- use Magento \Newsletter \Model \Subscriber ;
17+ use Magento \Newsletter \Model \CustomerSubscriberCache ;
1418use Magento \Newsletter \Model \ResourceModel \Subscriber \CollectionFactory ;
15- use Magento \Customer \ Api \ Data \ CustomerExtensionInterface ;
19+ use Magento \Newsletter \ Model \ Subscriber ;
1620use Magento \Newsletter \Model \SubscriberFactory ;
1721use Magento \Newsletter \Model \SubscriptionManagerInterface ;
1822use Magento \Store \Model \Store ;
1923use Magento \Store \Model \StoreManagerInterface ;
20- use Magento \Framework \Api \SearchResults ;
2124use Psr \Log \LoggerInterface ;
2225
2326/**
@@ -52,11 +55,6 @@ class CustomerPlugin
5255 */
5356 private $ storeManager ;
5457
55- /**
56- * @var array
57- */
58- private $ customerSubscriber = [];
59-
6058 /**
6159 * @var SubscriberFactory
6260 */
@@ -67,6 +65,11 @@ class CustomerPlugin
6765 */
6866 private $ logger ;
6967
68+ /**
69+ * @var CustomerSubscriberCache
70+ */
71+ private $ customerSubscriberCache ;
72+
7073 /**
7174 * @param SubscriberFactory $subscriberFactory
7275 * @param ExtensionAttributesFactory $extensionFactory
@@ -75,6 +78,7 @@ class CustomerPlugin
7578 * @param Share $shareConfig
7679 * @param StoreManagerInterface $storeManager
7780 * @param LoggerInterface $logger
81+ * @param CustomerSubscriberCache|null $customerSubscriberCache
7882 */
7983 public function __construct (
8084 SubscriberFactory $ subscriberFactory ,
@@ -83,7 +87,8 @@ public function __construct(
8387 SubscriptionManagerInterface $ subscriptionManager ,
8488 Share $ shareConfig ,
8589 StoreManagerInterface $ storeManager ,
86- LoggerInterface $ logger
90+ LoggerInterface $ logger ,
91+ CustomerSubscriberCache $ customerSubscriberCache = null
8792 ) {
8893 $ this ->subscriberFactory = $ subscriberFactory ;
8994 $ this ->extensionFactory = $ extensionFactory ;
@@ -92,6 +97,8 @@ public function __construct(
9297 $ this ->shareConfig = $ shareConfig ;
9398 $ this ->storeManager = $ storeManager ;
9499 $ this ->logger = $ logger ;
100+ $ this ->customerSubscriberCache = $ customerSubscriberCache
101+ ?? ObjectManager::getInstance ()->get (CustomerSubscriberCache::class);
95102 }
96103
97104 /**
@@ -129,10 +136,11 @@ public function afterSave(
129136 }
130137 if ($ needToUpdate ) {
131138 $ storeId = $ this ->getCurrentStoreId ($ result );
139+ $ customerId = (int )$ result ->getId ();
132140 $ subscriber = $ subscribeStatus
133- ? $ this ->subscriptionManager ->subscribeCustomer (( int ) $ result -> getId () , $ storeId )
134- : $ this ->subscriptionManager ->unsubscribeCustomer (( int ) $ result -> getId () , $ storeId );
135- $ this ->customerSubscriber [( int ) $ result -> getId ()] = $ subscriber ;
141+ ? $ this ->subscriptionManager ->subscribeCustomer ($ customerId , $ storeId )
142+ : $ this ->subscriptionManager ->unsubscribeCustomer ($ customerId , $ storeId );
143+ $ this ->customerSubscriberCache -> setCustomerSubscriber ( $ customerId , $ subscriber) ;
136144 }
137145 $ this ->addIsSubscribedExtensionAttribute ($ result , $ subscriber ->isSubscribed ());
138146
@@ -258,7 +266,7 @@ public function afterGetList(CustomerRepositoryInterface $subject, SearchResults
258266 $ extensionAttributes = $ customer ->getExtensionAttributes ();
259267 /** @var Subscriber $subscribe */
260268 $ subscribe = $ collection ->getItemByColumnValue ('subscriber_email ' , $ customer ->getEmail ());
261- $ isSubscribed = $ subscribe && (int ) $ subscribe ->getStatus () === Subscriber::STATUS_SUBSCRIBED ;
269+ $ isSubscribed = $ subscribe && (int )$ subscribe ->getStatus () === Subscriber::STATUS_SUBSCRIBED ;
262270 $ extensionAttributes ->setIsSubscribed ($ isSubscribed );
263271 }
264272
@@ -315,22 +323,20 @@ private function deleteSubscriptionsAfterCustomerDelete(CustomerInterface $custo
315323 private function getSubscriber (CustomerInterface $ customer ): Subscriber
316324 {
317325 $ customerId = (int )$ customer ->getId ();
318- if (isset ($ this ->customerSubscriber [$ customerId ])) {
319- return $ this ->customerSubscriber [$ customerId ];
320- }
321-
322- /** @var Subscriber $subscriber */
323- $ subscriber = $ this ->subscriberFactory ->create ();
324- $ websiteId = $ this ->getCurrentWebsiteId ($ customer );
325- $ subscriber ->loadByCustomer ((int )$ customer ->getId (), $ websiteId );
326- /**
327- * If subscriber was't found by customer id then try to find subscriber by customer email.
328- * It need when the customer is creating and he has already subscribed as guest by same email.
329- */
330- if (!$ subscriber ->getId ()) {
331- $ subscriber ->loadBySubscriberEmail ((string )$ customer ->getEmail (), $ websiteId );
326+ $ subscriber = $ this ->customerSubscriberCache ->getCustomerSubscriber ($ customerId );
327+ if ($ subscriber === null ) {
328+ $ subscriber = $ this ->subscriberFactory ->create ();
329+ $ websiteId = $ this ->getCurrentWebsiteId ($ customer );
330+ $ subscriber ->loadByCustomer ((int )$ customer ->getId (), $ websiteId );
331+ /**
332+ * If subscriber wasn't found by customer id then try to find subscriber by customer email.
333+ * It need when the customer is creating and he has already subscribed as guest by same email.
334+ */
335+ if (!$ subscriber ->getId ()) {
336+ $ subscriber ->loadBySubscriberEmail ((string )$ customer ->getEmail (), $ websiteId );
337+ }
338+ $ this ->customerSubscriberCache ->setCustomerSubscriber ($ customerId , $ subscriber );
332339 }
333- $ this ->customerSubscriber [$ customerId ] = $ subscriber ;
334340
335341 return $ subscriber ;
336342 }
0 commit comments