Completed
Push — master ( adff0c...fcdb68 )
by Vladimir
02:58
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\ConsoleInterface;
6
use allejo\stakx\System\Filesystem;
7
use allejo\stakx\Utilities\ArrayUtilities;
8
use Symfony\Component\Yaml\Exception\ParseException;
9
use Symfony\Component\Yaml\Yaml;
10
11
class Configuration
12
{
13
    const DEFAULT_NAME = "_config.yml";
14
15
    /**
16
     * An array representation of the main Yaml configuration
17
     *
18
     * @var array
19
     */
20
    private $configuration;
21
22
    /**
23
     * @var ConsoleInterface
24
     */
25
    private $output;
26
27
    /**
28
     * @var Filesystem
29
     */
30
    private $fs;
31
32
    /**
33
     * Configuration constructor.
34
     *
35
     * @param string                $configFile
36
     * @param ConsoleInterface|null $output
37
     */
38 15
    public function __construct($configFile, $output = null)
39
    {
40 15
        $this->configuration = array();
41 15
        $this->output        = new ConsoleInterface($output);
42 15
        $this->fs            = new Filesystem();
43
44 15
        if ($this->fs->exists($configFile))
45
        {
46
            try
47
            {
48 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...
49
            }
50
            catch (ParseException $e)
51
            {
52
                $this->output->error("Parsing the configuration failed: {message}", array(
53
                    "message" => $e->getMessage()
54
                ));
55
                $this->output->error("Using default configuration...");
56
            }
57
        }
58
59 15
        $this->defaultConfiguration();
60 15
        $this->handleDeprecations();
61 15
    }
62
63 1
    public function isDebug ()
64
    {
65 1
        return $this->returnConfigOption('debug', false);
66
    }
67
68
    /**
69
     * @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...
70
     *
71
     * @return mixed|null
72
     */
73 2
    public function getBaseUrl ()
74
    {
75 2
        $base = $this->returnConfigOption('base');
76
77 2
        if (is_null($base))
78
        {
79 2
            return $this->returnConfigOption('baseurl');
80
        }
81
82
        return $base;
83
    }
84
85
    /**
86
     * @return string[]
87
     */
88 1
    public function getDataFolders ()
89
    {
90 1
        return $this->returnConfigOption('data');
91
    }
92
93
    /**
94
     * @return string[]
95
     */
96 1
    public function getDataSets ()
97
    {
98 1
        return $this->returnConfigOption('datasets');
99
    }
100
101 1
    public function getIncludes ()
102
    {
103 1
        return $this->returnConfigOption('include', array());
104
    }
105
106 1
    public function getExcludes ()
107
    {
108 1
        return $this->returnConfigOption('exclude', array());
109
    }
110
111 1
    public function getTheme ()
112
    {
113 1
        return $this->returnConfigOption('theme');
114
    }
115
116 1
    public function getConfiguration ()
117
    {
118 1
        return $this->configuration;
119
    }
120
121 1
    public function getPageViewFolders ()
122
    {
123 1
        return $this->returnConfigOption('pageviews');
124
    }
125
126 3
    public function getTargetFolder ()
127
    {
128 3
        return $this->returnConfigOption('target');
129
    }
130
131 1
    public function getCollectionsFolders ()
132
    {
133 1
        return $this->returnConfigOption('collections');
134
    }
135
136 1
    public function getTwigAutoescape ()
137
    {
138 1
        return $this->configuration['twig']['autoescape'];
139
    }
140
141
    /**
142
     * Return the specified configuration option if available, otherwise return the default
143
     *
144
     * @param  string     $name    The configuration option to lookup
145
     * @param  mixed|null $default The default value returned if the configuration option isn't found
146
     *
147
     * @return mixed|null
148
     */
149 15
    private function returnConfigOption ($name, $default = null)
150
    {
151 15
        return (isset($this->configuration[$name]) ? $this->configuration[$name] : $default);
152
    }
153
154 15
    private function defaultConfiguration ()
155
    {
156
        $defaultConfig = array(
157 15
            'baseurl' => '',
158
            'target' => '_site',
159
            'twig' => array(
160
                'autoescape' => false
161
            ),
162
            'include' => array(
163
                '.htaccess'
164
            ),
165
            'exclude' => array(
166
                '/^_.*/',
167
                '*.twig',
168
                'node_modules/',
169
                'stakx-theme.yml'
170
            )
171
        );
172
173 15
        if (is_array($this->configuration))
174
        {
175 15
            $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name');
176
        }
177
        else
178
        {
179 1
            $this->configuration = $defaultConfig;
180
        }
181 15
    }
182
183 15
    private function handleDeprecations ()
184
    {
185
        // @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...
186 15
        $base = $this->returnConfigOption('base');
187
188 15
        if (!is_null($base))
189
        {
190
            $this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0.");
191
        }
192
    }
193
}