Skip to content

Commit f454fe3

Browse files
authored
ENGCOM-7023: Cleanup ObjectManager usage - Magento_EncryptionKey #27080
2 parents f09000e + d5e6e57 commit f454fe3

File tree

3 files changed

+82
-34
lines changed

3 files changed

+82
-34
lines changed

app/code/Magento/EncryptionKey/Controller/Adminhtml/Crypt/Key/Index.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key;
89

9-
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
10+
use Magento\Backend\App\Action\Context;
11+
use Magento\EncryptionKey\Block\Adminhtml\Crypt\Key\Form;
12+
use Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
14+
use Magento\Framework\App\DeploymentConfig\Writer;
1015

1116
/**
1217
* Key Index action
1318
*/
14-
class Index extends \Magento\EncryptionKey\Controller\Adminhtml\Crypt\Key implements HttpGetActionInterface
19+
class Index extends Key implements HttpGetActionInterface
1520
{
21+
/**
22+
* @var Writer
23+
*/
24+
private $writer;
25+
26+
/**
27+
* @param Context $context
28+
* @param Writer $writer
29+
*/
30+
public function __construct(
31+
Context $context,
32+
Writer $writer
33+
) {
34+
parent::__construct($context);
35+
$this->writer = $writer;
36+
}
37+
1638
/**
1739
* Render main page with form
1840
*
1941
* @return void
2042
*/
2143
public function execute()
2244
{
23-
/** @var \Magento\Framework\App\DeploymentConfig\Writer $writer */
24-
$writer = $this->_objectManager->get(\Magento\Framework\App\DeploymentConfig\Writer::class);
25-
if (!$writer->checkIfWritable()) {
26-
$this->messageManager->addError(__('Deployment configuration file is not writable.'));
45+
if (!$this->writer->checkIfWritable()) {
46+
$this->messageManager->addErrorMessage(__('Deployment configuration file is not writable.'));
2747
}
2848

2949
$this->_view->loadLayout();
3050
$this->_setActiveMenu('Magento_EncryptionKey::system_crypt_key');
3151
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Encryption Key'));
3252

3353
if (($formBlock = $this->_view->getLayout()->getBlock('crypt.key.form')) &&
34-
($data = $this->_objectManager->get(\Magento\Backend\Model\Session::class)->getFormData(true))) {
35-
/* @var \Magento\EncryptionKey\Block\Adminhtml\Crypt\Key\Form $formBlock */
54+
($data = $this->_session->getFormData(true))) {
55+
/* @var Form $formBlock */
3656
$formBlock->setFormData($data);
3757
}
3858

app/code/Magento/EncryptionKey/Setup/Patch/Data/SodiumChachaPatch.php

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,65 @@
77

88
namespace Magento\EncryptionKey\Setup\Patch\Data;
99

10+
use Magento\Config\Model\Config\Backend\Encrypted;
11+
use Magento\Config\Model\Config\Structure;
12+
use Magento\Framework\App\Area;
13+
use Magento\Framework\App\State;
14+
use Magento\Framework\Config\ScopeInterface;
15+
use Magento\Framework\Encryption\EncryptorInterface;
16+
use Magento\Framework\Setup\ModuleDataSetupInterface;
1017
use Magento\Framework\Setup\Patch\DataPatchInterface;
11-
use Magento\Framework\App\ObjectManager;
1218

1319
/**
1420
* Migrate encrypted configuration values to the latest cipher
1521
*/
1622
class SodiumChachaPatch implements DataPatchInterface
1723
{
1824
/**
19-
* @var \Magento\Framework\Config\ScopeInterface
20-
*/
21-
private $scope;
22-
23-
/**
24-
* @var \Magento\Framework\Setup\ModuleDataSetupInterface
25+
* @var ModuleDataSetupInterface
2526
*/
2627
private $moduleDataSetup;
2728

2829
/**
29-
* @var \Magento\Config\Model\Config\Structure
30+
* @var Structure
3031
*/
3132
private $structure;
3233

3334
/**
34-
* @var \Magento\Framework\Encryption\EncryptorInterface
35+
* @var EncryptorInterface
3536
*/
3637
private $encryptor;
3738

3839
/**
39-
* @var \Magento\Framework\App\State
40+
* @var State
4041
*/
4142
private $state;
4243

44+
/**
45+
* @var ScopeInterface
46+
*/
47+
private $scope;
48+
4349
/**
4450
* SodiumChachaPatch constructor.
45-
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
46-
* @param \Magento\Config\Model\Config\Structure\Proxy $structure
47-
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
48-
* @param \Magento\Framework\App\State $state
49-
* @param \Magento\Framework\Config\ScopeInterface|null $scope
51+
* @param ModuleDataSetupInterface $moduleDataSetup
52+
* @param Structure $structure
53+
* @param EncryptorInterface $encryptor
54+
* @param State $state
55+
* @param ScopeInterface $scope
5056
*/
5157
public function __construct(
52-
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
53-
\Magento\Config\Model\Config\Structure\Proxy $structure,
54-
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
55-
\Magento\Framework\App\State $state,
56-
\Magento\Framework\Config\ScopeInterface $scope = null
58+
ModuleDataSetupInterface $moduleDataSetup,
59+
Structure $structure,
60+
EncryptorInterface $encryptor,
61+
State $state,
62+
ScopeInterface $scope
5763
) {
5864
$this->moduleDataSetup = $moduleDataSetup;
5965
$this->structure = $structure;
6066
$this->encryptor = $encryptor;
6167
$this->state = $state;
62-
$this->scope = $scope ?? ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class);
68+
$this->scope = $scope;
6369
}
6470

6571
/**
@@ -72,6 +78,8 @@ public function apply()
7278
$this->reEncryptSystemConfigurationValues();
7379

7480
$this->moduleDataSetup->endSetup();
81+
82+
return $this;
7583
}
7684

7785
/**
@@ -106,19 +114,25 @@ private function reEncryptSystemConfigurationValues()
106114
$currentScope = $this->scope->getCurrentScope();
107115
$structure = $this->structure;
108116
$paths = $this->state->emulateAreaCode(
109-
\Magento\Framework\App\Area::AREA_ADMINHTML,
117+
Area::AREA_ADMINHTML,
110118
function () use ($structure) {
111-
$this->scope->setCurrentScope(\Magento\Framework\App\Area::AREA_ADMINHTML);
119+
$this->scope->setCurrentScope(Area::AREA_ADMINHTML);
112120
/** Returns list of structure paths to be re encrypted */
113121
$paths = $structure->getFieldPathsByAttribute(
114122
'backend_model',
115-
\Magento\Config\Model\Config\Backend\Encrypted::class
123+
Encrypted::class
116124
);
117125
/** Returns list of mapping between configPath => [structurePaths] */
118126
$mappedPaths = $structure->getFieldPaths();
119127
foreach ($mappedPaths as $mappedPath => $data) {
120128
foreach ($data as $structurePath) {
121-
if ($structurePath !== $mappedPath && $key = array_search($structurePath, $paths)) {
129+
if ($structurePath === $mappedPath) {
130+
continue;
131+
}
132+
133+
$key = array_search($structurePath, $paths);
134+
135+
if ($key) {
122136
$paths[$key] = $mappedPath;
123137
}
124138
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="\Magento\EncryptionKey\Setup\Patch\Data\SodiumChachaPatch">
10+
<arguments>
11+
<argument name="structure" xsi:type="object">Magento\Config\Model\Config\Structure\Proxy</argument>
12+
</arguments>
13+
</type>
14+
</config>

0 commit comments

Comments
 (0)