Passed
Pull Request — master (#147)
by Simone
02:01
created

Container::ensureResourcesItemIsDefined()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources;
13
14
class Container
15
{
16
    private $resources;
17
18
    private $rewrites;
19
20
    private $allowed = [
21
        'allowed',
22
        'allowedRanges',
23
        'allowedValues',
24
        'defaults',
25
        'mandatory',
26
        'rules',
27
    ];
28
29
    private $globals;
30
31
    private $resourceConstraints;
32
33 32
    public function __construct(array $resources)
34
    {
35 32
        $this->ensureResourcesItemIsDefined($resources);
36
37 31
        $this->rewrites = [];
38 31
        $this->globals  = [];
39
40 31
        $this->ensureValidConstraints($resources);
41
42 30
        foreach ($resources['resources'] as $item) {
43 29
            if (isset($item['rewrite'])) {
44 2
                foreach ($item['rewrite'] as $field => $rewriteRule) {
45 2
                    $this->rewrites[$field] = $rewriteRule;
46
                }
47
            }
48
        }
49
50 30
        if (isset($resources['globals'])) {
51 1
            $this->globals = $resources['globals'];
52
        }
53
54 30
        $this->resources = $resources;
55 30
    }
56
57 31
    private function ensureValidConstraints($resources)
58
    {
59 31
        foreach ($resources['resources'] as $item) {
60 30
            if (isset($item['constraints'])) {
61 30
                foreach ($item['constraints'] as $name => $value) {
62 27
                    if (!in_array($name, $this->allowed)) {
63 1
                        throw new \Sensorario\Resources\Exceptions\InvalidConstraintException(
64
                            'Invalid constraint: '
65 1
                            . 'name ' . $name
66 30
                            . '; value ' . $value
67
                        );
68
                    }
69
                }
70
            }
71
        }
72 30
    }
73
74 2
    public function countResources()
75
    {
76 2
        return count(
77 2
            $this->resources['resources']
78
        );
79
    }
80
81 27
    private function getResourceConstraints($resource)
82
    {
83 27
        if (!$this->resourceConstraints) {
84 27
            $this->resourceConstraints = $this->resources['resources'][$resource]['constraints'];
85
        }
86
87 27
        return $this->resourceConstraints;
88
    }
89
90 2
    public function create($resource, array $constraints)
91
    {
92 2
        foreach ($constraints as $name => $value) {
93
            if (
94 2
                !isset($this->getResourceConstraints($resource)['allowed'])
95 1
                || !in_array($name, $this->getResourceConstraints($resource)['allowed'])
96
            ) {
97 1
                throw new \Sensorario\Resources\Exceptions\NotAllowedConstraintException(
98 1
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
99
                );
100
            }
101
        }
102
103 1
        return true;
104
    }
105
106 25
    public function allowed($resource)
107
    {
108 25
        $allowed = [];
109
110 25
        foreach ($this->allowed as $item) {
111 25
            if (isset($this->getResourceConstraints($resource)[$item])) {
112 24
                $allowed = array_merge(
113 24
                    $allowed,
114 24
                    $this->getResourceConstraints($resource)[$item]
115
                );
116
            }
117
        }
118
119 25
        return $allowed;
120
    }
121
122 22
    public function getConstraints($constraint, $resource) : array
123
    {
124 22
        if (isset($this->getResourceConstraints($resource)[$constraint])) {
125 18
            return $this->getResourceConstraints($resource)[$constraint];
126
        }
127
128 22
        return [];
129
    }
130
131 23
    public function rewrites()
132
    {
133 23
        return $this->rewrites;
134
    }
135
136 22
    public function globals()
137
    {
138 22
        return $this->globals;
139
    }
140
141 22
    public function mandatory($resource)
142
    {
143 22
        return $this->getConstraints(
144 22
            'mandatory',
145 22
            $resource
146
        );
147
    }
148
149 22
    public function defaults($resource)
150
    {
151 22
        return $this->getConstraints(
152 22
            'defaults',
153 22
            $resource
154
        );
155
    }
156
157 22
    public function rules($resource)
158
    {
159 22
        return $this->getConstraints(
160 22
            'rules',
161 22
            $resource
162
        );
163
    }
164
165 22
    public function allowedValues($resource)
166
    {
167 22
        return $this->getConstraints(
168 22
            'allowedValues',
169 22
            $resource
170
        );
171
    }
172
173 22
    public function ranges($resource)
174
    {
175 22
        return $this->getConstraints(
176 22
            'allowedRanges',
177 22
            $resource
178
        );
179
    }
180
181 32
    public function ensureResourcesItemIsDefined($resources)
182
    {
183 32
        if (!isset($resources['resources'])) {
184 1
            throw new \Sensorario\Resources\Exceptions\EmptyConfigurationException(
185 1
                'resources element is not defined'
186
            );
187
        }
188 31
    }
189
}
190