Passed
Push — master ( 527ce7...dff99b )
by Nils
02:47
created

Config::storeSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 mixed $settingsArray;
11
12
    public function __construct(string $configFile)
13
    {
14
        $this->configArray = Yaml::parse(file_get_contents($configFile));
15
        $this->settingsArray = json_decode(file_get_contents($this->getConfigFile()), true);
16
    }
17
18
    public function getInventorioServer(): string
19
    {
20
        return $this->configArray['inventorio']['server'];
21
    }
22
23
    public function getCommands(): array
24
    {
25
        if (array_key_exists('commands', $this->settingsArray)) {
26
            return $this->settingsArray['commands'];
27
        } else {
28
            return [];
29
        }
30
    }
31
32
    public function addCommand(string $identifier, string $command, string $name): void
33
    {
34
        $commands = $this->getCommands();
35
        $commands[$identifier] = ['name' => $name, 'command' => $command];
36
37
        $this->settingsArray['commands'] = $commands;
38
39
        $this->storeSettings();
40
    }
41
42
    public function removeCommand(string $identifier): void
43
    {
44
        $commands = $this->getCommands();
45
46
        unset($commands[$identifier]);
47
48
        $this->settingsArray['commands'] = $commands;
49
50
        $this->storeSettings();
51
    }
52
53
    private function storeSettings(): void
54
    {
55
        file_put_contents($this->getConfigFile(), json_encode($this->settingsArray));
56
    }
57
58
    public function getConfigFile(): string
59
    {
60
        return getenv("HOME") . DIRECTORY_SEPARATOR . $this->configArray['inventorio']['configFile'];
61
    }
62
}
63