1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2017 Toan Nguyen. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Gojira\Framework\App\Configuration; |
10
|
|
|
|
11
|
|
|
use Gojira\Framework\Data\DataObject; |
12
|
|
|
use Gojira\Framework\Serializer\Serializer; |
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base class to work with configuration items |
17
|
|
|
* |
18
|
|
|
* @package Gojira\Framework\App\Configuration |
19
|
|
|
* @author Toan Nguyen <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Configuration extends DataObject implements ConfigurationInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Filesystem |
25
|
|
|
*/ |
26
|
|
|
private $filesystem; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PathInterface |
30
|
|
|
*/ |
31
|
|
|
private $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Configuration items |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $configItems = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Config constructor. |
42
|
|
|
* |
43
|
|
|
* @param array $data |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $data = []) |
46
|
|
|
{ |
47
|
|
|
$this->path = new Path(); |
48
|
|
|
$this->filesystem = new Filesystem(); |
49
|
|
|
$this->configItems = $this->getConfigItems(); |
50
|
|
|
|
51
|
|
|
parent::__construct($data); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function checkConfig() |
58
|
|
|
{ |
59
|
|
|
if ($this->filesystem->exists($this->path->getPath(PathInterface::CONFIG_PATH))) { |
60
|
|
|
$this->configItems = $this->getConfigItems(); |
61
|
|
|
if (!isset($this->configItems[self::OPTIONS]) |
62
|
|
|
|| !isset($this->configItems[self::OPTIONS][OptionsInterface::JIRA_STOP])) { |
63
|
|
|
return false; |
64
|
|
|
} else { |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Overwrite data in the object. |
74
|
|
|
* |
75
|
|
|
* The $key parameter can be string or array. |
76
|
|
|
* If $key is string, the attribute value will be overwritten by $value |
77
|
|
|
* |
78
|
|
|
* If $key is an array, it will overwrite all the data in the object. |
79
|
|
|
* |
80
|
|
|
* @param string|array $key |
81
|
|
|
* @param mixed $value |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function setData($key, $value = null) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
if ($key === (array)$key) { |
88
|
|
|
$this->_data = $key; |
89
|
|
|
} else { |
90
|
|
|
$this->_data[$key] = $value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function saveConfig() |
100
|
|
|
{ |
101
|
|
|
$result = []; |
102
|
|
|
$basePath = $this->path->getPath(); |
103
|
|
|
$configFilePath = $this->path->getPath(PathInterface::CONFIG_PATH); |
104
|
|
|
if (!$this->filesystem->exists($basePath)) { |
105
|
|
|
$this->filesystem->mkdir($basePath); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!$this->filesystem->exists($configFilePath)) { |
109
|
|
|
$jsonContent = Serializer::encode(($this->getData()) ?: $this->configItems); |
110
|
|
|
$this->filesystem->dumpFile($configFilePath, $jsonContent); |
111
|
|
|
|
112
|
|
|
$result['msg'] = 'Your top secret information has been sent to us. Thank you!'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function clearConfig() |
122
|
|
|
{ |
123
|
|
|
$result = []; |
124
|
|
|
$basePath = $this->path->getPath(); |
125
|
|
|
$configFilePath = $this->path->getPath(PathInterface::CONFIG_PATH); |
126
|
|
|
if (!$this->filesystem->exists($configFilePath)) { |
127
|
|
|
if ($this->filesystem->exists($basePath)) { |
128
|
|
|
$this->filesystem->remove($basePath); |
129
|
|
|
$result['msg'] = '<info>There is no stored data. Skipping.</info>'; |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
$this->filesystem->remove($configFilePath); |
133
|
|
|
$this->filesystem->remove($basePath); |
134
|
|
|
$this->configItems = $this->initDefaultConfig(); |
135
|
|
|
$result['msg'] = '<info>Configuration deleted successfully!</info>'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getConfigItems() |
145
|
|
|
{ |
146
|
|
|
if (empty($this->configItems)) { |
147
|
|
|
$this->setData($this->initDefaultConfig()); |
148
|
|
|
} elseif ($this->filesystem->exists($this->path->getPath(PathInterface::CONFIG_PATH))) { |
149
|
|
|
$this->setData(Serializer::decode($this->getFileContent())); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->configItems = $this->getData(); |
|
|
|
|
153
|
|
|
|
154
|
|
|
return $this->configItems; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get config file content |
159
|
|
|
* |
160
|
|
|
* @return bool|string |
161
|
|
|
*/ |
162
|
|
|
private function getFileContent() |
163
|
|
|
{ |
164
|
|
|
return file_get_contents($this->path->getPath(PathInterface::CONFIG_PATH)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Initialise the default config items |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
private function initDefaultConfig() |
173
|
|
|
{ |
174
|
|
|
return [ |
175
|
|
|
self::PATHS => $this->path->initDefaultPaths(), |
176
|
|
|
self::AUTH => [], |
177
|
|
|
self::OPTIONS => [] |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.