Completed
Push — master ( 3442f8...f4ff57 )
by Vladimir
02:32
created

Configuration   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
dl 0
loc 207
ccs 56
cts 62
cp 0.9032
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 5

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parseConfiguration() 0 20 3
A setLogger() 0 4 1
A isDebug() 0 4 1
A getBaseUrl() 0 12 3
A getDataFolders() 0 4 1
A getDataSets() 0 4 1
A getIncludes() 0 4 1
A getExcludes() 0 4 1
A getTheme() 0 4 1
A getConfiguration() 0 4 1
A getPageViewFolders() 0 4 1
A getTargetFolder() 0 4 1
A getCollectionsFolders() 0 4 1
A getTwigAutoescape() 0 4 1
A getRedirectTemplate() 0 4 1
A returnConfigOption() 0 4 2
A defaultConfiguration() 0 23 1
A handleDeprecations() 0 10 2
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx;
9
10
use allejo\stakx\System\Filesystem;
11
use allejo\stakx\Utilities\ArrayUtilities;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\Yaml\Exception\ParseException;
15
use Symfony\Component\Yaml\Yaml;
16
17
class Configuration implements LoggerAwareInterface
18
{
19
    const DEFAULT_NAME = '_config.yml';
20
21
    /**
22
     * A list of regular expressions or files directly related to stakx websites that should not be copied over to the
23
     * compiled website as an asset.
24
     *
25
     * @var array
26
     */
27
    public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/');
28
29
    /**
30
     * An array representation of the main Yaml configuration.
31
     *
32
     * @var array
33
     */
34
    private $configuration;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $output;
40
41
    /**
42
     * @var Filesystem
43
     */
44
    private $fs;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
45
46
    /**
47
     * Configuration constructor.
48
     */
49 30
    public function __construct()
50
    {
51 30
        $this->configuration = array();
52 30
        $this->fs = new Filesystem();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53 30
    }
54
55
    /**
56
     * Parse a given configuration file and configure this Configuration instance.
57
     *
58
     * This function should be called with 'null' passed when "configuration-less" mode is used
59
     *
60
     * @param string|null $configFile The path to the configuration file. If null, the default configuration will be
61
     *                                used
62
     */
63 30
    public function parseConfiguration($configFile = null)
64
    {
65 30
        if ($this->fs->exists($configFile))
66
        {
67
            try
68
            {
69 2
                $this->configuration = Yaml::parse(file_get_contents($configFile));
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\..._contents($configFile)) can also be of type string or object<stdClass>. However, the property $configuration is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
70
            }
71
            catch (ParseException $e)
72
            {
73
                $this->output->error('Parsing the configuration failed: {message}', array(
74
                    'message' => $e->getMessage(),
75
                ));
76
                $this->output->error('Using default configuration...');
77
            }
78
        }
79
80 30
        $this->defaultConfiguration();
81 30
        $this->handleDeprecations();
82 30
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 17
    public function setLogger(LoggerInterface $logger)
88
    {
89 17
        $this->output = $logger;
90 17
    }
91
92 14
    public function isDebug()
93
    {
94 14
        return $this->returnConfigOption('debug', false);
95
    }
96
97
    /**
98
     * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl'
99
     *
100
     * @return mixed|null
101
     */
102 4
    public function getBaseUrl()
103
    {
104 4
        $base = $this->returnConfigOption('base');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105 4
        $baseUrl = $this->returnConfigOption('baseurl');
106
107 4
        if (is_null($base) || (!empty($baseUrl)))
108
        {
109 3
            return $baseUrl;
110
        }
111
112 1
        return $base;
113
    }
114
115
    /**
116
     * @return string[]
117
     */
118 1
    public function getDataFolders()
119
    {
120 1
        return $this->returnConfigOption('data');
121
    }
122
123
    /**
124
     * @return string[]
125
     */
126 1
    public function getDataSets()
127
    {
128 1
        return $this->returnConfigOption('datasets');
129
    }
130
131 1
    public function getIncludes()
132
    {
133 1
        return $this->returnConfigOption('include', array());
134
    }
135
136 1
    public function getExcludes()
137
    {
138 1
        return $this->returnConfigOption('exclude', array());
139
    }
140
141 14
    public function getTheme()
142
    {
143 14
        return $this->returnConfigOption('theme');
144
    }
145
146 1
    public function getConfiguration()
147
    {
148 1
        return $this->configuration;
149
    }
150
151 1
    public function getPageViewFolders()
152
    {
153 1
        return $this->returnConfigOption('pageviews');
154
    }
155
156 3
    public function getTargetFolder()
157
    {
158 3
        return $this->returnConfigOption('target');
159
    }
160
161 1
    public function getCollectionsFolders()
162
    {
163 1
        return $this->returnConfigOption('collections');
164
    }
165
166 14
    public function getTwigAutoescape()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
167
    {
168 14
        return $this->configuration['twig']['autoescape'];
169
    }
170
171
    public function getRedirectTemplate()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
172
    {
173
        return $this->configuration['templates']['redirect'];
174
    }
175
176
    /**
177
     * Return the specified configuration option if available, otherwise return the default.
178
     *
179
     * @param string     $name    The configuration option to lookup
180
     * @param mixed|null $default The default value returned if the configuration option isn't found
181
     *
182
     * @return mixed|null
183
     */
184 30
    private function returnConfigOption($name, $default = null)
185
    {
186 30
        return isset($this->configuration[$name]) ? $this->configuration[$name] : $default;
187
    }
188
189 30
    private function defaultConfiguration()
190
    {
191
        $defaultConfig = array(
192 30
            'baseurl'   => '',
193 30
            'target'    => '_site',
194
            'twig'      => array(
195
                'autoescape' => false,
196
            ),
197
            'include'   => array(
198
                '.htaccess',
199
            ),
200
            'exclude'   => array(
201 30
                'node_modules/',
202 30
                'stakx-theme.yml',
203 30
                self::DEFAULT_NAME,
204
            ),
205
            'templates' => array(
206
                'redirect' => false,
207
            ),
208
        );
209
210 30
        $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name');
211 30
    }
212
213 30
    private function handleDeprecations()
214
    {
215
        // @TODO 1.0.0 handle 'base' deprecation in _config.yml
216 30
        $base = $this->returnConfigOption('base');
217
218 30
        if (!is_null($base))
219
        {
220 2
            $this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0.");
221
        }
222 30
    }
223
}
224