Passed
Push — master ( 1a588d...65a263 )
by Nils
02:38
created

InventorioCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
c 3
b 0
f 0
dl 0
loc 101
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initConfiguration() 0 3 1
A configure() 0 3 1
A isInitialized() 0 4 1
A getServerId() 0 9 2
A getConfigFile() 0 3 1
A isRemoteEnabled() 0 13 3
A setRemoteEnabled() 0 11 2
A setLogfileEnabled() 0 11 2
A getUserId() 0 9 2
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 setRemoteEnabled(bool $isEnabled): void
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
        $config['remote'] = $isEnabled;
81
82
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
83
    }
84
85
    protected function setLogfileEnabled(bool $isEnabled): void
86
    {
87
        if (!$this->isInitialized()) {
88
            throw new \RuntimeException('System was not initialized yet.');
89
        }
90
91
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
92
93
        $config['logfile'] = $isEnabled;
94
95
        file_put_contents($this->getConfigFile(), json_encode($config), JSON_PRETTY_PRINT);;
96
    }
97
98
    /**
99
     * Return the user identifier.
100
     */
101
    protected function getUserId(): string
102
    {
103
        if (!$this->isInitialized()) {
104
            throw new \RuntimeException('System was not initialized yet.');
105
        }
106
107
        $config = json_decode(file_get_contents($this->getConfigFile()), true);
108
109
        return $config['userId'];
110
    }
111
}
112