Passed
Push — master ( 686045...01d1c1 )
by Thorsten
01:37
created

ConfigProviderParams::checkLoader()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.583

Importance

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