Test Failed
Pull Request — master (#147)
by Simone
01:55
created

Container::countResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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 15
    public function __construct(array $resources)
34
    {
35 15
        $this->ensureResourcesItemIsDefined();
0 ignored issues
show
Bug introduced by
The call to ensureResourcesItemIsDefined() misses a required argument $resources.

This check looks for function calls that miss required arguments.

Loading history...
36
37
        $this->rewrites = [];
38
        $this->globals  = [];
39
40
        $this->ensureValidConstraints($resources);
41
42
        foreach ($resources['resources'] as $item) {
43
            if (isset($item['rewrite'])) {
44
                foreach ($item['rewrite'] as $field => $rewriteRule) {
45
                    $this->rewrites[$field] = $rewriteRule;
46
                }
47
            }
48
        }
49
50
        if (isset($resources['globals'])) {
51
            $this->globals = $resources['globals'];
52
        }
53
54
        $this->resources = $resources;
55
    }
56
57
    private function ensureValidConstraints($resources)
58
    {
59
        foreach ($resources['resources'] as $item) {
60
            if (isset($item['constraints'])) {
61
                foreach ($item['constraints'] as $name => $value) {
62
                    if (!in_array($name, $this->allowed)) {
63
                        throw new \Sensorario\Resources\Exceptions\InvalidConstraintException(
64
                            'Invalid constraint: '
65
                            . 'name ' . $name
66
                            . '; value ' . $value
67
                        );
68
                    }
69
                }
70
            }
71
        }
72
    }
73
74
    public function countResources()
75
    {
76
        return count(
77
            $this->resources['resources']
78
        );
79
    }
80
81
    private function getResourceConstraints($resource)
82
    {
83
        if (!$this->resourceConstraints) {
84
            $this->resourceConstraints = $this->resources['resources'][$resource]['constraints'];
85
        }
86
87
        return $this->resourceConstraints;
88
    }
89
90
    public function create($resource, array $constraints)
91
    {
92
        foreach ($constraints as $name => $value) {
93
            if (
94
                !isset($this->getResourceConstraints($resource)['allowed'])
95
                || !in_array($name, $this->getResourceConstraints($resource)['allowed'])
96
            ) {
97
                throw new \Sensorario\Resources\Exceptions\NotAllowedConstraintException(
98
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
99
                );
100
            }
101
        }
102
103
        return true;
104
    }
105
106
    public function allowed($resource)
107
    {
108
        $allowed = [];
109
110
        foreach ($this->allowed as $item) {
111
            if (isset($this->getResourceConstraints($resource)[$item])) {
112
                $allowed = array_merge(
113
                    $allowed,
114
                    $this->getResourceConstraints($resource)[$item]
115
                );
116
            }
117
        }
118
119
        return $allowed;
120
    }
121
122
    public function getConstraints($constraint, $resource) : array
123
    {
124
        if (isset($this->getResourceConstraints($resource)[$constraint])) {
125
            return $this->getResourceConstraints($resource)[$constraint];
126
        }
127
128
        return [];
129
    }
130
131
    public function rewrites()
132
    {
133
        return $this->rewrites;
134
    }
135
136
    public function globals()
137
    {
138
        return $this->globals;
139
    }
140
141
    public function mandatory($resource)
142
    {
143
        return $this->getConstraints(
144
            'mandatory',
145
            $resource
146
        );
147
    }
148
149
    public function defaults($resource)
150
    {
151
        return $this->getConstraints(
152
            'defaults',
153
            $resource
154
        );
155
    }
156
157
    public function rules($resource)
158
    {
159
        return $this->getConstraints(
160
            'rules',
161
            $resource
162
        );
163
    }
164
165
    public function allowedValues($resource)
166
    {
167
        return $this->getConstraints(
168
            'allowedValues',
169
            $resource
170
        );
171
    }
172
173
    public function ranges($resource)
174
    {
175
        return $this->getConstraints(
176
            'allowedRanges',
177
            $resource
178
        );
179
    }
180
181
    public function ensureResourcesItemIsDefined($resources)
182
    {
183
        if (!isset($resources['resources'])) {
184
            throw new \Sensorario\Resources\Exceptions\EmptyConfigurationException(
185
                'resources element is not defined'
186
            );
187
        }
188
    }
189
}
190