Skip to content

Commit 0c65df3

Browse files
committed
magento2-login-as-customer/issues/104: Global refactoring
- Introduce Magento\LoginAsCustomer\Api\ConfigInterface
1 parent ac4aa67 commit 0c65df3

File tree

10 files changed

+131
-68
lines changed

10 files changed

+131
-68
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\LoginAsCustomer\Api;
9+
10+
/**
11+
* LoginAsCustomer config
12+
*
13+
* @api
14+
*/
15+
interface ConfigInterface
16+
{
17+
/**
18+
* Check if Login As Customer extension is enabled
19+
*
20+
* @return bool
21+
*/
22+
public function isEnabled(): bool;
23+
24+
/**
25+
* Check if store view manual choice is enabled
26+
*
27+
* @return bool
28+
*/
29+
public function isStoreManualChoiceEnabled(): bool;
30+
31+
/**
32+
* Gte secrets expiration time (in seconds)
33+
*
34+
* @return int
35+
*/
36+
public function getSecretExpirationTime(): int;
37+
}

app/code/Magento/LoginAsCustomer/Api/DeleteOldSecretsInterface.php renamed to app/code/Magento/LoginAsCustomer/Api/DeleteOutdatedSecretsInterface.php

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

1010
/**
11+
* Delete outdated secret records
12+
*
1113
* @api
1214
*/
13-
interface DeleteOldSecretsInterface
15+
interface DeleteOutdatedSecretsInterface
1416
{
1517
/**
16-
* Delete old secret key records
18+
* Delete outdated secret records
1719
*/
18-
public function execute():void;
20+
public function execute(): void;
1921
}

app/code/Magento/LoginAsCustomer/Cron/DeleteOldSecrets.php renamed to app/code/Magento/LoginAsCustomer/Cron/DeleteOutdatedSecrets.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,31 @@
77

88
namespace Magento\LoginAsCustomer\Cron;
99

10-
use Magento\LoginAsCustomer\Api\DeleteOldSecretsInterface;
11-
use Magento\LoginAsCustomer\Model\Config;
10+
use Magento\LoginAsCustomer\Api\ConfigInterface;
11+
use Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface;
1212

1313
/**
1414
* @api
1515
*/
16-
class DeleteOldSecrets implements DeleteOldSecretsInterface
16+
class DeleteOutdatedSecrets implements DeleteOutdatedSecretsInterface
1717
{
1818
/**
19-
* @var DeleteOldSecretsInterface
19+
* @var DeleteOutdatedSecretsInterface
2020
*/
2121
private $deleteOldSecretsProcessor;
2222

2323
/**
24-
* @var Config
24+
* @var ConfigInterface
2525
*/
2626
private $config;
2727

2828
/**
29-
* DeleteOldSecrets constructor.
30-
* @param DeleteOldSecretsInterface $deleteOldSecretsProcessor
31-
* @param Config $config
29+
* @param DeleteOutdatedSecretsInterface $deleteOldSecretsProcessor
30+
* @param ConfigInterface $config
3231
*/
3332
public function __construct(
34-
DeleteOldSecretsInterface $deleteOldSecretsProcessor,
35-
Config $config
33+
DeleteOutdatedSecretsInterface $deleteOldSecretsProcessor,
34+
ConfigInterface $config
3635
) {
3736
$this->deleteOldSecretsProcessor = $deleteOldSecretsProcessor;
3837
$this->config = $config;

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

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,56 @@
88
namespace Magento\LoginAsCustomer\Model;
99

1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11-
use Magento\Framework\App\ProductMetadataInterface;
12-
use Magento\Store\Model\ScopeInterface;
11+
use Magento\LoginAsCustomer\Api\ConfigInterface;
1312

1413
/**
15-
* LoginAsCustomer module config model.
14+
* @inheritdoc
1615
*/
17-
class Config
16+
class Config implements ConfigInterface
1817
{
1918
/**
2019
* Extension config path
2120
*/
22-
private const XML_PATH_EXTENSION_ENABLED = 'loginascustomer/general/enabled';
23-
private const ENABLE_STORE_VIEW_MANUAL_CHOICE = 'loginascustomer/general/enable_store_view_manual_choice';
24-
25-
public const TIME_FRAME = 60;
21+
private const XML_PATH_ENABLED = 'login_as_customer/general/enabled';
22+
private const XML_PATH_STORE_VIEW_MANUAL_CHOICE_ENABLED
23+
= 'login_as_customer/general/store_view_manual_choice_enabled';
24+
private const XML_PATH_SECRETS_EXPIRATION_TIME = 'login_as_customer/general/secrets_expiration_time';
2625

2726
/**
2827
* @var ScopeConfigInterface
2928
*/
30-
3129
private $scopeConfig;
3230

33-
/**
34-
* @var \Magento\Framework\App\ProductMetadataInterface
35-
*/
36-
private $metadata;
37-
3831
/**
3932
* @param ScopeConfigInterface $scopeConfig
40-
* @param ProductMetadataInterface $metadata
4133
*/
4234
public function __construct(
43-
ScopeConfigInterface $scopeConfig,
44-
ProductMetadataInterface $metadata
35+
ScopeConfigInterface $scopeConfig
4536
) {
4637
$this->scopeConfig = $scopeConfig;
47-
$this->metadata = $metadata;
4838
}
4939

5040
/**
51-
* Check if Login As Customer extension is enabled.
52-
*
53-
* @return bool
41+
* @inheritdoc
5442
*/
5543
public function isEnabled(): bool
5644
{
57-
return (bool)$this->scopeConfig->getValue(
58-
self::XML_PATH_EXTENSION_ENABLED,
59-
ScopeInterface::SCOPE_STORE,
60-
null
61-
);
45+
return (bool)$this->scopeConfig->getValue(self::XML_PATH_ENABLED);
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function isStoreManualChoiceEnabled(): bool
52+
{
53+
return (bool)$this->scopeConfig->getValue(self::XML_PATH_STORE_VIEW_MANUAL_CHOICE_ENABLED);
6254
}
6355

6456
/**
65-
* Check if store view manual choice is enabled.
66-
*
67-
* @return bool
57+
* @inheritdoc
6858
*/
69-
public function isManualChoiceEnabled(): bool
59+
public function getSecretExpirationTime(): int
7060
{
71-
return (bool)$this->scopeConfig->getValue(
72-
self::ENABLE_STORE_VIEW_MANUAL_CHOICE,
73-
ScopeInterface::SCOPE_STORE,
74-
null
75-
);
61+
return (int)$this->scopeConfig->getValue(self::XML_PATH_SECRETS_EXPIRATION_TIME);
7662
}
7763
}

app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteOldSecrets.php renamed to app/code/Magento/LoginAsCustomer/Model/ResourceModel/DeleteOutdatedSecrets.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Stdlib\DateTime\DateTime;
12-
use Magento\LoginAsCustomer\Api\DeleteOldSecretsInterface;
13-
use Magento\LoginAsCustomer\Model\Config;
12+
use Magento\LoginAsCustomer\Api\ConfigInterface;
13+
use Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface;
1414

1515
/**
16-
* @api
16+
* @inheritdoc
1717
*/
18-
class DeleteOldSecrets implements DeleteOldSecretsInterface
18+
class DeleteOutdatedSecrets implements DeleteOutdatedSecretsInterface
1919
{
2020
/**
2121
* @var ResourceConnection
@@ -27,26 +27,35 @@ class DeleteOldSecrets implements DeleteOldSecretsInterface
2727
*/
2828
private $dateTime;
2929

30+
/**
31+
* @var ConfigInterface
32+
*/
33+
private $config;
34+
3035
/**
3136
* @param ResourceConnection $resourceConnection
37+
* @param DateTime $dateTime
38+
* @param ConfigInterface $config
3239
*/
3340
public function __construct(
3441
ResourceConnection $resourceConnection,
35-
DateTime $dateTime
42+
DateTime $dateTime,
43+
ConfigInterface $config
3644
) {
3745
$this->resourceConnection = $resourceConnection;
3846
$this->dateTime = $dateTime;
47+
$this->config = $config;
3948
}
4049

4150
/**
42-
* Delete old secret key records
51+
* @inheritdoc
4352
*/
44-
public function execute():void
53+
public function execute(): void
4554
{
4655
$connection = $this->resourceConnection->getConnection();
4756
$tableName = $this->resourceConnection->getTableName('login_as_customer');
4857

49-
$timePoint = date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - Config::TIME_FRAME);
58+
$timePoint = date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - $this->config->getSecretExpirationTime());
5059

5160
$connection->delete(
5261
$tableName,

app/code/Magento/LoginAsCustomer/Model/ResourceModel/GetAuthenticateData.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Stdlib\DateTime\DateTime;
1212
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\LoginAsCustomer\Api\ConfigInterface;
1314
use Magento\LoginAsCustomer\Api\GetAuthenticateDataInterface;
14-
use Magento\LoginAsCustomer\Model\Config;
1515

1616
/**
1717
* @api
@@ -28,15 +28,24 @@ class GetAuthenticateData implements GetAuthenticateDataInterface
2828
*/
2929
private $dateTime;
3030

31+
/**
32+
* @var ConfigInterface
33+
*/
34+
private $config;
35+
3136
/**
3237
* @param ResourceConnection $resourceConnection
38+
* @param DateTime $dateTime
39+
* @param ConfigInterface $config
3340
*/
3441
public function __construct(
3542
ResourceConnection $resourceConnection,
36-
DateTime $dateTime
43+
DateTime $dateTime,
44+
ConfigInterface $config
3745
) {
3846
$this->resourceConnection = $resourceConnection;
3947
$this->dateTime = $dateTime;
48+
$this->config = $config;
4049
}
4150

4251
/**
@@ -50,7 +59,7 @@ public function execute(string $secretKey):array
5059
$connection = $this->resourceConnection->getConnection();
5160
$tableName = $this->resourceConnection->getTableName('login_as_customer');
5261

53-
$timePoint = date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - Config::TIME_FRAME);
62+
$timePoint = date('Y-m-d H:i:s', $this->dateTime->gmtTimestamp() - $this->config->getSecretExpirationTime());
5463

5564
$select = $connection->select()
5665
->from(['main_table' => $tableName])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
10+
<default>
11+
<login_as_customer>
12+
<general>
13+
<enabled>0</enabled>
14+
<store_view_manual_choice_enabled>0</store_view_manual_choice_enabled>
15+
<secrets_expiration_time>60</secrets_expiration_time>
16+
</general>
17+
</login_as_customer>
18+
</default>
19+
</config>

app/code/Magento/LoginAsCustomer/etc/di.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
<preference for="Magento\LoginAsCustomer\Api\GetAuthenticateDataInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\GetAuthenticateData" />
1111
<preference for="Magento\LoginAsCustomer\Api\AuthenticateCustomerInterface" type="Magento\LoginAsCustomer\Model\AuthenticateCustomer" />
1212
<preference for="Magento\LoginAsCustomer\Api\DeleteSecretInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteSecret" />
13-
<preference for="Magento\LoginAsCustomer\Api\DeleteOldSecretsInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteOldSecrets" />
13+
<preference for="Magento\LoginAsCustomer\Api\DeleteOutdatedSecretsInterface" type="Magento\LoginAsCustomer\Model\ResourceModel\DeleteOutdatedSecrets" />
14+
<preference for="Magento\LoginAsCustomer\Api\ConfigInterface" type="Magento\LoginAsCustomer\Model\Config" />
1415
</config>

app/code/Magento/LoginAsCustomerUi/Controller/Adminhtml/Login/Login.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Backend\App\Action;
1414
use Magento\Customer\Api\CustomerRepositoryInterface;
1515
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\LoginAsCustomer\Api\ConfigInterface;
1617
use Magento\LoginAsCustomer\Api\CreateSecretInterface;
1718

1819
/**
@@ -51,7 +52,7 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
5152
private $customerRepository;
5253

5354
/**
54-
* @var \Magento\LoginAsCustomer\Model\Config
55+
* @var ConfigInterface
5556
*/
5657
private $config;
5758

@@ -67,7 +68,7 @@ class Login extends Action implements HttpGetActionInterface, HttpPostActionInte
6768
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
6869
* @param \Magento\Framework\Url $url
6970
* @param CustomerRepositoryInterface $customerRepository
70-
* @param \Magento\LoginAsCustomer\Model\Config $config,
71+
* @param ConfigInterface $config,
7172
* @param CreateSecretInterface $createSecretProcessor
7273
*/
7374
public function __construct(
@@ -76,7 +77,7 @@ public function __construct(
7677
\Magento\Store\Model\StoreManagerInterface $storeManager,
7778
\Magento\Framework\Url $url,
7879
CustomerRepositoryInterface $customerRepository,
79-
\Magento\LoginAsCustomer\Model\Config $config,
80+
ConfigInterface $config,
8081
CreateSecretInterface $createSecretProcessor
8182
) {
8283
parent::__construct($context);
@@ -111,14 +112,14 @@ public function execute(): ResultInterface
111112
}
112113

113114
try {
114-
$customer = $this->customerRepository->getById($customerId);
115+
$this->customerRepository->getById($customerId);
115116
} catch (NoSuchEntityException $e) {
116117
$this->messageManager->addErrorMessage(__('Customer with this ID are no longer exist.'));
117118
return $resultRedirect->setPath('customer/index/index');
118119
}
119120

120121
$customerStoreId = $request->getParam('store_id');
121-
if (!isset($customerStoreId) && $this->config->isManualChoiceEnabled()) {
122+
if (!isset($customerStoreId) && $this->config->isStoreManualChoiceEnabled()) {
122123
$this->messageManager->addNoticeMessage(__('Please select a Store View to login in.'));
123124
return $resultRedirect->setPath('loginascustomer/login/manual', ['entity_id' => $customerId ]);
124125
}

app/code/Magento/LoginAsCustomerUi/ViewModel/Configuration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\LoginAsCustomerUi\ViewModel;
99

1010
use Magento\Customer\Model\Context;
11+
use Magento\LoginAsCustomer\Api\ConfigInterface;
1112

1213
/**
1314
* View model to get extension configuration in the template
@@ -28,12 +29,11 @@ class Configuration implements \Magento\Framework\View\Element\Block\ArgumentInt
2829
private $httpContext;
2930

3031
/**
31-
* Configuration constructor.
32-
* @param \Magento\LoginAsCustomer\Model\Config $config
32+
* @param ConfigInterface $config
3333
* @param \Magento\Framework\App\Http\Context $httpContext
3434
*/
3535
public function __construct(
36-
\Magento\LoginAsCustomer\Model\Config $config,
36+
ConfigInterface $config,
3737
\Magento\Framework\App\Http\Context $httpContext
3838
) {
3939
$this->config = $config;

0 commit comments

Comments
 (0)