Configuration   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createModules() 0 15 4
A createConfigContainer() 0 11 2
A getConfigContainer() 0 4 1
A getModules() 0 4 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur;
22
23
use Caridea\Container\Properties;
24
25
/**
26
 * A bootstrapper for reading in module and configuration info.
27
 *
28
 * This class expects a `Traversable` full of class names in the
29
 * `system.modules` configuration setting. Each class name *must* extend
30
 * `Minotaur\Module` or an `UnexpectedValueException` will be thrown.
31
 */
32
class Configuration
33
{
34
    /**
35
     * @var array<Module> Instantiated modules
36
     */
37
    protected $modules;
38
    /**
39
     * @var Properties The config container
40
     */
41
    protected $config;
42
43
    /**
44
     * Creates a new Configuration.
45
     *
46
     * This constructor expects a `Traversable` full of class names in the
47
     * `system.modules` configuration setting. Each class name *must* extend
48
     * `Minotaur\Module` or an `UnexpectedValueException` will be thrown.
49
     *
50
     * @param array $config The system configuration
51
     * @throws \UnexpectedValueException if a module class doesn't extend `Minotaur\Module`
52
     */
53
    public function __construct(array $config)
54
    {
55
        $this->modules = $this->createModules($config);
56
        $this->config = $this->createConfigContainer($config);
57
    }
58
59
    private function createModules(array $config)
60
    {
61
        $modules = [];
62
        $sysModules = $config['system.modules'] ?? null;
63
        if (is_iterable($sysModules)) {
64
            foreach ($sysModules as $className) {
65
                if (!is_a($className, \Minotaur\Module::class, true)) {
66
                    throw new \UnexpectedValueException("Not a module class: '$className'");
67
                } else {
68
                    $modules[] = new $className();
69
                }
70
            }
71
        }
72
        return $modules;
73
    }
74
75
    private function createConfigContainer(array $config): Properties
76
    {
77
        $sysConfig = [];
78
        // first set module defaults
79
        foreach ($this->modules as $module) {
80
            $sysConfig = array_merge($sysConfig, $module->getConfig());
81
        }
82
        // then bring in user-specified values
83
        $sysConfig = array_merge($sysConfig, $config);
84
        return new Properties($sysConfig);
85
    }
86
87
    /**
88
     * Gets the configuration settings container.
89
     *
90
     * @return - The config container
0 ignored issues
show
Documentation introduced by
The doc-type - could not be parsed: Unknown type name "-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
91
     */
92
    public function getConfigContainer(): Properties
93
    {
94
        return $this->config;
95
    }
96
97
    /**
98
     * Gets the loaded modules.
99
     *
100
     * @return array<Module> The loaded modules
101
     */
102
    public function getModules(): array
103
    {
104
        return $this->modules;
105
    }
106
}
107