Test Failed
Pull Request — master (#41)
by Julien
02:33
created

Config   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 104
ccs 34
cts 34
cp 1
rs 10
wmc 22
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
F __construct() 0 28 13
A __get() 0 4 1
A __set() 0 4 1
A __isset() 0 4 1
A __unset() 0 4 1
A rewind() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A valid() 0 4 1
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
        /** Override config values with environment variables if present */
44 27
        $config['SSPKS_COMMIT'] = (array_key_exists('SSPKS_COMMIT', $_ENV) && $_ENV['SSPKS_COMMIT'])?$_ENV['SSPKS_COMMIT']:NULL;
45 27
        $config['SSPKS_BRANCH'] = (array_key_exists('SSPKS_BRANCH', $_ENV) && $_ENV['SSPKS_BRANCH'])?$_ENV['SSPKS_BRANCH']:NULL;
46
        
47
        $config['site']['name'] = (array_key_exists('SSPKS_SITE_NAME', $_ENV) && $_ENV['SSPKS_SITE_NAME'])?$_ENV['SSPKS_SITE_NAME']:$config['site']['name'];
48
        $config['site']['theme'] = (array_key_exists('SSPKS_SITE_THEME', $_ENV) && $_ENV['SSPKS_SITE_THEME'])?$_ENV['SSPKS_SITE_THEME']:$config['site']['theme'];
49
        $config['site']['redirectindex'] = (array_key_exists('SSPKS_SITE_REDIRECTINDEX', $_ENV) && $_ENV['SSPKS_SITE_REDIRECTINDEX'])?$_ENV['SSPKS_SITE_REDIRECTINDEX']:$config['site']['redirectindex'];
50
51
        $this->config = $config;
52
        $this->config['basePath'] = $this->basePath;
53 26
    }
54
55 26
    /**
56
     * Getter magic method.
57
     *
58
     * @param string $name Name of requested value.
59
     * @return mixed Requested value.
60
     */
61
    public function __get($name)
62
    {
63
        return $this->config[$name];
64 26
    }
65
66 26
    /**
67 26
     * Setter magic method.
68
     *
69
     * @param string $name Name of variable to set.
70
     * @param mixed $value Value to set.
71
     */
72
    public function __set($name, $value)
73
    {
74
        $this->config[$name] = $value;
75 2
    }
76
77 2
    /**
78
     * Isset feature magic method.
79
     *
80
     * @param string $name Name of requested value.
81
     * @return bool TRUE if value exists, FALSE otherwise.
82
     */
83
    public function __isset($name)
84
    {
85 1
        return isset($this->config[$name]);
86
    }
87 1
88 1
    /**
89
     * Unset feature magic method.
90 1
     *
91
     * @param string $name Name of value to unset.
92 1
     */
93 1
    public function __unset($name)
94
    {
95 1
        unset($this->config[$name]);
96
    }
97 1
98
    public function rewind()
99
    {
100 1
        $this->iterPos = 0;
101
    }
102 1
103
    public function current()
104
    {
105 1
        return $this->config[array_keys($this->config)[$this->iterPos]];
106
    }
107 1
108 1
    public function key()
109
    {
110 1
        return array_keys($this->config)[$this->iterPos];
111
    }
112 1
113
    public function next()
114
    {
115
        $this->iterPos++;
116
    }
117
118
    public function valid()
119
    {
120
        return isset(array_keys($this->config)[$this->iterPos]);
121
    }
122
}
123