Passed
Push — master ( 3cd5ab...f863cd )
by Nils
02:29
created

InventorioCommand::isCollectEnabled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Startwind\Inventorio\Command;
4
5
use Startwind\Inventorio\Config\Config;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputOption;
8
9
abstract class InventorioCommand extends Command
10
{
11
    protected Config $config;
12
13
    protected function configure(): void
14
    {
15
        $this->addOption('configFile', 'c', InputOption::VALUE_OPTIONAL, 'The configuration file', __DIR__ . '/../../config/default.yml');
16
    }
17
18
    /**
19
     * Return the path to the configuration file.
20
     */
21
    protected function getConfigFile(): string
22
    {
23
        return $this->config->getConfigFile();
24
    }
25
26
    protected function initConfiguration(string $configFile): void
27
    {
28
        $this->config = new Config($configFile);
29
    }
30
31
    /**
32
     * Return true if the application is already initialized.
33
     */
34
    protected function isInitialized(): bool
35
    {
36
        $configFile = $this->getConfigFile();
37
        return file_exists($configFile);
38
    }
39
40
    /**
41
     * Return the server identifier.
42
     */
43
    protected function getServerId(): string
44
    {
45
        if (!$this->isInitialized()) {
46
            throw new \RuntimeException('System was not initialized yet.');
47
        }
48
49
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
50
51
        return $config['serverId'];
52
    }
53
54
    /**
55
     * Return true if remote is enabled
56
     */
57
    protected function isRemoteEnabled(): bool
58
    {
59
        if (!$this->isInitialized()) {
60
            throw new \RuntimeException('System was not initialized yet.');
61
        }
62
63
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
64
65
        if (!array_key_exists('remote', $config)) {
66
            return false;
67
        }
68
69
        return $config['remote'];
70
    }
71
72
    protected function isCollectEnabled(): bool
73
    {
74
        if (!$this->isInitialized()) {
75
            throw new \RuntimeException('System was not initialized yet.');
76
        }
77
78
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
79
80
        if (!array_key_exists('collect', $config)) {
81
            return false;
82
        }
83
84
        return $config['collect'];
85
    }
86
87
88
    protected function areLogfilesEnabled(): bool
89
    {
90
        if (!$this->isInitialized()) {
91
            throw new \RuntimeException('System was not initialized yet.');
92
        }
93
94
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
95
96
        if (!array_key_exists('logfile', $config)) {
97
            return false;
98
        }
99
100
        return $config['logfile'];
101
    }
102
103
    protected function setRemoteEnabled(bool $isEnabled): void
104
    {
105
        if (!$this->isInitialized()) {
106
            throw new \RuntimeException('System was not initialized yet.');
107
        }
108
109
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
110
111
        $config['remote'] = $isEnabled;
112
113
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
114
    }
115
116
    protected function setLogfileEnabled(bool $isEnabled): void
117
    {
118
        if (!$this->isInitialized()) {
119
            throw new \RuntimeException('System was not initialized yet.');
120
        }
121
122
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
123
124
        $config['logfile'] = $isEnabled;
125
126
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
127
    }
128
129
    protected function setCollectEnabled(bool $isEnabled): void
130
    {
131
        if (!$this->isInitialized()) {
132
            throw new \RuntimeException('System was not initialized yet.');
133
        }
134
135
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
136
137
        $config['collect'] = $isEnabled;
138
139
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
140
    }
141
142
    /**
143
     * Return the user identifier.
144
     */
145
    protected function getUserId(): string
146
    {
147
        if (!$this->isInitialized()) {
148
            throw new \RuntimeException('System was not initialized yet.');
149
        }
150
151
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
152
153
        return $config['userId'];
154
    }
155
}
156