Skip to content

Commit daed603

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface refactoring
1 parent b994aa2 commit daed603

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

app/code/Magento/LoginAsCustomer/Api/AuthenticateCustomerInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77

88
namespace Magento\LoginAsCustomer\Api;
99

10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
12+
1013
/**
14+
* Authenticate a customer
15+
*
1116
* @api
1217
*/
1318
interface AuthenticateCustomerInterface
1419
{
1520
/**
16-
* Authenticate a customer by customer ID
21+
* Authenticate a customer
1722
*
18-
* @return bool
19-
* @param int $customerId
20-
* @param int $adminId
23+
* @param AuthenticationDataInterface $authenticationData
24+
* @return void
25+
* @throws LocalizedException
2126
*/
22-
public function execute(int $customerId, int $adminId):bool;
27+
public function execute(AuthenticationDataInterface $authenticationData): void;
2328
}

app/code/Magento/LoginAsCustomer/Model/AuthenticateCustomer.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
namespace Magento\LoginAsCustomer\Model;
99

1010
use Magento\Customer\Model\Session;
11+
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;
13+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
1214

1315
/**
14-
* @api
16+
* @inheritdoc
1517
*/
1618
class AuthenticateCustomer implements AuthenticateCustomerInterface
1719
{
@@ -21,7 +23,6 @@ class AuthenticateCustomer implements AuthenticateCustomerInterface
2123
private $customerSession;
2224

2325
/**
24-
* AuthenticateCustomer constructor.
2526
* @param Session $customerSession
2627
*/
2728
public function __construct(
@@ -31,25 +32,20 @@ public function __construct(
3132
}
3233

3334
/**
34-
* Authenticate a customer by customer ID
35-
*
36-
* @return bool
37-
* @param int $customerId
38-
* @param int $adminId
35+
* @inheritdoc
3936
*/
40-
public function execute(int $customerId, int $adminId):bool
37+
public function execute(AuthenticationDataInterface $authenticationData): void
4138
{
4239
if ($this->customerSession->getId()) {
43-
/* Logout if logged in */
4440
$this->customerSession->logout();
4541
}
4642

47-
$loggedIn = $this->customerSession->loginById($customerId);
48-
if ($loggedIn) {
49-
$this->customerSession->regenerateId();
50-
$this->customerSession->setLoggedAsCustomerAdmindId($adminId);
43+
$loggedIn = $this->customerSession->loginById($authenticationData->getCustomerId());
44+
if (!$loggedIn) {
45+
throw new LocalizedException(__('Login was not successful.'));
5146
}
5247

53-
return $loggedIn;
48+
$this->customerSession->regenerateId();
49+
$this->customerSession->setLoggedAsCustomerAdmindId($authenticationData->getAdminId());
5450
}
5551
}

app/code/Magento/LoginAsCustomerSales/Plugin/AuthenticateCustomer.php

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use Magento\Customer\Model\Session as CustomerSession;
1111
use Magento\Checkout\Model\Session as CheckoutSession;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\LoginAsCustomer\Api\Data\AuthenticationDataInterface;
1214
use Magento\Quote\Api\CartRepositoryInterface;
1315
use Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface;
1416

@@ -51,54 +53,44 @@ public function __construct(
5153
* Remove all items from guest shopping cart
5254
*
5355
* @param AuthenticateCustomerInterface $subject
54-
* @param int $customerId
55-
* @param int $adminId
56-
* @throws \Magento\Framework\Exception\LocalizedException
57-
* @throws \Magento\Framework\Exception\NoSuchEntityException
56+
* @param AuthenticationDataInterface $authenticationData
57+
* @return null
58+
* @throws LocalizedException
5859
*
5960
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6061
*/
6162
public function beforeExecute(
6263
AuthenticateCustomerInterface $subject,
63-
int $customerId,
64-
int $adminId
64+
AuthenticationDataInterface $authenticationData
6565
) {
6666
if (!$this->customerSession->getId()) {
6767
$quote = $this->checkoutSession->getQuote();
6868
/* Remove items from guest cart */
69-
foreach ($quote->getAllVisibleItems() as $item) {
70-
$quote->removeItem($item->getId());
71-
}
72-
$this->quoteRepository->save($quote);
69+
$quote->removeAllItems();
7370
}
71+
return null;
7472
}
7573

7674
/**
77-
* Mark customer cart as not guest
75+
* Mark customer cart as not-guest
7876
*
79-
* @param int $adminId
8077
* @param AuthenticateCustomerInterface $subject
81-
* @param bool $result
82-
* @param int $customerId
83-
* @return bool
84-
* @throws \Magento\Framework\Exception\LocalizedException
85-
* @throws \Magento\Framework\Exception\NoSuchEntityException
78+
* @param null $result
79+
* @param AuthenticationDataInterface $authenticationData
80+
* @return void
81+
* @throws LocalizedException
8682
*
8783
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8884
*/
8985
public function afterExecute(
9086
AuthenticateCustomerInterface $subject,
91-
bool $result,
92-
int $customerId,
93-
int $adminId
87+
$result,
88+
AuthenticationDataInterface $authenticationData
9489
) {
95-
if ($result) {
96-
$this->checkoutSession->loadCustomerQuote();
90+
$this->checkoutSession->loadCustomerQuote();
91+
$quote = $this->checkoutSession->getQuote();
9792

98-
$quote = $this->checkoutSession->getQuote();
99-
$quote->setCustomerIsGuest(0);
100-
$this->quoteRepository->save($quote);
101-
}
102-
return $result;
93+
$quote->setCustomerIsGuest(0);
94+
$this->quoteRepository->save($quote);
10395
}
10496
}

0 commit comments

Comments
 (0)