Configuration   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 73
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createModules() 0 12 3
A createConfigContainer() 0 11 2
A getConfigContainer() 0 4 1
A getModules() 0 4 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea Module
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
 * @copyright 2017-2018 LibreWorks contributors
20
 * @license   Apache-2.0
21
 */
22
namespace Caridea\Module;
23
24
use Caridea\Container\Properties;
25
26
/**
27
 * A bootstrapper for reading in module and configuration info.
28
 *
29
 * Once constructed, this object is immutable.
30
 */
31
class Configuration
32
{
33
    /**
34
     * @var array<Module>  Instantiated modules
35
     */
36
    protected $modules;
37
    /**
38
     * @var Properties  The config container
39
     */
40
    protected $config;
41
42
    /**
43
     * Creates a new Configuration.
44
     *
45
     * This constructor expects an array full of class names in the `$modules`
46
     * parameter. Each class name *must* extend `Caridea\Module\Module` or an
47
     * `UnexpectedValueException` will be thrown.
48
     *
49
     * @param array<string> $modules  An array of module class names
50
     * @param array<string,mixed> $config  The system configuration
51
     * @throws \UnexpectedValueException if a module class doesn't extend `Caridea\Module\Module`
52
     */
53 2
    public function __construct(array $modules, array $config)
54
    {
55 2
        $this->modules = $this->createModules($modules);
56 1
        $this->config = $this->createConfigContainer($config);
57 1
    }
58
59 2
    private function createModules(array $sysModules)
60
    {
61 2
        $modules = [];
62 2
        foreach ($sysModules as $className) {
63 2
            if (!is_a($className, Module::class, true)) {
64 1
                throw new \UnexpectedValueException("Not a module class: '$className'");
65
            } else {
66 1
                $modules[] = new $className();
67
            }
68
        }
69 1
        return $modules;
70
    }
71
72 1
    private function createConfigContainer(array $config): Properties
73
    {
74 1
        $sysConfig = [];
75
        // first set module defaults
76 1
        foreach ($this->modules as $module) {
77 1
            $sysConfig = array_merge($sysConfig, $module->getConfig());
78
        }
79
        // then bring in user-specified values
80 1
        $sysConfig = array_merge($sysConfig, $config);
81 1
        return new Properties($sysConfig);
82
    }
83
84
    /**
85
     * Gets the configuration settings container.
86
     *
87
     * @return \Caridea\Container\Properties  The config container
88
     */
89 1
    public function getConfigContainer(): Properties
90
    {
91 1
        return $this->config;
92
    }
93
94
    /**
95
     * Gets the loaded modules.
96
     *
97
     * @return array<\Caridea\Module\Module> The loaded modules
98
     */
99 1
    public function getModules(): array
100
    {
101 1
        return $this->modules;
102
    }
103
}
104