Config::__construct()   F
last analyzed

Complexity

Conditions 14
Paths 2050

Size

Total Lines 68
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 20.0854

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 35
c 2
b 0
f 0
nc 2050
nop 2
dl 0
loc 68
ccs 24
cts 35
cp 0.6857
crap 20.0854
rs 2.1

How to fix   Long Method    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;
0 ignored issues
show
Bug introduced by
The type \Symfony\Component\Yaml\Yaml was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use \Symfony\Component\Yaml\Exception\ParseException;
0 ignored issues
show
Bug introduced by
The type \Symfony\Component\Yaml\Exception\ParseException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 30
    public function __construct($basePath, $cfgFile = 'conf/sspks.yaml')
29
    {
30 30
        $this->iterPos  = 0;
31 30
        $this->basePath = $basePath;
32 30
        $this->cfgFile  = $this->basePath . DIRECTORY_SEPARATOR . $cfgFile;
33
34 30
        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 29
            $config = Yaml::parse(file_get_contents($this->cfgFile));
41 1
        } catch (ParseException $e) {
42 1
            throw new \Exception($e->getMessage());
43
        }
44
        
45
        /** Init variables that are not actual config variables */
46 28
        $config['SSPKS_COMMIT'] = '';
47 28
        $config['SSPKS_BRANCH'] = '';
48
        
49
        /** Override config values with environment variables if present */
50 28
        if ($this->envVarIsNotEmpty('SSPKS_COMMIT')) {
51
            $config['SSPKS_COMMIT'] = $_ENV['SSPKS_COMMIT'];
52
        }
53
        
54 28
        if ($this->envVarIsNotEmpty('SSPKS_BRANCH')) {
55
            $config['SSPKS_BRANCH'] = $_ENV['SSPKS_BRANCH'];
56
        }
57
        
58 28
        if ($this->envVarIsNotEmpty('SSPKS_SITE_NAME')) {
59
            $config['site']['name'] = $_ENV['SSPKS_SITE_NAME'];
60
        }
61
        
62 28
        if ($this->envVarIsNotEmpty('SSPKS_SITE_THEME')) {
63
            $config['site']['theme'] = $_ENV['SSPKS_SITE_THEME'];
64
        }
65
        
66 28
        if ($this->envVarIsNotEmpty('SSPKS_SITE_REDIRECTINDEX')) {
67
            $config['site']['redirectindex'] = $_ENV['SSPKS_SITE_REDIRECTINDEX'];
68
        }
69
        
70 28
        if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_FILE_MASK')) {
71
            $config['packages']['file_mask'] = $_ENV['SSPKS_PACKAGES_FILE_MASK'];
72
        }
73
74 28
        if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_MAINTAINER')) {
75
            $config['packages']['maintainer'] = $_ENV['SSPKS_PACKAGES_MAINTAINER'];
76
        }
77
78 28
        if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_MAINTAINER_URL')) {
79
            $config['packages']['maintainer_url'] = $_ENV['SSPKS_PACKAGES_MAINTAINER_URL'];
80
        }
81
82 28
        if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_DISTRIBUTOR')) {
83
            $config['packages']['distributor'] = $_ENV['SSPKS_PACKAGES_DISTRIBUTOR'];
84
        }
85
86 28
        if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_DISTRIBUTOR_URL')) {
87
            $config['packages']['distributor_url'] = $_ENV['SSPKS_PACKAGES_DISTRIBUTOR_URL'];
88
        }
89
90 28
        if ($this->envVarIsNotEmpty('SSPKS_PACKAGES_SUPPORT_URL')) {
91
            $config['packages']['support_url'] = $_ENV['SSPKS_PACKAGES_SUPPORT_URL'];
92
        }
93
94 28
        $this->config = $config;
95 28
        $this->config['basePath'] = $this->basePath;
96 28
    }
97
    
98
    /**
99
     * Checks wether an env variable exists and is not an empty string.
100
     *
101
     * @param string $name Name of requested environment variable.
102
     * @return boolean value.
103
     */
104 28
    public function envVarIsNotEmpty($name)
105
    {
106 28
        return (array_key_exists($name, $_ENV) && $_ENV[$name]);
107
    }
108
109
    /**
110
     * Getter magic method.
111
     *
112
     * @param string $name Name of requested value.
113
     * @return mixed Requested value.
114
     */
115 27
    public function __get($name)
116
    {
117 27
        return $this->config[$name];
118
    }
119
120
    /**
121
     * Setter magic method.
122
     *
123
     * @param string $name Name of variable to set.
124
     * @param mixed $value Value to set.
125
     */
126 27
    public function __set($name, $value)
127
    {
128 27
        $this->config[$name] = $value;
129 27
    }
130
131
    /**
132
     * Isset feature magic method.
133
     *
134
     * @param string $name Name of requested value.
135
     * @return bool TRUE if value exists, FALSE otherwise.
136
     */
137 1
    public function __isset($name)
138
    {
139 1
        return isset($this->config[$name]);
140
    }
141
142
    /**
143
     * Unset feature magic method.
144
     *
145
     * @param string $name Name of value to unset.
146
     */
147 1
    public function __unset($name)
148
    {
149 1
        unset($this->config[$name]);
150 1
    }
151
152 1
    public function rewind()
153
    {
154 1
        $this->iterPos = 0;
155 1
    }
156
157 1
    public function current()
158
    {
159 1
        return $this->config[array_keys($this->config)[$this->iterPos]];
160
    }
161
162 1
    public function key()
163
    {
164 1
        return array_keys($this->config)[$this->iterPos];
165
    }
166
167 1
    public function next()
168
    {
169 1
        $this->iterPos++;
170 1
    }
171
172 1
    public function valid()
173
    {
174 1
        return isset(array_keys($this->config)[$this->iterPos]);
175
    }
176
}
177