Skip to content

Commit c3e7587

Browse files
MAGETWO-61778: Implement config:set command
1 parent 1091b3e commit c3e7587

File tree

7 files changed

+262
-64
lines changed

7 files changed

+262
-64
lines changed

app/code/Magento/Config/Console/Command/ConfigSet/ConfigSetProcessorFactory.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,22 @@ public function __construct(
5555
/**
5656
* Creates an instance of specified processor.
5757
*
58-
* @param string $processor The name of processor
58+
* @param string $processorName The name of processor
5959
* @return ConfigSetProcessorInterface New processor instance
6060
* @throws ConfigurationMismatchException If processor type is not exists in processors array
61+
* or declared class has wrong implementation
6162
*/
62-
public function create($processor)
63+
public function create($processorName)
6364
{
64-
if (!isset($this->processors[$processor])) {
65-
throw new ConfigurationMismatchException(__('Class for type "%1" was not declared', $processor));
65+
if (!isset($this->processors[$processorName])) {
66+
throw new ConfigurationMismatchException(__('Class for type "%1" was not declared', $processorName));
6667
}
6768

68-
$object = $this->objectManager->create($this->processors[$processor]);
69+
$object = $this->objectManager->create($this->processors[$processorName]);
6970

7071
if (!$object instanceof ConfigSetProcessorInterface) {
7172
throw new ConfigurationMismatchException(
72-
__('%1 does not implement %2', get_class($object), ConfigSetProcessorInterface::class)
73+
__('%1 should implement %2', get_class($object), ConfigSetProcessorInterface::class)
7374
);
7475
}
7576

app/code/Magento/Config/Console/Command/ConfigSet/DefaultProcessor.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,48 @@
77

88
use Magento\Config\App\Config\Type\System;
99
use Magento\Config\Console\Command\ConfigSetCommand;
10+
use Magento\Config\Model\Config;
1011
use Magento\Config\Model\ConfigFactory;
1112
use Magento\Framework\App\Config\ConfigPathResolver;
1213
use Magento\Framework\App\DeploymentConfig;
1314
use Magento\Framework\Exception\CouldNotSaveException;
14-
use Magento\Framework\Exception\StateException;
1515
use Magento\Store\Model\ScopeInterface;
1616
use Symfony\Component\Console\Input\InputInterface;
1717

1818
/**
1919
* Processes default flow of config:set command.
20-
* This processor saves the value of configuration.
20+
* This processor saves the value of configuration into database.
2121
*
2222
* {@inheritdoc}
2323
*/
2424
class DefaultProcessor implements ConfigSetProcessorInterface
2525
{
2626
/**
27+
* The factory that creates config model instances.
28+
*
29+
* @see Config
2730
* @var ConfigFactory
2831
*/
2932
private $configFactory;
3033

3134
/**
35+
* The deployment configuration reader.
36+
*
3237
* @var DeploymentConfig
3338
*/
3439
private $deploymentConfig;
3540

3641
/**
42+
* The resolver for configuration paths according to source type.
43+
*
3744
* @var ConfigPathResolver
3845
*/
3946
private $configPathResolver;
4047

4148
/**
42-
* @param ConfigFactory $configFactory
43-
* @param DeploymentConfig $deploymentConfig
44-
* @param ConfigPathResolver $configPathResolver
49+
* @param ConfigFactory $configFactory The factory that creates config model instances
50+
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
51+
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
4552
*/
4653
public function __construct(
4754
ConfigFactory $configFactory,
@@ -58,17 +65,20 @@ public function __construct(
5865
* Requires installed application.
5966
*
6067
* {@inheritdoc}
61-
* @throws StateException
6268
*/
6369
public function process(InputInterface $input)
6470
{
65-
$path = $input->getArgument(ConfigSetCommand::ARG_PATH);
66-
$value = $input->getArgument(ConfigSetCommand::ARG_VALUE);
67-
$scope = $input->getOption(ConfigSetCommand::OPTION_SCOPE);
68-
$scopeCode = $input->getOption(ConfigSetCommand::OPTION_SCOPE_CODE);
71+
try {
72+
$path = $input->getArgument(ConfigSetCommand::ARG_PATH);
73+
$value = $input->getArgument(ConfigSetCommand::ARG_VALUE);
74+
$scope = $input->getOption(ConfigSetCommand::OPTION_SCOPE);
75+
$scopeCode = $input->getOption(ConfigSetCommand::OPTION_SCOPE_CODE);
76+
} catch (\Exception $exception) {
77+
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
78+
}
6979

7080
if (!$this->deploymentConfig->isAvailable()) {
71-
throw new StateException(
81+
throw new CouldNotSaveException(
7282
__(
7383
'We can\'t save this option because Magento is not installed. '
7484
. 'To lock this value, enter the command again using the --%1 option.',
@@ -86,16 +96,21 @@ public function process(InputInterface $input)
8696
);
8797
}
8898

89-
$config = $this->configFactory->create();
90-
$config->setDataByPath($path, $value);
99+
try {
100+
/** @var Config $config */
101+
$config = $this->configFactory->create();
102+
$config->setDataByPath($path, $value);
91103

92-
if (in_array($scope, [ScopeInterface::SCOPE_WEBSITE, ScopeInterface::SCOPE_WEBSITES])) {
93-
$config->setWebsite($scopeCode);
94-
} elseif (in_array($scope, [ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_STORES])) {
95-
$config->setStore($scopeCode);
96-
}
104+
if (in_array($scope, [ScopeInterface::SCOPE_WEBSITE, ScopeInterface::SCOPE_WEBSITES])) {
105+
$config->setWebsite($scopeCode);
106+
} elseif (in_array($scope, [ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_STORES])) {
107+
$config->setStore($scopeCode);
108+
}
97109

98-
$config->save();
110+
$config->save();
111+
} catch (\Exception $exception) {
112+
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
113+
}
99114
}
100115

101116
/**

app/code/Magento/Config/Console/Command/ConfigSet/LockProcessor.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Magento\Framework\App\DeploymentConfig;
1616
use Magento\Framework\Config\File\ConfigFilePool;
1717
use Magento\Framework\Exception\CouldNotSaveException;
18-
use Magento\Framework\Exception\FileSystemException;
1918
use Magento\Framework\Stdlib\ArrayManager;
2019
use Symfony\Component\Console\Input\InputInterface;
2120

@@ -28,42 +27,55 @@
2827
class LockProcessor implements ConfigSetProcessorInterface
2928
{
3029
/**
30+
* The deployment configuration reader.
31+
*
3132
* @var DeploymentConfig
3233
*/
3334
private $deploymentConfig;
3435

3536
/**
37+
* The deployment configuration writer.
38+
*
3639
* @var DeploymentConfig\Writer
3740
*/
3841
private $deploymentConfigWriter;
3942

4043
/**
44+
* An array manager for different manipulations with arrays.
45+
*
4146
* @var ArrayManager
4247
*/
4348
private $arrayManager;
4449

4550
/**
51+
* The resolver for configuration paths according to source type.
52+
*
4653
* @var ConfigPathResolver
4754
*/
4855
private $configPathResolver;
4956

5057
/**
58+
* The manager for system configuration structure.
59+
*
5160
* @var Structure
5261
*/
5362
private $configStructure;
5463

5564
/**
65+
* The factory for configuration value objects.
66+
*
67+
* @see Value
5668
* @var ValueFactory
5769
*/
5870
private $configValueFactory;
5971

6072
/**
61-
* @param DeploymentConfig $deploymentConfig
62-
* @param DeploymentConfig\Writer $writer
63-
* @param ArrayManager $arrayManager
64-
* @param ConfigPathResolver $configPathResolver
65-
* @param Structure $configStructure
66-
* @param ValueFactory $configValueFactory
73+
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
74+
* @param DeploymentConfig\Writer $writer The deployment configuration writer
75+
* @param ArrayManager $arrayManager An array manager for different manipulations with arrays
76+
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
77+
* @param Structure $configStructure The manager for system configuration structure
78+
* @param ValueFactory $configValueFactory The factory for configuration value objects
6779
*/
6880
public function __construct(
6981
DeploymentConfig $deploymentConfig,
@@ -89,27 +101,27 @@ public function __construct(
89101
*/
90102
public function process(InputInterface $input)
91103
{
92-
$path = $input->getArgument(ConfigSetCommand::ARG_PATH);
93-
$value = $input->getArgument(ConfigSetCommand::ARG_VALUE);
94-
$scope = $input->getOption(ConfigSetCommand::OPTION_SCOPE);
95-
$scopeCode = $input->getOption(ConfigSetCommand::OPTION_SCOPE_CODE);
96-
$configPath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);
104+
try {
105+
$path = $input->getArgument(ConfigSetCommand::ARG_PATH);
106+
$value = $input->getArgument(ConfigSetCommand::ARG_VALUE);
107+
$scope = $input->getOption(ConfigSetCommand::OPTION_SCOPE);
108+
$scopeCode = $input->getOption(ConfigSetCommand::OPTION_SCOPE_CODE);
97109

98-
/** @var Field $field */
99-
$field = $this->deploymentConfig->isAvailable()
100-
? $this->configStructure->getElement($path)
101-
: null;
102-
/** @var Value $backendModel */
103-
$backendModel = $field && $field->hasBackendModel()
104-
? $field->getBackendModel()
105-
: $this->configValueFactory->create();
110+
$configPath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);
111+
/** @var Field $field */
112+
$field = $this->deploymentConfig->isAvailable()
113+
? $this->configStructure->getElement($path)
114+
: null;
115+
/** @var Value $backendModel */
116+
$backendModel = $field && $field->hasBackendModel()
117+
? $field->getBackendModel()
118+
: $this->configValueFactory->create();
106119

107-
$backendModel->setPath($path);
108-
$backendModel->setScope($scope);
109-
$backendModel->setScopeId($scopeCode);
110-
$backendModel->setValue($value);
120+
$backendModel->setPath($path);
121+
$backendModel->setScope($scope);
122+
$backendModel->setScopeId($scopeCode);
123+
$backendModel->setValue($value);
111124

112-
try {
113125
/**
114126
* Temporary solution until Magento introduce unified interface
115127
* for storing system configuration into database and configuration files.
@@ -123,7 +135,7 @@ public function process(InputInterface $input)
123135
);
124136

125137
$backendModel->afterSave();
126-
} catch (FileSystemException $exception) {
138+
} catch (\Exception $exception) {
127139
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
128140
}
129141
}

app/code/Magento/Config/Console/Command/ConfigSetCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function configure()
9090
static::OPTION_SCOPE_CODE,
9191
null,
9292
InputArgument::OPTIONAL,
93-
'Scope code'
93+
'Scope code (required only if scope code is not \'default\')'
9494
),
9595
new InputOption(
9696
static::OPTION_LOCK,

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/ConfigSetProcessorFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function testCreateNonExisted()
7373

7474
/**
7575
* @expectedException \Magento\Framework\Exception\LocalizedException
76-
* @expectedExceptionMessage stdClass does not implement
76+
* @expectedExceptionMessage stdClass should implement
7777
*/
7878
public function testCreateWrongImplementation()
7979
{

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/DefaultProcessorTest.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
*/
66
namespace Magento\Config\Test\Unit\Console\Command\ConfigSet;
77

8+
use Magento\Config\App\Config\Type\System;
89
use Magento\Config\Console\Command\ConfigSet\DefaultProcessor;
910
use Magento\Config\Console\Command\ConfigSetCommand;
1011
use Magento\Config\Model\Config;
1112
use Magento\Config\Model\ConfigFactory;
1213
use Magento\Framework\App\Config\ConfigPathResolver;
1314
use Magento\Framework\App\Config\ScopeConfigInterface;
1415
use Magento\Framework\App\DeploymentConfig;
16+
use Magento\Store\Model\ScopeInterface;
1517
use PHPUnit_Framework_MockObject_MockObject as Mock;
1618
use Symfony\Component\Console\Input\InputInterface;
1719

@@ -85,11 +87,17 @@ protected function setUp()
8587
);
8688
}
8789

88-
public function testProcess()
90+
/**
91+
* Tests process of default flow.
92+
*
93+
* @param string $path
94+
* @param string $value
95+
* @param string $scope
96+
* @param string|null $scopeCode
97+
* @dataProvider processDataProvider
98+
*/
99+
public function testProcess($path, $value, $scope, $scopeCode)
89100
{
90-
$path = 'test/test/test';
91-
$value = 'value';
92-
93101
$this->inputMock->expects($this->any())
94102
->method('getArgument')
95103
->willReturnMap([
@@ -99,11 +107,12 @@ public function testProcess()
99107
$this->inputMock->expects($this->any())
100108
->method('getOption')
101109
->willReturnMap([
102-
[ConfigSetCommand::OPTION_SCOPE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT],
103-
[ConfigSetCommand::OPTION_SCOPE_CODE, null],
110+
[ConfigSetCommand::OPTION_SCOPE, $scope],
111+
[ConfigSetCommand::OPTION_SCOPE_CODE, $scopeCode],
104112
]);
105113
$this->configPathResolverMock->expects($this->once())
106114
->method('resolve')
115+
->with($path, $scope, $scopeCode, System::CONFIG_TYPE)
107116
->willReturn('system/default/test/test/test');
108117
$this->deploymentConfigMock->expects($this->once())
109118
->method('get')
@@ -119,6 +128,18 @@ public function testProcess()
119128
$this->model->process($this->inputMock);
120129
}
121130

131+
/**
132+
* @return array
133+
*/
134+
public function processDataProvider()
135+
{
136+
return [
137+
['test/test/test', 'value', ScopeConfigInterface::SCOPE_TYPE_DEFAULT, null],
138+
['test/test/test', 'value', ScopeInterface::SCOPE_WEBSITE, 'base'],
139+
['test/test/test', 'value', ScopeInterface::SCOPE_STORE, 'test'],
140+
];
141+
}
142+
122143
/**
123144
* @expectedException \Magento\Framework\Exception\LocalizedException
124145
* @expectedExceptionMessage We can't save this option because Magento is not installed.
@@ -143,6 +164,8 @@ public function testProcessNotInstalled()
143164
$this->deploymentConfigMock->expects($this->once())
144165
->method('isAvailable')
145166
->willReturn(false);
167+
$this->configMock->expects($this->never())
168+
->method('save');
146169

147170
$this->model->process($this->inputMock);
148171
}

0 commit comments

Comments
 (0)