Completed
Push — master ( 359eec...f60dea )
by Vladimir
15:35
created

Configuration   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 213
ccs 65
cts 75
cp 0.8667
rs 10
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 11 2
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
B defaultConfiguration() 0 30 2
A handleDeprecations() 0 10 2
1
<?php
2
3
namespace allejo\stakx\Object;
4
5
use allejo\stakx\System\Filesystem;
6
use allejo\stakx\Utilities\ArrayUtilities;
7
use Psr\Log\LoggerAwareInterface;
8
use Psr\Log\LoggerInterface;
9
use Symfony\Component\Yaml\Exception\ParseException;
10
use Symfony\Component\Yaml\Yaml;
11
12
class Configuration implements LoggerAwareInterface
13
{
14
    const DEFAULT_NAME = "_config.yml";
15
16
    /**
17
     * A list of regular expressions or files directly related to stakx websites that should not be copied over to the
18
     * compiled website as an asset.
19
     *
20
     * @var array
21
     */
22
    public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/');
23
24
    /**
25
     * An array representation of the main Yaml configuration
26
     *
27
     * @var array
28
     */
29
    private $configuration;
30
31
    /**
32
     * @var LoggerInterface
33
     */
34
    private $output;
35
36
    /**
37
     * @var Filesystem
38
     */
39
    private $fs;
40
41
    /**
42
     * Configuration constructor.
43
     */
44 16
    public function __construct()
45
    {
46 16
        $this->configuration = array();
47 16
        $this->fs            = new Filesystem();
48 16
    }
49
50
    /**
51
     * Parse a given configuration file and configure this Configuration instance.
52
     *
53
     * This function should be called with 'null' passed when "configuration-less" mode is used
54
     *
55
     * @param string|null $configFile The path to the configuration file. If null, the default configuration will be
56
     *                                used
57
     */
58 16
    public function parseConfiguration ($configFile = null)
59
    {
60 16
        if ($this->fs->exists($configFile))
61 16
        {
62
            try
63
            {
64 16
                $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...
65
            }
66 16
            catch (ParseException $e)
67
            {
68
                $this->output->error("Parsing the configuration failed: {message}", array(
69
                    "message" => $e->getMessage()
70
                ));
71
                $this->output->error("Using default configuration...");
72
            }
73 16
        }
74
75 16
        $this->defaultConfiguration();
76 16
        $this->handleDeprecations();
77 16
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 15
    public function setLogger(LoggerInterface $logger)
83
    {
84 15
        $this->output = $logger;
85 15
    }
86
87 2
    public function isDebug ()
88
    {
89 2
        return $this->returnConfigOption('debug', false);
90
    }
91
92
    /**
93
     * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl'
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
94
     *
95
     * @return mixed|null
96
     */
97 2
    public function getBaseUrl ()
98
    {
99 2
        $base = $this->returnConfigOption('base');
100
101 2
        if (is_null($base))
102 2
        {
103 2
            return $this->returnConfigOption('baseurl');
104
        }
105
106
        return $base;
107
    }
108
109
    /**
110
     * @return string[]
111
     */
112 1
    public function getDataFolders ()
113
    {
114 1
        return $this->returnConfigOption('data');
115
    }
116
117
    /**
118
     * @return string[]
119
     */
120 1
    public function getDataSets ()
121
    {
122 1
        return $this->returnConfigOption('datasets');
123
    }
124
125 1
    public function getIncludes ()
126
    {
127 1
        return $this->returnConfigOption('include', array());
128
    }
129
130 1
    public function getExcludes ()
131
    {
132 1
        return $this->returnConfigOption('exclude', array());
133
    }
134
135 2
    public function getTheme ()
136
    {
137 2
        return $this->returnConfigOption('theme');
138
    }
139
140 1
    public function getConfiguration ()
141
    {
142 1
        return $this->configuration;
143
    }
144
145 1
    public function getPageViewFolders ()
146
    {
147 1
        return $this->returnConfigOption('pageviews');
148
    }
149
150 3
    public function getTargetFolder ()
151
    {
152 3
        return $this->returnConfigOption('target');
153
    }
154
155 1
    public function getCollectionsFolders ()
156
    {
157 1
        return $this->returnConfigOption('collections');
158
    }
159
160 2
    public function getTwigAutoescape ()
161
    {
162 2
        return $this->configuration['twig']['autoescape'];
163
    }
164
165
    public function getRedirectTemplate ()
166
    {
167
        return $this->configuration['templates']['redirect'];
168
    }
169
170
    /**
171
     * Return the specified configuration option if available, otherwise return the default
172
     *
173
     * @param  string     $name    The configuration option to lookup
174
     * @param  mixed|null $default The default value returned if the configuration option isn't found
175
     *
176
     * @return mixed|null
177
     */
178 16
    private function returnConfigOption ($name, $default = null)
179
    {
180 16
        return (isset($this->configuration[$name]) ? $this->configuration[$name] : $default);
181
    }
182
183 16
    private function defaultConfiguration ()
184
    {
185
        $defaultConfig = array(
186 16
            'baseurl' => '',
187 16
            'target' => '_site',
188
            'twig' => array(
189
                'autoescape' => false
190 16
            ),
191
            'include' => array(
192
                '.htaccess'
193 16
            ),
194
            'exclude' => array(
195 16
                'node_modules/',
196 16
                'stakx-theme.yml',
197
                self::DEFAULT_NAME
198 16
            ),
199
            'templates' => array(
200
                'redirect' => false
201 16
            )
202 16
        );
203
204 16
        if (is_array($this->configuration))
205 16
        {
206 16
            $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name');
207 16
        }
208
        else
209
        {
210
            $this->configuration = $defaultConfig;
211
        }
212 16
    }
213
214 16
    private function handleDeprecations ()
215
    {
216
        // @TODO 1.0.0 handle 'base' deprecation in _config.yml
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
217 16
        $base = $this->returnConfigOption('base');
218
219 16
        if (!is_null($base))
220 16
        {
221
            $this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0.");
222
        }
223
    }
224
}