Passed
Push — master ( b85596...79672d )
by Thorsten
01:52
created

ConfigProviderParams::assertScopeExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
use Assert\Assertion;
14
15
final class ConfigProviderParams implements ConfigProviderParamsInterface
16
{
17
    private $params;
18
19 13
    public function __construct(array $params)
20
    {
21 13
        $this->params = $this->verifyParams($params);
22 7
    }
23
24 10
    public function hasScope(string $scope): bool
25
    {
26 10
        return isset($this->params[$scope]);
27
    }
28
29 7
    public function getLoader(string $scope): ConfigLoaderInterface
30
    {
31 7
        $this->assertScopeExists($scope);
32 7
        $loader = $this->params[$scope]['loader'];
33 7
        if (!is_object($loader)) {
34 6
            $this->params[$scope]['loader'] = new $loader;
35
        }
36 7
        return $this->params[$scope]['loader'];
37
    }
38
39 7
    public function getLocations(string $scope): array
40
    {
41 7
        $this->assertScopeExists($scope);
42 7
        return $this->params[$scope]['locations'];
43
    }
44
45 7
    public function getSources(string $scope): array
46
    {
47 7
        $this->assertScopeExists($scope);
48 7
        return $this->params[$scope]['sources'];
49
    }
50
51 13
    private function verifyParams(array $params): array
52
    {
53 13
        Assertion::notEmpty($params, 'Given params may not be empty.');
54 12
        foreach ($params as $scope => $scopeParams) {
55 12
            if (isset($scopeParams['locations'])) {
56 4
                $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 4
    private function checkLocations(string $scope, array $scopeParams)
67
    {
68 4
        Assertion::isArray(
69 4
            $scopeParams['locations'],
70 4
            sprintf('The "locations" param within scope: "%s" must be an array', $scope)
71
        );
72 3
    }
73
74 11
    private function checkSources(string $scope, array $params)
75
    {
76 11
        Assertion::keyIsset($params, 'sources', sprintf('Missing required key "sources" within scope: "%s"', $scope));
77 10
        Assertion::isArray(
78 10
            $params['sources'],
79 10
            sprintf('The "sources" param within scope: "%s" must be an array', $scope)
80
        );
81 9
    }
82
83 9
    private function checkLoader(string $scope, array $params)
84
    {
85 9
        Assertion::keyIsset($params, 'loader', sprintf('Missing required key "loader" within scope: "%s"', $scope));
86 8
        if ($params['loader'] instanceof ConfigLoaderInterface) {
87 2
            return;
88
        }
89 7
        Assertion::string(
90 7
            $params['loader'],
91 7
            sprintf('The "loader" param within scope: "%s" must be a string(fqcn)', $scope)
92
        );
93 7
        Assertion::classExists(
94 7
            $params['loader'],
95 7
            sprintf('Configured loader: "%s" for scope: "%s" can not be found.', $params['loader'], $scope)
96
        );
97 6
        Assertion::implementsInterface(
98 6
            $params['loader'],
99 6
            ConfigLoaderInterface::class,
100 6
            sprintf(
101 6
                'Configured loader: "%s" for scope: "%s" does not implement required interface: %s',
102 6
                $params['loader'],
103 6
                $scope,
104 6
                ConfigLoaderInterface::class
105
            )
106
        );
107 6
    }
108
109 9
    private function assertScopeExists(string $scope)
110
    {
111 9
        Assertion::true($this->hasScope($scope), sprintf('Given scope: "%s" has not been registered.', $scope));
112 9
    }
113
}
114