Completed
Push — master ( fcdb68...d209bc )
by Vladimir
02:46
created

Configuration   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 45.21%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 190
rs 10
c 3
b 0
f 0
ccs 33
cts 73
cp 0.4521
wmc 24
lcom 1
cbo 5

18 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 returnConfigOption() 0 4 2
B defaultConfiguration() 0 28 2
A handleDeprecations() 0 10 2
1
<?php
2
3
namespace allejo\stakx\Object;
4
5
use allejo\stakx\Core\StakxLogger;
6
use allejo\stakx\System\Filesystem;
7
use allejo\stakx\Utilities\ArrayUtilities;
8
use Psr\Log\LoggerAwareInterface;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Component\Yaml\Exception\ParseException;
11
use Symfony\Component\Yaml\Yaml;
12
13
class Configuration implements LoggerAwareInterface
14
{
15
    const DEFAULT_NAME = "_config.yml";
16
17
    /**
18
     * An array representation of the main Yaml configuration
19
     *
20
     * @var array
21
     */
22
    private $configuration;
23
24
    /**
25
     * @var StakxLogger
26
     */
27
    private $output;
28
29
    /**
30
     * @var Filesystem
31
     */
32
    private $fs;
33
34
    /**
35
     * Configuration constructor.
36
     */
37 15
    public function __construct()
38
    {
39 15
        $this->configuration = array();
40 15
        $this->fs            = new Filesystem();
41 15
    }
42
43
    public function parseConfiguration ($configFile)
44
    {
45
        if ($this->fs->exists($configFile))
46
        {
47
            try
48
            {
49
                $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...
50
            }
51
            catch (ParseException $e)
52
            {
53
                $this->output->error("Parsing the configuration failed: {message}", array(
54
                    "message" => $e->getMessage()
55
                ));
56
                $this->output->error("Using default configuration...");
57
            }
58
        }
59
60
        $this->defaultConfiguration();
61
        $this->handleDeprecations();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function setLogger(LoggerInterface $logger)
68
    {
69
        $this->output = $logger;
0 ignored issues
show
Documentation Bug introduced by
$logger is of type object<Psr\Log\LoggerInterface>, but the property $output was declared to be of type object<allejo\stakx\Core\StakxLogger>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
70
    }
71
72 1
    public function isDebug ()
73
    {
74 1
        return $this->returnConfigOption('debug', false);
75
    }
76
77
    /**
78
     * @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...
79
     *
80
     * @return mixed|null
81
     */
82 2
    public function getBaseUrl ()
83
    {
84 2
        $base = $this->returnConfigOption('base');
85
86 2
        if (is_null($base))
87 2
        {
88 2
            return $this->returnConfigOption('baseurl');
89
        }
90
91
        return $base;
92
    }
93
94
    /**
95
     * @return string[]
96
     */
97 1
    public function getDataFolders ()
98
    {
99 1
        return $this->returnConfigOption('data');
100
    }
101
102
    /**
103
     * @return string[]
104
     */
105 1
    public function getDataSets ()
106
    {
107 1
        return $this->returnConfigOption('datasets');
108
    }
109
110 1
    public function getIncludes ()
111
    {
112 1
        return $this->returnConfigOption('include', array());
113
    }
114
115 1
    public function getExcludes ()
116
    {
117 1
        return $this->returnConfigOption('exclude', array());
118
    }
119
120 1
    public function getTheme ()
121
    {
122 1
        return $this->returnConfigOption('theme');
123
    }
124
125 1
    public function getConfiguration ()
126
    {
127 1
        return $this->configuration;
128
    }
129
130 1
    public function getPageViewFolders ()
131
    {
132 1
        return $this->returnConfigOption('pageviews');
133
    }
134
135 3
    public function getTargetFolder ()
136
    {
137 3
        return $this->returnConfigOption('target');
138
    }
139
140 1
    public function getCollectionsFolders ()
141
    {
142 1
        return $this->returnConfigOption('collections');
143
    }
144
145 1
    public function getTwigAutoescape ()
146
    {
147 1
        return $this->configuration['twig']['autoescape'];
148
    }
149
150
    /**
151
     * Return the specified configuration option if available, otherwise return the default
152
     *
153
     * @param  string     $name    The configuration option to lookup
154
     * @param  mixed|null $default The default value returned if the configuration option isn't found
155
     *
156
     * @return mixed|null
157
     */
158 13
    private function returnConfigOption ($name, $default = null)
159
    {
160 13
        return (isset($this->configuration[$name]) ? $this->configuration[$name] : $default);
161
    }
162
163
    private function defaultConfiguration ()
164
    {
165
        $defaultConfig = array(
166
            'baseurl' => '',
167
            'target' => '_site',
168
            'twig' => array(
169
                'autoescape' => false
170
            ),
171
            'include' => array(
172
                '.htaccess'
173
            ),
174
            'exclude' => array(
175
                '/^_.*/',
176
                '*.twig',
177
                'node_modules/',
178
                'stakx-theme.yml'
179
            )
180
        );
181
182
        if (is_array($this->configuration))
183
        {
184
            $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name');
185
        }
186
        else
187
        {
188
            $this->configuration = $defaultConfig;
189
        }
190
    }
191
192
    private function handleDeprecations ()
193
    {
194
        // @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...
195
        $base = $this->returnConfigOption('base');
196
197
        if (!is_null($base))
198
        {
199
            $this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0.");
200
        }
201
    }
202
}