Passed
Push — master ( dfc848...4ef93f )
by Nils
02:39
created

InventorioCommand::setServerApi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
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
    }
18
19
    /**
20
     * Return the path to the configuration file.
21
     */
22
    protected function getConfigFile(): string
23
    {
24
        return $this->config->getConfigFile();
25
    }
26
27
    protected function initConfiguration(string $configFile): void
28
    {
29
        $this->config = new Config($configFile);
30
    }
31
32
    /**
33
     * Return true if the application is already initialized.
34
     */
35
    protected function isInitialized(): bool
36
    {
37
        $configFile = $this->getConfigFile();
38
        return file_exists($configFile);
39
    }
40
41
    /**
42
     * Return the server identifier.
43
     */
44
    protected function getServerId(): string
45
    {
46
        if (!$this->isInitialized()) {
47
            throw new RuntimeException('System was not initialized yet.');
48
        }
49
50
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
51
52
        return $config['serverId'];
53
    }
54
55
    /**
56
     * Return true if remote is enabled
57
     */
58
    protected function isRemoteEnabled(): bool
59
    {
60
        if (!$this->isInitialized()) {
61
            throw new RuntimeException('System was not initialized yet.');
62
        }
63
64
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
65
66
        if (!array_key_exists('remote', $config)) {
67
            return false;
68
        }
69
70
        return $config['remote'];
71
    }
72
73
    protected function isCollectEnabled(): bool
74
    {
75
        if (!$this->isInitialized()) {
76
            throw new RuntimeException('System was not initialized yet.');
77
        }
78
79
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
80
81
        if (!array_key_exists('collect', $config)) {
82
            return false;
83
        }
84
85
        return $config['collect'];
86
    }
87
88
89
    protected function areLogfilesEnabled(): 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('logfile', $config)) {
98
            return false;
99
        }
100
101
        return $config['logfile'];
102
    }
103
104
    protected function setRemoteEnabled(bool $isEnabled): void
105
    {
106
        if (!$this->isInitialized()) {
107
            throw new RuntimeException('System was not initialized yet.');
108
        }
109
110
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
111
112
        $config['remote'] = $isEnabled;
113
114
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);
115
    }
116
117
    protected function setLogfileEnabled(bool $isEnabled): void
118
    {
119
        if (!$this->isInitialized()) {
120
            throw new RuntimeException('System was not initialized yet.');
121
        }
122
123
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
124
125
        $config['logfile'] = $isEnabled;
126
127
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);
128
    }
129
130
    protected function setCollectEnabled(bool $isEnabled): void
131
    {
132
        if (!$this->isInitialized()) {
133
            throw new RuntimeException('System was not initialized yet.');
134
        }
135
136
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
137
138
        $config['collect'] = $isEnabled;
139
140
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);
141
    }
142
143
    protected function setServerApi(string $serverApi): void
144
    {
145
        if (!$this->isInitialized()) {
146
            throw new RuntimeException('System was not initialized yet.');
147
        }
148
149
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
150
151
        $config['serverApi'] = $serverApi;
152
153
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);
154
    }
155
156
    /**
157
     * Return the user identifier.
158
     */
159
    protected function getUserId(): string
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
        return $config['userId'];
168
    }
169
}
170