Skip to content

Commit e270394

Browse files
committed
Refactoring and best practices for UpgradeQuoteCustomerEmailObserver
1 parent b0ba2ac commit e270394

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

app/code/Magento/Customer/Observer/UpgradeQuoteCustomerEmailObserver.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Customer\Observer;
89

@@ -36,25 +37,31 @@ public function __construct(
3637
* @param Observer $observer
3738
* @return void
3839
*/
39-
public function execute(Observer $observer)
40+
public function execute(Observer $observer): void
4041
{
42+
/** @var \Magento\Customer\Model\Data\Customer $customerOrig */
43+
$customerOrig = $observer->getEvent()->getOrigCustomerDataObject();
44+
if (!$customerOrig) {
45+
return;
46+
}
47+
48+
$emailOrig = $customerOrig->getEmail();
49+
4150
/** @var \Magento\Customer\Model\Data\Customer $customer */
4251
$customer = $observer->getEvent()->getCustomerDataObject();
4352
$email = $customer->getEmail();
4453

45-
/** @var \Magento\Customer\Model\Data\Customer $customerOrig */
46-
$customerOrig = $observer->getEvent()->getOrigCustomerDataObject();
47-
if ($customerOrig) {
48-
$emailOrig = $customerOrig->getEmail();
49-
50-
if ($email != $emailOrig) {
51-
try {
52-
$quote = $this->quoteRepository->getForCustomer($customer->getId());
53-
$quote->setCustomerEmail($email);
54-
$this->quoteRepository->save($quote);
55-
} catch (NoSuchEntityException $e) {
56-
}
57-
}
54+
if ($email == $emailOrig) {
55+
return;
5856
}
57+
58+
try {
59+
$quote = $this->quoteRepository->getForCustomer($customer->getId());
60+
$quote->setCustomerEmail($email);
61+
$this->quoteRepository->save($quote);
62+
} catch (NoSuchEntityException $e) {
63+
return;
64+
}
65+
5966
}
6067
}

app/code/Magento/Customer/Test/Unit/Observer/UpgradeQuoteCustomerEmailObserverTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Customer\Test\Unit\Observer;
89

@@ -82,14 +83,14 @@ public function testUpgradeQuoteCustomerEmail()
8283
->method('getOrigCustomerDataObject')
8384
->will($this->returnValue($customerOrig));
8485

85-
$customer->expects($this->any())
86-
->method('getEmail')
87-
->willReturn($this->returnValue($email));
88-
8986
$customerOrig->expects($this->any())
9087
->method('getEmail')
9188
->willReturn($this->returnValue($origEmail));
9289

90+
$customer->expects($this->any())
91+
->method('getEmail')
92+
->willReturn($this->returnValue($email));
93+
9394
$this->quoteRepositoryMock->expects($this->once())
9495
->method('getForCustomer')
9596
->willReturn($quoteMock);

0 commit comments

Comments
 (0)