Passed
Push — master ( 65a263...816e51 )
by Nils
02:44
created

InventorioCommand::getUserId()   A

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 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 areLogfilesEnabled(): 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('logfile', $config)) {
81
            return false;
82
        }
83
84
        return $config['logfile'];
85
    }
86
87
    protected function setRemoteEnabled(bool $isEnabled): void
88
    {
89
        if (!$this->isInitialized()) {
90
            throw new \RuntimeException('System was not initialized yet.');
91
        }
92
93
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
94
95
        $config['remote'] = $isEnabled;
96
97
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
98
    }
99
100
    protected function setLogfileEnabled(bool $isEnabled): void
101
    {
102
        if (!$this->isInitialized()) {
103
            throw new \RuntimeException('System was not initialized yet.');
104
        }
105
106
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
107
108
        $config['logfile'] = $isEnabled;
109
110
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
111
    }
112
113
    /**
114
     * Return the user identifier.
115
     */
116
    protected function getUserId(): string
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
        return $config['userId'];
125
    }
126
}
127