Passed
Push — master ( d9cb1f...3ce59a )
by Jonas
02:42
created

Configuration::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Glamorous\Boiler\Helpers;
4
5
class Configuration
6
{
7
    /**
8
     * Array with the whole configuration.
9
     *
10
     * @var array
11
     */
12
    private $config = [];
13
14
    /**
15
     * Location of the config file.
16
     *
17
     * @var string
18
     */
19
    private $configLocation = '/.config/boiler';
20
21
    /**
22
     * Filename of the config file.
23
     *
24
     * @var string
25
     */
26
    private $configFile = 'configuration.json';
27
28
    /**
29
     * Full path to the configurationFile.
30
     *
31
     * @var string
32
     */
33
    private $configFileLocation;
34
35
    /**
36
     * Singleton pattern instance.
37
     *
38
     * @var self
39
     */
40
    private static $instance = null;
41
42
    /**
43
     * Configuration constructor.
44
     *
45
     * @SuppressWarnings(Superglobals)
46
     */
47
    private function __construct()
48
    {
49
        $this->configLocation = $_SERVER['HOME'] . $this->configLocation;
50
        $this->configFileLocation = $this->configLocation . '/' . $this->configFile;
51
        $this->setupConfiguration();
52
    }
53
54
    /**
55
     * Get singleton instance of the class.
56
     *
57
     * @return Configuration
58
     */
59
    public static function getInstance()
60
    {
61
        if (self::$instance === null) {
62
            self::$instance = new Configuration();
63
        }
64
65
        return self::$instance;
66
    }
67
68
    private function setupConfiguration()
69
    {
70
        if (!is_dir($this->configLocation)) {
71
            mkdir($this->configLocation, 0777, true);
72
        }
73
74
        if (!file_exists($this->configFileLocation)) {
75
            $this->config = [
76
                'paths' => [],
77
            ];
78
            $this->saveConfigurationFile();
79
        }
80
81
        $this->loadConfiguration();
82
    }
83
84
    public function loadConfiguration()
85
    {
86
        $json = file_get_contents($this->configFileLocation);
87
        $this->config = $json ? json_decode($json, true) : [];
88
    }
89
90
    private function saveConfigurationFile()
91
    {
92
        file_put_contents(
93
            $this->configFileLocation,
94
            json_encode($this->config, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT)
95
        );
96
    }
97
98
    /**
99
     * Add the current path to the configuration.
100
     *
101
     * @param string $path
102
     *
103
     * @return bool
104
     */
105
    public function addPath(string $path): bool
106
    {
107
        if (!is_array($this->config['paths']) || !in_array($path, $this->config['paths'])) {
108
            $this->config['paths'][] = $path;
109
            $this->saveConfigurationFile();
110
111
            return true;
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * Remove the given path from the configuration.
119
     *
120
     * @param string $path
121
     *
122
     * @return bool
123
     */
124
    public function removePath(string $path): bool
125
    {
126
        $arrayKey = array_search($path, $this->config['paths']);
127
        if (is_array($this->config['paths']) && $arrayKey !== false) {
128
            unset($this->config['paths'][$arrayKey]);
129
            $this->saveConfigurationFile();
130
131
            return true;
132
        }
133
134
        return false;
135
    }
136
137
    /**
138
     * Get all paths from the configuration.
139
     *
140
     * @return array
141
     */
142
    public function getPaths(): array
143
    {
144
        if (array_key_exists('paths', $this->config) && is_array($this->config['paths'])) {
145
            return $this->config['paths'];
146
        }
147
148
        return [];
149
    }
150
}
151