Passed
Push — master ( aed423...cd0ae5 )
by Nils
02:40
created

Config::removeCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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