Completed
Push — rework ( 381fae...5b8955 )
by Markus
02:46
created

Config::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace SSpkS;
4
5
use \Symfony\Component\Yaml\Yaml;
6
use \Symfony\Component\Yaml\Exception\ParseException;
7
8
/**
9
 * Configuration class
10
 *
11
 * @property array $site Site properties
12
 * @property array $paths Different paths
13
 * @property array excludedSynoServices Synology services to exclude from package list
14
 * @property string basePath Path to site root (where index.php is located)
15
 * @property string baseUrl URL to site root (where index.php is located)
16
 */
17
class Config implements \Iterator
18
{
19
    private $iterPos;
20
    private $basePath;
21
    private $cfgFile;
22
    private $config;
23
24 4
    public function __construct($basePath, $cfgFile = 'conf/sspks.yaml')
25
    {
26 4
        $this->iterPos  = 0;
27 4
        $this->basePath = $basePath;
28 4
        $this->cfgFile  = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile;
29
30 4
        if (!file_exists($this->cfgFile)) {
31 1
            throw new \Exception('Config file "' . $this->cfgFile . '" not found!');
32
        }
33
34
        try {
35
            /** @var array $config */
36 3
            $config = Yaml::parse(file_get_contents($this->cfgFile));
37 3
        } catch (ParseException $e) {
38 1
            throw new \Exception($e->getMessage());
39
        }
40
41 2
        $this->config = $config;
42 2
        $this->config['basePath'] = $this->basePath;
43 2
    }
44
45
    /**
46
     * Getter magic method.
47
     *
48
     * @param string $name Name of requested value.
49
     * @return mixed Requested value.
50
     */
51 2
    public function __get($name)
52
    {
53 2
        return $this->config[$name];
54
    }
55
56
    /**
57
     * Setter magic method.
58
     *
59
     * @param string $name Name of variable to set.
60
     * @param mixed $value Value to set.
61
     */
62 1
    public function __set($name, $value)
63
    {
64 1
        $this->config[$name] = $value;
65 1
    }
66
67
    /**
68
     * Isset feature magic method.
69
     *
70
     * @param string $name Name of requested value.
71
     * @return bool TRUE if value exists, FALSE otherwise.
72
     */
73 1
    public function __isset($name)
74
    {
75 1
        return isset($this->config[$name]);
76
    }
77
78
    /**
79
     * Unset feature magic method.
80
     *
81
     * @param string $name Name of value to unset.
82
     */
83 1
    public function __unset($name)
84
    {
85 1
        unset($this->config[$name]);
86 1
    }
87
88 1
    public function rewind()
89
    {
90 1
        $this->iterPos = 0;
91 1
    }
92
93 1
    public function current()
94
    {
95 1
        return $this->config[array_keys($this->config)[$this->iterPos]];
96
    }
97
98
    public function key()
99
    {
100
        return array_keys($this->config)[$this->iterPos];
101
    }
102
103 1
    public function next()
104
    {
105 1
        $this->iterPos++;
106 1
    }
107
108 1
    public function valid()
109
    {
110 1
        return isset(array_keys($this->config)[$this->iterPos]);
111
    }
112
}
113