Completed
Push — master ( 6739a2...818625 )
by Simone
02:47
created

Container::getResourceConstraints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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