Passed
Push — master ( 842060...a5ad86 )
by Julien
02:09
created

Config::envVarIsNotEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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