1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Command; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
use Startwind\Inventorio\Config\Config; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
abstract class InventorioCommand extends Command |
11
|
|
|
{ |
12
|
|
|
protected Config $config; |
13
|
|
|
|
14
|
|
|
protected function configure(): void |
15
|
|
|
{ |
16
|
|
|
$this->addOption('configFile', 'c', InputOption::VALUE_OPTIONAL, 'The configuration file', __DIR__ . '/../../config/default.yml'); |
17
|
|
|
$this->addOption('debug', '', InputOption::VALUE_NONE, 'Switch on debug mode'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Return the path to the configuration file. |
22
|
|
|
*/ |
23
|
|
|
protected function getConfigFile(): string |
24
|
|
|
{ |
25
|
|
|
return $this->config->getConfigFile(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function initConfiguration(string $configFile): void |
29
|
|
|
{ |
30
|
|
|
$this->config = new Config($configFile); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return true if the application is already initialized. |
35
|
|
|
*/ |
36
|
|
|
protected function isInitialized(): bool |
37
|
|
|
{ |
38
|
|
|
$configFile = $this->getConfigFile(); |
39
|
|
|
return file_exists($configFile); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return the server identifier. |
44
|
|
|
*/ |
45
|
|
|
protected function getServerId(): string |
46
|
|
|
{ |
47
|
|
|
if (!$this->isInitialized()) { |
48
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
52
|
|
|
|
53
|
|
|
return $config['serverId']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return true if remote is enabled |
58
|
|
|
*/ |
59
|
|
|
protected function isRemoteEnabled(): bool |
60
|
|
|
{ |
61
|
|
|
if (!$this->isInitialized()) { |
62
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
66
|
|
|
|
67
|
|
|
if (!array_key_exists('remote', $config)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $config['remote']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function isCollectEnabled(): bool |
75
|
|
|
{ |
76
|
|
|
if (!$this->isInitialized()) { |
77
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
81
|
|
|
|
82
|
|
|
if (!array_key_exists('collect', $config)) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $config['collect']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function isSmartCareEnabled(): bool |
90
|
|
|
{ |
91
|
|
|
if (!$this->isInitialized()) { |
92
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
96
|
|
|
|
97
|
|
|
if (!array_key_exists('smartCare', $config)) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $config['smartCare']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
protected function areLogfilesEnabled(): bool |
106
|
|
|
{ |
107
|
|
|
if (!$this->isInitialized()) { |
108
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
112
|
|
|
|
113
|
|
|
if (!array_key_exists('logfile', $config)) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $config['logfile']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function setRemoteEnabled(bool $isEnabled): void |
121
|
|
|
{ |
122
|
|
|
if (!$this->isInitialized()) { |
123
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
127
|
|
|
|
128
|
|
|
$config['remote'] = $isEnabled; |
129
|
|
|
|
130
|
|
|
file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function setLogfileEnabled(bool $isEnabled): void |
134
|
|
|
{ |
135
|
|
|
if (!$this->isInitialized()) { |
136
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
140
|
|
|
|
141
|
|
|
$config['logfile'] = $isEnabled; |
142
|
|
|
|
143
|
|
|
file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function setCollectEnabled(bool $isEnabled): void |
147
|
|
|
{ |
148
|
|
|
if (!$this->isInitialized()) { |
149
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
153
|
|
|
|
154
|
|
|
$config['collect'] = $isEnabled; |
155
|
|
|
|
156
|
|
|
file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function setSmartCareEnabled(bool $isEnabled): void |
160
|
|
|
{ |
161
|
|
|
if (!$this->isInitialized()) { |
162
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
166
|
|
|
|
167
|
|
|
$config['smartCare'] = $isEnabled; |
168
|
|
|
|
169
|
|
|
file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
protected function setServerApi(string $serverApi): void |
174
|
|
|
{ |
175
|
|
|
if (!$this->isInitialized()) { |
176
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
180
|
|
|
|
181
|
|
|
$config['serverApi'] = $serverApi; |
182
|
|
|
|
183
|
|
|
file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Return the user identifier. |
188
|
|
|
*/ |
189
|
|
|
protected function getUserId(): string |
190
|
|
|
{ |
191
|
|
|
if (!$this->isInitialized()) { |
192
|
|
|
throw new RuntimeException('System was not initialized yet.'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$config = json_decode(file_get_contents($this->getConfigFile()), true); |
196
|
|
|
|
197
|
|
|
return $config['userId']; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|