Completed
Push — master ( 287228...c124e1 )
by Vladimir
02:23
created

Configuration::defaultConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 1
rs 9.0856
c 1
b 0
f 0
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 18
    public function __construct()
45
    {
46 18
        $this->configuration = array();
47 18
        $this->fs            = new Filesystem();
48 18
    }
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 18
    public function parseConfiguration ($configFile = null)
59
    {
60 18
        if ($this->fs->exists($configFile))
61 18
        {
62
            try
63
            {
64 18
                $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 18
            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 18
        }
74
75 18
        $this->defaultConfiguration();
76 18
        $this->handleDeprecations();
77 18
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 17
    public function setLogger(LoggerInterface $logger)
83
    {
84 17
        $this->output = $logger;
85 17
    }
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 4
    public function getBaseUrl ()
98
    {
99 4
        $base = $this->returnConfigOption('base');
100 4
        $baseUrl = $this->returnConfigOption('baseurl');
101
102 4
        if (is_null($base) || (!empty($baseUrl)))
103 4
        {
104 3
            return $baseUrl;
105
        }
106
107 1
        return $base;
108
    }
109
110
    /**
111
     * @return string[]
112
     */
113 1
    public function getDataFolders ()
114
    {
115 1
        return $this->returnConfigOption('data');
116
    }
117
118
    /**
119
     * @return string[]
120
     */
121 1
    public function getDataSets ()
122
    {
123 1
        return $this->returnConfigOption('datasets');
124
    }
125
126 1
    public function getIncludes ()
127
    {
128 1
        return $this->returnConfigOption('include', array());
129
    }
130
131 1
    public function getExcludes ()
132
    {
133 1
        return $this->returnConfigOption('exclude', array());
134
    }
135
136 2
    public function getTheme ()
137
    {
138 2
        return $this->returnConfigOption('theme');
139
    }
140
141 1
    public function getConfiguration ()
142
    {
143 1
        return $this->configuration;
144
    }
145
146 1
    public function getPageViewFolders ()
147
    {
148 1
        return $this->returnConfigOption('pageviews');
149
    }
150
151 3
    public function getTargetFolder ()
152
    {
153 3
        return $this->returnConfigOption('target');
154
    }
155
156 1
    public function getCollectionsFolders ()
157
    {
158 1
        return $this->returnConfigOption('collections');
159
    }
160
161 2
    public function getTwigAutoescape ()
162
    {
163 2
        return $this->configuration['twig']['autoescape'];
164
    }
165
166
    public function getRedirectTemplate ()
167
    {
168
        return $this->configuration['templates']['redirect'];
169
    }
170
171
    /**
172
     * Return the specified configuration option if available, otherwise return the default
173
     *
174
     * @param  string     $name    The configuration option to lookup
175
     * @param  mixed|null $default The default value returned if the configuration option isn't found
176
     *
177
     * @return mixed|null
178
     */
179 18
    private function returnConfigOption ($name, $default = null)
180
    {
181 18
        return (isset($this->configuration[$name]) ? $this->configuration[$name] : $default);
182
    }
183
184 18
    private function defaultConfiguration ()
185
    {
186
        $defaultConfig = array(
187 18
            'baseurl' => '',
188 18
            'target' => '_site',
189
            'twig' => array(
190
                'autoescape' => false
191 18
            ),
192
            'include' => array(
193
                '.htaccess'
194 18
            ),
195
            'exclude' => array(
196 18
                'node_modules/',
197 18
                'stakx-theme.yml',
198
                self::DEFAULT_NAME
199 18
            ),
200
            'templates' => array(
201
                'redirect' => false
202 18
            )
203 18
        );
204
205 18
        $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name');
206 18
    }
207
208 18
    private function handleDeprecations ()
209
    {
210
        // @TODO 1.0.0 handle 'base' deprecation in _config.yml
211 18
        $base = $this->returnConfigOption('base');
212
213 18
        if (!is_null($base))
214 18
        {
215 2
            $this->output->warning("The 'base' configuration option has been replaced by 'baseurl' and will be removed in in version 1.0.0.");
216 2
        }
217
    }
218
}