Completed
Push — rework ( d09de3...810325 )
by Markus
03:02
created

Config::__unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
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 3
cts 3
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
 */
15
class Config implements \Iterator
16
{
17
    private $iterPos;
18
    private $basePath;
19
    private $cfgFile;
20
    private $config;
21
22 3
    public function __construct($basePath, $cfgFile = 'conf/sspks.yaml')
23
    {
24 3
        $this->iterPos  = 0;
25 3
        $this->basePath = $basePath;
26 3
        $this->cfgFile  = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile;
27
28 3
        if (!file_exists($this->cfgFile)) {
29 1
            throw new \Exception('Config file "' . $this->cfgFile . '" not found!');
30
        }
31
32
        try {
33
            /** @var array $config */
34 2
            $config = Yaml::parse(file_get_contents($this->cfgFile));
35 2
        } catch (ParseException $e) {
36 1
            throw new \Exception($e->getMessage());
37
        }
38
39 1
        $this->config = $config;
40 1
        $this->config['basePath'] = $this->basePath;
41
    }
42
43
    /**
44
     * Getter magic method.
45
     *
46
     * @param string $name Name of requested value.
47
     * @return mixed Requested value.
48 1
     */
49
    public function __get($name)
50 1
    {
51
        return $this->config[$name];
52
    }
53
54
    /**
55
     * Setter magic method.
56
     *
57
     * @param string $name Name of variable to set.
58
     * @param mixed $value Value to set.
59
     */
60
    public function __set($name, $value)
61
    {
62
        $this->config[$name] = $value;
63
    }
64 1
65
    /**
66 1
     * Isset feature magic method.
67 1
     *
68
     * @param string $name Name of requested value.
69 1
     * @return bool TRUE if value exists, FALSE otherwise.
70
     */
71 1
    public function __isset($name)
72
    {
73
        return isset($this->config[$name]);
74 1
    }
75
76
    /**
77 1
     * Unset feature magic method.
78
     *
79 1
     * @param string $name Name of value to unset.
80
     */
81 1
    public function __unset($name)
82 1
    {
83
        unset($this->config[$name]);
84 1
    }
85
86 1
    public function rewind()
87
    {
88
        $this->iterPos = 0;
89
    }
90
91
    public function current()
92
    {
93
        return $this->config[array_keys($this->config)[$this->iterPos]];
94
    }
95
96
    public function key()
97
    {
98
        return array_keys($this->config)[$this->iterPos];
99
    }
100
101
    public function next()
102
    {
103
        $this->iterPos++;
104
    }
105
106
    public function valid()
107
    {
108
        return isset(array_keys($this->config)[$this->iterPos]);
109
    }
110
}
111