Completed
Push — develop ( 927119...79656c )
by Julien
05:10 queued 02:36
created

Config::__construct()   F

Complexity

Conditions 13
Paths 1026

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 13

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 17
c 2
b 0
f 0
nc 1026
nop 2
dl 0
loc 28
ccs 13
cts 13
cp 1
crap 13
rs 2.7716

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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