Completed
Pull Request — master (#18)
by Vladimir
02:19
created

Configuration::getPageViewFolders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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
    /**
44
     * Parse a given configuration file and configure this Configuration instance.
45
     *
46
     * This function should be called with 'null' passed when "configuration-less" mode is used
47
     *
48
     * @param string|null $configFile The path to the configuration file. If null, the default configuration will be
49
     *                                used
50
     */
51 15
    public function parseConfiguration ($configFile = null)
52
    {
53 15
        if ($this->fs->exists($configFile))
54 15
        {
55
            try
56
            {
57 15
                $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...
58
            }
59 15
            catch (ParseException $e)
60
            {
61
                $this->output->error("Parsing the configuration failed: {message}", array(
62
                    "message" => $e->getMessage()
63
                ));
64
                $this->output->error("Using default configuration...");
65
            }
66 15
        }
67
68 15
        $this->defaultConfiguration();
69 15
        $this->handleDeprecations();
70 15
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 15
    public function setLogger(LoggerInterface $logger)
76 1
    {
77 15
        $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...
78 15
    }
79
80 1
    public function isDebug ()
81
    {
82 1
        return $this->returnConfigOption('debug', false);
83
    }
84
85
    /**
86
     * @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...
87
     *
88
     * @return mixed|null
89
     */
90 2
    public function getBaseUrl ()
91
    {
92 2
        $base = $this->returnConfigOption('base');
93
94 2
        if (is_null($base))
95 2
        {
96 2
            return $this->returnConfigOption('baseurl');
97
        }
98
99
        return $base;
100
    }
101
102
    /**
103
     * @return string[]
104
     */
105 1
    public function getDataFolders ()
106
    {
107 1
        return $this->returnConfigOption('data');
108
    }
109
110
    /**
111
     * @return string[]
112
     */
113 1
    public function getDataSets ()
114
    {
115 1
        return $this->returnConfigOption('datasets');
116
    }
117
118 1
    public function getIncludes ()
119
    {
120 1
        return $this->returnConfigOption('include', array());
121
    }
122
123 1
    public function getExcludes ()
124
    {
125 1
        return $this->returnConfigOption('exclude', array());
126
    }
127
128 1
    public function getTheme ()
129
    {
130 1
        return $this->returnConfigOption('theme');
131
    }
132
133 1
    public function getConfiguration ()
134
    {
135 1
        return $this->configuration;
136
    }
137
138 1
    public function getPageViewFolders ()
139
    {
140 1
        return $this->returnConfigOption('pageviews');
141
    }
142
143 3
    public function getTargetFolder ()
144
    {
145 3
        return $this->returnConfigOption('target');
146
    }
147
148 1
    public function getCollectionsFolders ()
149
    {
150 1
        return $this->returnConfigOption('collections');
151
    }
152
153 1
    public function getTwigAutoescape ()
154
    {
155 1
        return $this->configuration['twig']['autoescape'];
156
    }
157
158
    public function getRedirectTemplate ()
159
    {
160
        return $this->configuration['templates']['redirect'];
161
    }
162
163
    /**
164
     * Return the specified configuration option if available, otherwise return the default
165
     *
166
     * @param  string     $name    The configuration option to lookup
167
     * @param  mixed|null $default The default value returned if the configuration option isn't found
168
     *
169
     * @return mixed|null
170
     */
171 15
    private function returnConfigOption ($name, $default = null)
172
    {
173 15
        return (isset($this->configuration[$name]) ? $this->configuration[$name] : $default);
174
    }
175
176 15
    private function defaultConfiguration ()
177
    {
178
        $defaultConfig = array(
179 15
            'baseurl' => '',
180 15
            'target' => '_site',
181
            'twig' => array(
182
                'autoescape' => false
183 15
            ),
184
            'include' => array(
185
                '.htaccess'
186 15
            ),
187
            'exclude' => array(
188 15
                '/^_(?!themes).*/',
189 15
                '/.twig$/',
190 15
                'node_modules/',
191
                'stakx-theme.yml'
192 15
            ),
193
            'templates' => array(
194
                'redirect' => false
195 15
            )
196 15
        );
197
198 15
        if (is_array($this->configuration))
199 15
        {
200 15
            $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name');
201 15
        }
202
        else
203
        {
204
            $this->configuration = $defaultConfig;
205
        }
206 15
    }
207
208 15
    private function handleDeprecations ()
209
    {
210
        // @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...
211 15
        $base = $this->returnConfigOption('base');
212
213 15
        if (!is_null($base))
214 15
        {
215
            $this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0.");
216
        }
217
    }
218
}