Passed
Push — master ( 926a22...db3e5d )
by Thorsten
01:38
created

ConfigProviderParams::checkLoader()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.3183

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 10
cts 16
cp 0.625
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 2
crap 6.3183
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/config project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\Config;
12
13
final class ConfigProviderParams implements ConfigProviderParamsInterface
14
{
15
    private $params;
16
17 11
    public function __construct(array $params)
18
    {
19 11
        $this->params = $this->verifyParams($params);
20 5
    }
21
22 8
    public function hasScope(string $scope): bool
23
    {
24 8
        return isset($this->params[$scope]);
25
    }
26
27 5
    public function getLoader(string $scope): ConfigLoaderInterface
28
    {
29 5
        $this->assertScopeExists($scope);
30 5
        $loader = $this->params[$scope]['loader'];
31 5
        if (!is_object($loader)) {
32 5
            $this->params[$scope]['loader'] = new $loader;
33
        }
34 5
        return $this->params[$scope]['loader'];
35
    }
36
37 5
    public function getLocations(string $scope): array
38
    {
39 5
        $this->assertScopeExists($scope);
40 5
        return $this->params[$scope]['locations'];
41
    }
42
43 5
    public function getSources(string $scope): array
44
    {
45 5
        $this->assertScopeExists($scope);
46 5
        return $this->params[$scope]['sources'];
47
    }
48
49 11
    private function verifyParams(array $params): array
50
    {
51 11
        if (empty($params)) {
52 1
            throw new \Exception('Given params may not be empty.');
53
        }
54 10
        foreach ($params as $scope => $scopeParams) {
55 10
            if (isset($scopeParams['locations'])) {
56 3
                $this->checkLocations($scope, $scopeParams);
57
            } else {
58 7
                $params[$scope]['locations'] = [];
59
            }
60 9
            $this->checkSources($scope, $scopeParams);
61 7
            $this->checkLoader($scope, $scopeParams);
62
        }
63 5
        return $params;
64
    }
65
66 3
    private function checkLocations(string $scope, array $scopeParams)
67
    {
68 3
        if (!is_array($scopeParams['locations'])) {
69 1
            throw new \Exception(sprintf('The "locations" param within scope: "%s" must be an array', $scope));
70
        }
71 2
    }
72
73 9
    private function checkSources(string $scope, array $params)
74
    {
75 9
        if (!isset($params['sources'])) {
76 1
            throw new \Exception(sprintf('Missing required key "sources" within scope: "%s"', $scope));
77
        }
78 8
        if (!is_array($params['sources'])) {
79 1
            throw new \Exception(sprintf('The "sources" param within scope: "%s" must be an array', $scope));
80
        }
81 7
    }
82
83 7
    private function checkLoader(string $scope, array $params)
84
    {
85 7
        if (!isset($params['loader'])) {
86 1
            throw new \Exception(sprintf('Missing required key "loader" within scope: "%s"', $scope));
87
        }
88 6
        if (!is_string($params['loader'])) {
89
            throw new \Exception(sprintf('The "loader" param within scope: "%s" must be a string(fqcn)', $scope));
90
        }
91 6
        if (!class_exists($params['loader'])) {
92 1
            throw new \Exception(
93 1
                sprintf('Configured loader: "%s" for scope: "%s" can not be found.', $params['loader'], $scope)
94
            );
95
        }
96 5
        $implementedInterfaces = class_implements($params['loader']);
97 5
        if (!in_array(ConfigLoaderInterface::class, $implementedInterfaces)) {
98
            throw new \Exception(sprintf(
99
                'Configured loader: "%s" for scope: "%s" does not implement required interface: %s',
100
                $params['loader'],
101
                $scope,
102
                ConfigLoaderInterface::class
103
            ));
104
        }
105 5
    }
106
107 7
    private function assertScopeExists(string $scope)
108
    {
109 7
        if (!$this->hasScope($scope)) {
110
            throw new \Exception(sprintf('Given scope: "%s" has not been registered.', $scope));
111
        }
112 7
    }
113
}
114