Skip to content

Commit f49dbc5

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-72775' into 2.3-develop-pr13
2 parents 231bfa2 + a74dc67 commit f49dbc5

File tree

3 files changed

+269
-66
lines changed

3 files changed

+269
-66
lines changed

app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
99
use Magento\Customer\Api\Data\CustomerInterface;
1010
use Magento\Newsletter\Model\SubscriberFactory;
11+
use Magento\Framework\Api\ExtensionAttributesFactory;
12+
use Magento\Newsletter\Model\ResourceModel\Subscriber;
13+
use Magento\Customer\Api\Data\CustomerExtensionInterface;
1114

1215
class CustomerPlugin
1316
{
@@ -18,14 +21,36 @@ class CustomerPlugin
1821
*/
1922
private $subscriberFactory;
2023

24+
/**
25+
* @var ExtensionAttributesFactory
26+
*/
27+
private $extensionFactory;
28+
29+
/**
30+
* @var Subscriber
31+
*/
32+
private $subscriberResource;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $customerSubscriptionStatus = [];
38+
2139
/**
2240
* Initialize dependencies.
2341
*
2442
* @param SubscriberFactory $subscriberFactory
43+
* @param ExtensionAttributesFactory $extensionFactory
44+
* @param Subscriber $subscriberResource
2545
*/
26-
public function __construct(SubscriberFactory $subscriberFactory)
27-
{
46+
public function __construct(
47+
SubscriberFactory $subscriberFactory,
48+
ExtensionAttributesFactory $extensionFactory,
49+
Subscriber $subscriberResource
50+
) {
2851
$this->subscriberFactory = $subscriberFactory;
52+
$this->extensionFactory = $extensionFactory;
53+
$this->subscriberResource = $subscriberResource;
2954
}
3055

3156
/**
@@ -41,14 +66,34 @@ public function __construct(SubscriberFactory $subscriberFactory)
4166
*/
4267
public function afterSave(CustomerRepository $subject, CustomerInterface $result, CustomerInterface $customer)
4368
{
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());
69+
$resultId = $result->getId();
70+
/** @var \Magento\Newsletter\Model\Subscriber $subscriber */
71+
$subscriber = $this->subscriberFactory->create();
72+
73+
$subscriber->updateSubscription($resultId);
74+
// update the result only if the original customer instance had different value.
75+
$initialExtensionAttributes = $result->getExtensionAttributes();
76+
if ($initialExtensionAttributes === null) {
77+
/** @var CustomerExtensionInterface $initialExtensionAttributes */
78+
$initialExtensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
79+
$result->setExtensionAttributes($initialExtensionAttributes);
80+
}
81+
82+
$newExtensionAttributes = $customer->getExtensionAttributes();
83+
if ($newExtensionAttributes
84+
&& $initialExtensionAttributes->getIsSubscribed() !== $newExtensionAttributes->getIsSubscribed()
85+
) {
86+
if ($newExtensionAttributes->getIsSubscribed()) {
87+
$subscriber->subscribeCustomerById($resultId);
88+
} else {
89+
$subscriber->unsubscribeCustomerById($resultId);
5090
}
5191
}
92+
93+
$isSubscribed = $subscriber->isSubscribed();
94+
$this->customerSubscriptionStatus[$resultId] = $isSubscribed;
95+
$initialExtensionAttributes->setIsSubscribed($isSubscribed);
96+
5297
return $result;
5398
}
5499

@@ -94,4 +139,47 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
94139
}
95140
return $result;
96141
}
142+
143+
/**
144+
* Plugin after getById customer that obtains newsletter subscription status for given customer.
145+
*
146+
* @param CustomerRepository $subject
147+
* @param CustomerInterface $customer
148+
* @return CustomerInterface
149+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
150+
*/
151+
public function afterGetById(CustomerRepository $subject, CustomerInterface $customer)
152+
{
153+
$extensionAttributes = $customer->getExtensionAttributes();
154+
155+
if ($extensionAttributes === null) {
156+
/** @var CustomerExtensionInterface $extensionAttributes */
157+
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);
158+
$customer->setExtensionAttributes($extensionAttributes);
159+
}
160+
if ($extensionAttributes->getIsSubscribed() === null) {
161+
$isSubscribed = $this->isSubscribed($customer);
162+
$extensionAttributes->setIsSubscribed($isSubscribed);
163+
}
164+
165+
return $customer;
166+
}
167+
168+
/**
169+
* This method returns newsletters subscription status for given customer.
170+
*
171+
* @param CustomerInterface $customer
172+
* @return bool
173+
*/
174+
private function isSubscribed(CustomerInterface $customer)
175+
{
176+
$customerId = $customer->getId();
177+
if (!isset($this->customerSubscriptionStatus[$customerId])) {
178+
$subscriber = $this->subscriberResource->loadByCustomerData($customer);
179+
$this->customerSubscriptionStatus[$customerId] = isset($subscriber['subscriber_status'])
180+
&& $subscriber['subscriber_status'] == 1;
181+
}
182+
183+
return $this->customerSubscriptionStatus[$customerId];
184+
}
97185
}

0 commit comments

Comments
 (0)