Skip to content

Commit ac5f697

Browse files
committed
Merge remote-tracking branch 'origin/magento2-login-as-customer#150V3' into login-as-customer-150
2 parents abbf014 + 5d79211 commit ac5f697

File tree

5 files changed

+24
-84
lines changed

5 files changed

+24
-84
lines changed

app/code/Magento/LoginAsCustomerAssistance/Api/IsAssistanceEnabledInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
*/
1313
interface IsAssistanceEnabledInterface
1414
{
15+
/**
16+
* Merchant assistance denied by customer status code.
17+
*/
18+
public const DENIED = 1;
19+
20+
/**
21+
* Merchant assistance allowed by customer status code.
22+
*/
23+
public const ALLOWED = 2;
24+
1525
/**
1626
* Get 'assistance_allowed' attribute from Customer by id.
1727
*

app/code/Magento/LoginAsCustomerAssistance/Model/IsAssistanceEnabled.php

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,17 @@ class IsAssistanceEnabled implements IsAssistanceEnabledInterface
2323
*/
2424
private $registry = [];
2525

26-
/**
27-
* Merchant assistance denied by customer status code.
28-
*/
29-
public const DENIED = 1;
30-
31-
/**
32-
* Merchant assistance allowed by customer status code.
33-
*/
34-
public const ALLOWED = 2;
35-
36-
/**
37-
* @var CustomerExtensionFactory
38-
*/
39-
private $customerExtensionFactory;
40-
41-
/**
42-
* @var CustomerRepositoryInterface
43-
*/
44-
private $customerRepository;
45-
4626
/**
4727
* @var GetLoginAsCustomerAssistanceAllowed
4828
*/
4929
private $getLoginAsCustomerAssistanceAllowed;
5030

5131
/**
52-
* @param CustomerExtensionFactory $customerExtensionFactory
53-
* @param CustomerRepositoryInterface $customerRepository
5432
* @param GetLoginAsCustomerAssistanceAllowed $getLoginAsCustomerAssistanceAllowed
5533
*/
5634
public function __construct(
57-
CustomerExtensionFactory $customerExtensionFactory,
58-
CustomerRepositoryInterface $customerRepository,
5935
GetLoginAsCustomerAssistanceAllowed $getLoginAsCustomerAssistanceAllowed
6036
) {
61-
$this->customerExtensionFactory = $customerExtensionFactory;
62-
$this->customerRepository = $customerRepository;
6337
$this->getLoginAsCustomerAssistanceAllowed = $getLoginAsCustomerAssistanceAllowed;
6438
}
6539

@@ -71,54 +45,10 @@ public function __construct(
7145
*/
7246
public function execute(int $customerId): bool
7347
{
74-
try {
75-
$customer = $this->customerRepository->getById($customerId);
76-
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
77-
} catch (NoSuchEntityException $exception) {
78-
// do nothing
79-
}
80-
if (isset($customer)) {
81-
$extensionAttributes = $customer->getExtensionAttributes();
82-
if ($extensionAttributes === null) {
83-
$extensionAttributes = $this->customerExtensionFactory->create();
84-
}
85-
if ($extensionAttributes->getAssistanceAllowed() === null) {
86-
if (isset($this->registry[$customerId])) {
87-
$assistanceAllowed = $this->registry[$customerId];
88-
} else {
89-
$assistanceAllowed = $this->getLoginAsCustomerAssistanceAllowed->execute($customerId);
90-
$this->registry[$customerId] = $assistanceAllowed;
91-
}
92-
$extensionAttributes->setAssistanceAllowed($this->resolveStatus($assistanceAllowed));
93-
$customer->setExtensionAttributes($extensionAttributes);
94-
}
95-
$assistanceAllowed = $this->resolveAllowance($customer->getExtensionAttributes()->getAssistanceAllowed());
96-
} else {
97-
$assistanceAllowed = false;
48+
if (!isset($this->registry[$customerId])) {
49+
$this->registry[$customerId] = $this->getLoginAsCustomerAssistanceAllowed->execute($customerId);
9850
}
9951

100-
return $assistanceAllowed;
101-
}
102-
103-
/**
104-
* Get integer status value from boolean.
105-
*
106-
* @param bool $assistanceAllowed
107-
* @return int
108-
*/
109-
private function resolveStatus(bool $assistanceAllowed): int
110-
{
111-
return $assistanceAllowed ? self::ALLOWED : self::DENIED;
112-
}
113-
114-
/**
115-
* Get boolean status value from integer.
116-
*
117-
* @param int $statusCode
118-
* @return bool
119-
*/
120-
private function resolveAllowance(int $statusCode): bool
121-
{
122-
return $statusCode === self::ALLOWED;
52+
return $this->registry[$customerId];
12353
}
12454
}

app/code/Magento/LoginAsCustomerAssistance/Plugin/CustomerDataValidatePlugin.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use Magento\Framework\App\RequestInterface;
1212
use Magento\Framework\AuthorizationInterface;
1313
use Magento\Framework\Message\MessageInterface;
14+
use Magento\LoginAsCustomerAssistance\Api\IsAssistanceEnabledInterface;
1415
use Magento\LoginAsCustomerAssistance\Model\ResourceModel\GetLoginAsCustomerAssistanceAllowed;
15-
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
1616

1717
/**
1818
* Check if User have permission to change Customers Opt-In preference.
@@ -109,11 +109,11 @@ private function isSetAssistanceAllowedParam(RequestInterface $request): bool
109109
private function isAssistanceAllowedChangeImportant(int $assistanceAllowed, int $assistanceAllowedParam): bool
110110
{
111111
$result = false;
112-
if (($assistanceAllowedParam === IsAssistanceEnabled::DENIED
113-
&& $assistanceAllowed === IsAssistanceEnabled::ALLOWED)
112+
if (($assistanceAllowedParam === IsAssistanceEnabledInterface::DENIED
113+
&& $assistanceAllowed === IsAssistanceEnabledInterface::ALLOWED)
114114
||
115-
($assistanceAllowedParam === IsAssistanceEnabled::ALLOWED
116-
&& $assistanceAllowed !== IsAssistanceEnabled::ALLOWED)) {
115+
($assistanceAllowedParam === IsAssistanceEnabledInterface::ALLOWED
116+
&& $assistanceAllowed !== IsAssistanceEnabledInterface::ALLOWED)) {
117117
$result = true;
118118
}
119119

@@ -128,6 +128,6 @@ private function isAssistanceAllowedChangeImportant(int $assistanceAllowed, int
128128
*/
129129
private function resolveStatus(bool $assistanceAllowed): int
130130
{
131-
return $assistanceAllowed ? IsAssistanceEnabled::ALLOWED : IsAssistanceEnabled::DENIED;
131+
return $assistanceAllowed ? IsAssistanceEnabledInterface::ALLOWED : IsAssistanceEnabledInterface::DENIED;
132132
}
133133
}

app/code/Magento/LoginAsCustomerAssistance/Plugin/DataProviderWithDefaultAddressesPlugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Customer\Model\Customer\DataProviderWithDefaultAddresses;
1111
use Magento\Framework\AuthorizationInterface;
1212
use Magento\LoginAsCustomerApi\Api\ConfigInterface;
13-
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
13+
use Magento\LoginAsCustomerAssistance\Api\IsAssistanceEnabledInterface;
1414
use Magento\LoginAsCustomerAssistance\Model\ResourceModel\GetLoginAsCustomerAssistanceAllowed;
1515

1616
/**
@@ -123,6 +123,6 @@ public function afterGetMeta(
123123
*/
124124
private function resolveStatus(bool $assistanceAllowed): int
125125
{
126-
return $assistanceAllowed ? IsAssistanceEnabled::ALLOWED : IsAssistanceEnabled::DENIED;
126+
return $assistanceAllowed ? IsAssistanceEnabledInterface::ALLOWED : IsAssistanceEnabledInterface::DENIED;
127127
}
128128
}

app/code/Magento/LoginAsCustomerAssistance/view/frontend/templates/shopping-assistance.phtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
use Magento\Framework\Escaper;
8-
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
8+
use Magento\LoginAsCustomerAssistance\Api\IsAssistanceEnabledInterface;
99
use Magento\LoginAsCustomerAssistance\ViewModel\ShoppingAssistanceViewModel;
1010

1111
/** @var Escaper $escaper */
@@ -17,8 +17,8 @@ $viewModel = $block->getViewModel();
1717
{
1818
".form-create-account, .form-edit-account": {
1919
"Magento_LoginAsCustomerAssistance/js/opt-in": {
20-
"allowAccess": "<?= /* @noEscape */ IsAssistanceEnabled::ALLOWED ?>",
21-
"denyAccess": "<?= /* @noEscape */ IsAssistanceEnabled::DENIED ?>"
20+
"allowAccess": "<?= /* @noEscape */ IsAssistanceEnabledInterface::ALLOWED ?>",
21+
"denyAccess": "<?= /* @noEscape */ IsAssistanceEnabledInterface::DENIED ?>"
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)