Test Failed
Push — develop ( e23c35...f11935 )
by Julien
04:34 queued 02:20
created

Config::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
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 array packages Defaults for packages
15
 * @property string basePath Path to site root (where index.php is located)
16
 * @property string baseUrl URL to site root (where index.php is located)
17
 * @property string baseUrlRelative Relative URL to site root (without scheme or hostname)
18
 */
19
class Config implements \Iterator
20
{
21
    private $iterPos;
22
    private $basePath;
23
    private $cfgFile;
24
    private $config;
25
26 29
    public function __construct($basePath, $cfgFile = 'conf/sspks.yaml')
27
    {
28 29
        $this->iterPos  = 0;
29 29
        $this->basePath = $basePath;
30 29
        $this->cfgFile  = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile;
31
32 29
        if (!file_exists($this->cfgFile)) {
33 1
            throw new \Exception('Config file "' . $this->cfgFile . '" not found!');
34
        }
35
36
        try {
37
            /** @var array $config */
38 28
            $config = Yaml::parse(file_get_contents($this->cfgFile));
39 28
        } catch (ParseException $e) {
40 1
            throw new \Exception($e->getMessage());
41
        }
42
        
43 27
        /** Init variables that are not actual config variables */
44 27
        $config['SSPKS_COMMIT'] = '';
45 27
        $config['SSPKS_BRANCH'] = '';
46
        
47
        /** Override config values with environment variables if present */
48
        if (envVarIsNotEmpty('SSPKS_COMMIT')){
49
            $config['SSPKS_COMMIT'] = $_ENV['SSPKS_COMMIT'];
50
        }
51
        
52
        if (envVarIsNotEmpty('SSPKS_BRANCH')){
53 26
            $config['SSPKS_BRANCH'] = $_ENV['SSPKS_BRANCH'];
54
        }
55 26
        
56
        if (envVarIsNotEmpty('SSPKS_SITE_NAME')){
57
            $config['site']['name'] = $_ENV['SSPKS_SITE_NAME'];
58
        }
59
        
60
        if (envVarIsNotEmpty('SSPKS_SITE_THEME')){
61
            $config['site']['theme'] = $_ENV['SSPKS_SITE_THEME'];
62
        }
63
        
64 26
        if (envVarIsNotEmpty('SSPKS_SITE_REDIRECTINDEX')){
65
            $config['site']['redirectindex'] = $_ENV['SSPKS_SITE_REDIRECTINDEX'];
66 26
        }
67 26
68
        $this->config = $config;
69
        $this->config['basePath'] = $this->basePath;
70
    }
71
    
72
    /**
73
     * checks wether an env variable exists and is not an empty string.
74
     *
75 2
     * @param string $name Name of requested environment variable.
76
     * @return boolean value.
77 2
     */
78
    public function envVarIsNotEmpty($name)
79
    {
80
        return (array_key_exists($name, $_ENV) && $_ENV[$name]);
81
    }
82
83
    /**
84
     * Getter magic method.
85 1
     *
86
     * @param string $name Name of requested value.
87 1
     * @return mixed Requested value.
88 1
     */
89
    public function __get($name)
90 1
    {
91
        return $this->config[$name];
92 1
    }
93 1
94
    /**
95 1
     * Setter magic method.
96
     *
97 1
     * @param string $name Name of variable to set.
98
     * @param mixed $value Value to set.
99
     */
100 1
    public function __set($name, $value)
101
    {
102 1
        $this->config[$name] = $value;
103
    }
104
105 1
    /**
106
     * Isset feature magic method.
107 1
     *
108 1
     * @param string $name Name of requested value.
109
     * @return bool TRUE if value exists, FALSE otherwise.
110 1
     */
111
    public function __isset($name)
112 1
    {
113
        return isset($this->config[$name]);
114
    }
115
116
    /**
117
     * Unset feature magic method.
118
     *
119
     * @param string $name Name of value to unset.
120
     */
121
    public function __unset($name)
122
    {
123
        unset($this->config[$name]);
124
    }
125
126
    public function rewind()
127
    {
128
        $this->iterPos = 0;
129
    }
130
131
    public function current()
132
    {
133
        return $this->config[array_keys($this->config)[$this->iterPos]];
134
    }
135
136
    public function key()
137
    {
138
        return array_keys($this->config)[$this->iterPos];
139
    }
140
141
    public function next()
142
    {
143
        $this->iterPos++;
144
    }
145
146
    public function valid()
147
    {
148
        return isset(array_keys($this->config)[$this->iterPos]);
149
    }
150
}
151