|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bex\Behat\Magento2InitExtension\Fixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface; |
|
6
|
|
|
use Magento\Framework\App\Config\Storage\WriterInterface; |
|
7
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
|
8
|
|
|
use Magento\Framework\App\CacheInterface; |
|
9
|
|
|
use Magento\Store\Model\ScopeInterface; |
|
10
|
|
|
|
|
11
|
|
|
class MagentoConfigManager extends BaseFixture |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $configAttributes = ['path', 'value', 'scope_type', 'scope_code']; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $defaultConfig = ['scope_type' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'scope_code' => null]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ScopeConfigInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $configReader; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var WriterInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $configWriter; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var StoreManagerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $storeManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var CacheInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $cache; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $originalConfigs = []; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
|
|
parent::__construct(); |
|
51
|
|
|
$this->configReader = $this->getMagentoObject(ScopeConfigInterface::class); |
|
52
|
|
|
$this->configWriter = $this->getMagentoObject(WriterInterface::class); |
|
53
|
|
|
$this->storeManager = $this->getMagentoObject(StoreManagerInterface::class); |
|
54
|
|
|
$this->cache = $this->getMagentoObject(CacheInterface::class); |
|
55
|
|
|
$this->originalConfigs = []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param array $configs |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function changeConfigs(array $configs) |
|
64
|
|
|
{ |
|
65
|
|
|
foreach ($configs as $config) { |
|
66
|
|
|
$config = array_merge($this->defaultConfig, $config); |
|
67
|
|
|
if ($this->isValidConfig($config)) { |
|
68
|
|
|
$this->changeConfig($config['path'], $config['value'], $config['scope_type'], $config['scope_code']); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->cache->clean(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Revert the original configs |
|
77
|
|
|
*/ |
|
78
|
|
|
public function revertAllConfig() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->changeConfigs($this->originalConfigs); |
|
81
|
|
|
$this->originalConfigs = []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $config |
|
86
|
|
|
* |
|
87
|
|
|
* @return boolean |
|
88
|
|
|
*/ |
|
89
|
|
|
private function isValidConfig(array $config) |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($this->configAttributes as $attribute) { |
|
92
|
|
|
if (!array_key_exists($attribute, $config)) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return !empty($config['path']); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $path |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* @param string $scopeType |
|
104
|
|
|
* @param string $scopeCode |
|
105
|
|
|
*/ |
|
106
|
|
|
private function changeConfig($path, $value, $scopeType, $scopeCode) |
|
107
|
|
|
{ |
|
108
|
|
|
$originalValue = $this->configReader->getValue($path, $scopeType, $scopeCode); |
|
109
|
|
|
$this->storeOrigConfig($path, $originalValue, $scopeType, $scopeCode); |
|
110
|
|
|
$this->configWriter->save($path, $value, $scopeType, $this->getScopeIdByScopeCode($scopeType, $scopeCode)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $path |
|
115
|
|
|
* @param string $value |
|
116
|
|
|
* @param string $scopeType |
|
117
|
|
|
* @param string $scopeCode |
|
118
|
|
|
*/ |
|
119
|
|
|
private function storeOrigConfig($path, $value, $scopeType, $scopeCode) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->originalConfigs[] = [ |
|
122
|
|
|
'path' => $path, |
|
123
|
|
|
'value' => $value, |
|
124
|
|
|
'scope_type' => $scopeType, |
|
125
|
|
|
'scope_code' => $scopeCode |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $scopeType |
|
131
|
|
|
* @param string $scopeCode |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
private function getScopeIdByScopeCode($scopeType, $scopeCode) |
|
136
|
|
|
{ |
|
137
|
|
|
$scopeId = 0; |
|
138
|
|
|
|
|
139
|
|
|
$scopesByCode = $this->getScopesByCode($scopeType); |
|
140
|
|
|
|
|
141
|
|
|
if (isset($scopesByCode[$scopeCode])) { |
|
142
|
|
|
$scope = $scopesByCode[$scopeCode]; |
|
143
|
|
|
$scopeId = $scope->getId(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $scopeId; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $scopeType |
|
151
|
|
|
* |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
private function getScopesByCode($scopeType) |
|
155
|
|
|
{ |
|
156
|
|
|
$scopesByType = []; |
|
157
|
|
|
|
|
158
|
|
|
if ($scopeType == ScopeInterface::SCOPE_STORES) { |
|
159
|
|
|
$scopesByType = $this->storeManager->getStores(false, true); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if ($scopeType == ScopeInterface::SCOPE_WEBSITES) { |
|
163
|
|
|
$scopesByType = $this->storeManager->getWebsites(false, true); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $scopesByType; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|