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