Passed
Push — master ( 78a1e9...1a588d )
by Nils
03:05
created

Config::getConfigFile()   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
eloc 1
nc 1
nop 0
dl 0
loc 3
c 1
b 0
f 0
cc 1
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(): array
27
    {
28
        if (array_key_exists('commands', $this->settingsArray)) {
29
            return $this->settingsArray['commands'];
30
        } else {
31
            return [];
32
        }
33
    }
34
35
    public function addCommand(string $identifier, string $command, string $name): void
36
    {
37
        $commands = $this->getCommands();
38
        $commands[$identifier] = ['name' => $name, 'command' => $command];
39
40
        $this->settingsArray['commands'] = $commands;
41
42
        $this->storeSettings();
43
    }
44
45
    public function removeCommand(string $identifier): void
46
    {
47
        $commands = $this->getCommands();
48
49
        unset($commands[$identifier]);
50
51
        $this->settingsArray['commands'] = $commands;
52
53
        $this->storeSettings();
54
    }
55
56
    public function getLogfiles(): array
57
    {
58
        if (array_key_exists('logfiles', $this->settingsArray)) {
59
            return $this->settingsArray['logfiles'];
60
        } else {
61
            return [];
62
        }
63
    }
64
65
    public function addLogfile(string $logfile, string $name): void
66
    {
67
        $logfiles = $this->getLogfiles();
68
        $logfiles[] = ['file' => $logfile, 'name' => $name];
69
70
        $this->settingsArray['logfiles'] = $logfiles;
71
72
        $this->storeSettings();
73
    }
74
75
    public function removeLogfile(string $logfile): void
76
    {
77
        $logfiles = $this->getLogfiles();
78
79
        foreach($logfiles as $key => $logfileObject) {
80
            if($logfile === $logfileObject['file']) {
81
             unset($logfiles[$key]);
82
            }
83
        }
84
85
        $this->settingsArray['logfiles'] = $logfiles;
86
87
        $this->storeSettings();
88
    }
89
90
    private function storeSettings(): void
91
    {
92
        file_put_contents($this->getConfigFile(), json_encode($this->settingsArray));
93
    }
94
95
    public function getConfigFile(): string
96
    {
97
        return getenv("HOME") . DIRECTORY_SEPARATOR . $this->configArray['inventorio']['configFile'];
98
    }
99
}
100