Config::getCommands()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Config;
4
5
use RuntimeException;
6
use Symfony\Component\Yaml\Yaml;
7
8
class Config
9
{
10
    private array $configArray = [];
11
    private array $settingsArray = [];
12
13
    public function __construct(string $configFile)
14
    {
15
        $this->configArray = Yaml::parse(file_get_contents($configFile));
16
17
        if (file_exists($this->getConfigFile())) {
18
            $this->settingsArray = json_decode(file_get_contents($this->getConfigFile()), true);
19
        }
20
    }
21
22
    public function getInventorioServer(): string
23
    {
24
        if (array_key_exists('serverApi', $this->settingsArray)) {
25
            return $this->settingsArray['serverApi'];
26
        }
27
        return $this->configArray['inventorio']['server'];
28
    }
29
30
    public function getCommands($fromUserConfig = true): array
31
    {
32
        if ($fromUserConfig) {
33
            if (array_key_exists('commands', $this->settingsArray)) {
34
                return $this->settingsArray['commands'];
35
            } else {
36
                return [];
37
            }
38
        } else {
39
            return $this->configArray['commands'];
40
        }
41
    }
42
43
    public function getSecret(): string
44
    {
45
        if (array_key_exists('secret', $this->settingsArray)) {
46
            return $this->settingsArray['secret'];
47
        } else {
48
            return '';
49
        }
50
    }
51
52
    public function addCommand(string $identifier, string $command, string $name): void
53
    {
54
        $commands = $this->getCommands();
55
        $commands[$identifier] = ['name' => $name, 'command' => $command];
56
57
        $this->settingsArray['commands'] = $commands;
58
59
        $this->storeSettings();
60
    }
61
62
    public function removeCommand(string $identifier): void
63
    {
64
        $commands = $this->getCommands();
65
66
        unset($commands[$identifier]);
67
68
        $this->settingsArray['commands'] = $commands;
69
70
        $this->storeSettings();
71
    }
72
73
    public function getLogfiles(): array
74
    {
75
        if (array_key_exists('logfiles', $this->settingsArray)) {
76
            return $this->settingsArray['logfiles'];
77
        } else {
78
            return [];
79
        }
80
    }
81
82
    public function addLogfile(string $logfile, string $name): void
83
    {
84
        $logfiles = $this->getLogfiles();
85
86
        foreach ($logfiles as $existingLogfile) {
87
            if ($existingLogfile['name'] == $name) {
88
                throw new RuntimeException('Name is already used');
89
            }
90
91
            if ($existingLogfile['file'] == $logfile) {
92
                throw new RuntimeException('Logfile is already used');
93
            }
94
        }
95
96
        $logfiles[] = ['file' => $logfile, 'name' => $name];
97
98
99
        $this->settingsArray['logfiles'] = $logfiles;
100
101
        $this->storeSettings();
102
    }
103
104
    public function removeLogfile(string $logfile): void
105
    {
106
        $logfiles = $this->getLogfiles();
107
108
        foreach ($logfiles as $key => $logfileObject) {
109
            if ($logfile === $logfileObject['file']) {
110
                unset($logfiles[$key]);
111
            }
112
        }
113
114
        $this->settingsArray['logfiles'] = $logfiles;
115
116
        $this->storeSettings();
117
    }
118
119
    private function storeSettings(): void
120
    {
121
        file_put_contents($this->getConfigFile(), json_encode($this->settingsArray));
122
    }
123
124
    public function getConfigFile(): string
125
    {
126
        return getenv("HOME") . DIRECTORY_SEPARATOR . $this->configArray['inventorio']['configFile'];
127
    }
128
}
129