Skip to content

Commit d0191e7

Browse files
ISS-33802: refactor DeploymentConfig class in order to ensure that all methods follow the same reload logic
1 parent a2e458a commit d0191e7

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

lib/internal/Magento/Framework/App/DeploymentConfig.php

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DeploymentConfig
3838
*
3939
* @var array
4040
*/
41-
private $flatData = [];
41+
private $flatData;
4242

4343
/**
4444
* Injected configuration data
@@ -55,7 +55,7 @@ class DeploymentConfig
5555
* @param DeploymentConfig\Reader $reader
5656
* @param array $overrideData
5757
*/
58-
public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
58+
public function __construct(DeploymentConfig\Reader $reader, array $overrideData = [])
5959
{
6060
$this->reader = $reader;
6161
$this->overrideData = $overrideData;
@@ -64,63 +64,50 @@ public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
6464
/**
6565
* Gets data from flattened data
6666
*
67-
* @param string $key
67+
* @param string|null $key
6868
* @param mixed $defaultValue
6969
* @return mixed|null
7070
* @throws FileSystemException
7171
* @throws RuntimeException
7272
*/
73-
public function get($key = null, $defaultValue = null)
73+
public function get(string $key = null, $defaultValue = null)
7474
{
7575
$result = $this->getByKey($key);
7676
if ($result === null) {
77-
$this->resetData();
78-
$this->load();
77+
$this->reloadData();
7978
$result = $this->getByKey($key);
8079
}
8180
return $result ?? $defaultValue;
8281
}
8382

84-
/**
85-
* Checks if data available
86-
*
87-
* @return bool
88-
* @throws FileSystemException
89-
* @throws RuntimeException
90-
*/
91-
public function isAvailable()
92-
{
93-
$this->load();
94-
return isset($this->flatData[ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE]);
95-
}
96-
9783
/**
9884
* Gets a value specified key from config data
9985
*
100-
* @param string $key
86+
* @param string|null $key
10187
* @return null|mixed
10288
* @throws FileSystemException
10389
* @throws RuntimeException
10490
*/
105-
public function getConfigData($key = null)
91+
public function getConfigData(string $key = null)
10692
{
107-
$this->load();
108-
109-
if ($key !== null && !isset($this->data[$key])) {
110-
return null;
93+
$result = $this->getConfigDataByKey($key);
94+
if ($result === null) {
95+
$this->reloadData();
96+
$result = $this->getConfigDataByKey($key);
11197
}
112-
113-
return $this->data[$key] ?? $this->data;
98+
return $result;
11499
}
115100

116101
/**
117-
* Resets config data
102+
* Checks if data available
118103
*
119-
* @return void
104+
* @return bool
105+
* @throws FileSystemException
106+
* @throws RuntimeException
120107
*/
121-
public function resetData()
108+
public function isAvailable(): bool
122109
{
123-
$this->data = null;
110+
return $this->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE) !== null;
124111
}
125112

126113
/**
@@ -131,10 +118,20 @@ public function resetData()
131118
* @throws RuntimeException
132119
* @since 100.1.3
133120
*/
134-
public function isDbAvailable()
121+
public function isDbAvailable(): bool
135122
{
136-
$this->load();
137-
return isset($this->data['db']);
123+
return $this->getConfigData('db') !== null;
124+
}
125+
126+
/**
127+
* Resets config data
128+
*
129+
* @return void
130+
*/
131+
public function resetData(): void
132+
{
133+
$this->data = null;
134+
$this->flatData = null;
138135
}
139136

140137
/**
@@ -144,16 +141,14 @@ public function isDbAvailable()
144141
* @throws FileSystemException
145142
* @throws RuntimeException
146143
*/
147-
private function load()
144+
private function reloadData(): void
148145
{
149-
if (empty($this->data)) {
150-
$this->data = $this->reader->load();
151-
if ($this->overrideData) {
152-
$this->data = array_replace($this->data, $this->overrideData);
153-
}
154-
// flatten data for config retrieval using get()
155-
$this->flatData = $this->flattenParams($this->data);
146+
$this->data = $this->reader->load();
147+
if ($this->overrideData) {
148+
$this->data = array_replace($this->data, $this->overrideData);
156149
}
150+
// flatten data for config retrieval using get()
151+
$this->flatData = $this->flattenParams($this->data);
157152
}
158153

159154
/**
@@ -163,12 +158,12 @@ private function load()
163158
* each level of array is accessible by path key
164159
*
165160
* @param array $params
166-
* @param string $path
167-
* @param array $flattenResult
161+
* @param string|null $path
162+
* @param array|null $flattenResult
168163
* @return array
169164
* @throws RuntimeException
170165
*/
171-
private function flattenParams(array $params, $path = null, array &$flattenResult = null): array
166+
private function flattenParams(array $params, ?string $path = null, array &$flattenResult = null): array
172167
{
173168
if (null === $flattenResult) {
174169
$flattenResult = [];
@@ -195,17 +190,29 @@ private function flattenParams(array $params, $path = null, array &$flattenResul
195190

196191
/**
197192
* @param string|null $key
198-
* @return array|mixed|string
193+
* @return mixed|null
199194
*/
200-
protected function getByKey($key)
195+
private function getByKey(?string $key)
201196
{
202197
if ($key === null) {
203198
return $this->flatData ?: null;
204199
}
205-
if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
200+
if (is_array($this->flatData) && array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
206201
return '';
207202
}
208203

209204
return $this->flatData[$key] ?? null;
210205
}
206+
207+
/**
208+
* @param string|null $key
209+
* @return mixed|null
210+
*/
211+
private function getConfigDataByKey(?string $key)
212+
{
213+
if ($key === null) {
214+
return $this->data;
215+
}
216+
return $this->data[$key] ?? null;
217+
}
211218
}

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfigTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testNotAvailable(): void
149149
*/
150150
public function testNotAvailableThenAvailable(): void
151151
{
152-
$this->readerMock->expects($this->once())->method('load')->willReturn(['Test']);
152+
$this->readerMock->expects($this->exactly(2))->method('load')->willReturn(['Test']);
153153
$object = new DeploymentConfig($this->readerMock);
154154
$this->assertFalse($object->isAvailable());
155155
$this->assertFalse($object->isAvailable());
@@ -191,7 +191,7 @@ public function keyCollisionDataProvider(): array
191191
*/
192192
public function testResetData(): void
193193
{
194-
$this->readerMock->expects($this->once())->method('load')->willReturn(self::$fixture);
194+
$this->readerMock->expects($this->exactly(2))->method('load')->willReturn(self::$fixture);
195195
$this->assertSame(self::$flattenedFixture, $this->deploymentConfig->get());
196196
$this->deploymentConfig->resetData();
197197
// second time to ensure loader will be invoked only once after reset
@@ -208,7 +208,6 @@ public function testIsDbAvailable(): void
208208
{
209209
$this->readerMock->expects($this->exactly(2))->method('load')->willReturnOnConsecutiveCalls([], ['db' => []]);
210210
$this->assertFalse($this->deploymentConfig->isDbAvailable());
211-
$this->deploymentConfig->resetData();
212211
$this->assertTrue($this->deploymentConfig->isDbAvailable());
213212
}
214213

0 commit comments

Comments
 (0)