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

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 54
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addCommand() 0 8 1
A getInventorioServer() 0 3 1
A getCommands() 0 6 2
A removeCommand() 0 9 1
A __construct() 0 4 1
A getConfigFile() 0 3 1
A storeSettings() 0 3 1
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