3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Newsletter \Model \Plugin ;
7
8
8
9
use Magento \Customer \Api \CustomerRepositoryInterface ;
10
+ use Magento \Customer \Api \Data \CustomerExtensionInterface ;
9
11
use Magento \Customer \Api \Data \CustomerInterface ;
10
12
use Magento \Customer \Model \Config \Share ;
11
13
use Magento \Framework \Api \ExtensionAttributesFactory ;
14
+ use Magento \Framework \Api \SearchResults ;
15
+ use Magento \Framework \App \ObjectManager ;
12
16
use Magento \Framework \Exception \NoSuchEntityException ;
13
- use Magento \Newsletter \Model \Subscriber ;
17
+ use Magento \Newsletter \Model \CustomerSubscriberCache ;
14
18
use Magento \Newsletter \Model \ResourceModel \Subscriber \CollectionFactory ;
15
- use Magento \Customer \ Api \ Data \ CustomerExtensionInterface ;
19
+ use Magento \Newsletter \ Model \ Subscriber ;
16
20
use Magento \Newsletter \Model \SubscriberFactory ;
17
21
use Magento \Newsletter \Model \SubscriptionManagerInterface ;
18
22
use Magento \Store \Model \Store ;
19
23
use Magento \Store \Model \StoreManagerInterface ;
20
- use Magento \Framework \Api \SearchResults ;
21
24
use Psr \Log \LoggerInterface ;
22
25
23
26
/**
@@ -52,11 +55,6 @@ class CustomerPlugin
52
55
*/
53
56
private $ storeManager ;
54
57
55
- /**
56
- * @var array
57
- */
58
- private $ customerSubscriber = [];
59
-
60
58
/**
61
59
* @var SubscriberFactory
62
60
*/
@@ -67,6 +65,11 @@ class CustomerPlugin
67
65
*/
68
66
private $ logger ;
69
67
68
+ /**
69
+ * @var CustomerSubscriberCache
70
+ */
71
+ private $ customerSubscriberCache ;
72
+
70
73
/**
71
74
* @param SubscriberFactory $subscriberFactory
72
75
* @param ExtensionAttributesFactory $extensionFactory
@@ -75,6 +78,7 @@ class CustomerPlugin
75
78
* @param Share $shareConfig
76
79
* @param StoreManagerInterface $storeManager
77
80
* @param LoggerInterface $logger
81
+ * @param CustomerSubscriberCache|null $customerSubscriberCache
78
82
*/
79
83
public function __construct (
80
84
SubscriberFactory $ subscriberFactory ,
@@ -83,7 +87,8 @@ public function __construct(
83
87
SubscriptionManagerInterface $ subscriptionManager ,
84
88
Share $ shareConfig ,
85
89
StoreManagerInterface $ storeManager ,
86
- LoggerInterface $ logger
90
+ LoggerInterface $ logger ,
91
+ CustomerSubscriberCache $ customerSubscriberCache = null
87
92
) {
88
93
$ this ->subscriberFactory = $ subscriberFactory ;
89
94
$ this ->extensionFactory = $ extensionFactory ;
@@ -92,6 +97,8 @@ public function __construct(
92
97
$ this ->shareConfig = $ shareConfig ;
93
98
$ this ->storeManager = $ storeManager ;
94
99
$ this ->logger = $ logger ;
100
+ $ this ->customerSubscriberCache = $ customerSubscriberCache
101
+ ?? ObjectManager::getInstance ()->get (CustomerSubscriberCache::class);
95
102
}
96
103
97
104
/**
@@ -129,10 +136,11 @@ public function afterSave(
129
136
}
130
137
if ($ needToUpdate ) {
131
138
$ storeId = $ this ->getCurrentStoreId ($ result );
139
+ $ customerId = (int )$ result ->getId ();
132
140
$ 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) ;
136
144
}
137
145
$ this ->addIsSubscribedExtensionAttribute ($ result , $ subscriber ->isSubscribed ());
138
146
@@ -258,7 +266,7 @@ public function afterGetList(CustomerRepositoryInterface $subject, SearchResults
258
266
$ extensionAttributes = $ customer ->getExtensionAttributes ();
259
267
/** @var Subscriber $subscribe */
260
268
$ 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 ;
262
270
$ extensionAttributes ->setIsSubscribed ($ isSubscribed );
263
271
}
264
272
@@ -315,22 +323,20 @@ private function deleteSubscriptionsAfterCustomerDelete(CustomerInterface $custo
315
323
private function getSubscriber (CustomerInterface $ customer ): Subscriber
316
324
{
317
325
$ 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 );
332
339
}
333
- $ this ->customerSubscriber [$ customerId ] = $ subscriber ;
334
340
335
341
return $ subscriber ;
336
342
}
0 commit comments