InventorioCommand::getServerId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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