|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sensorario\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use \RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
class Container |
|
8
|
|
|
{ |
|
9
|
|
|
private $resources; |
|
10
|
|
|
|
|
11
|
|
|
private $allowed = [ |
|
12
|
|
|
'allowed', |
|
13
|
|
|
'mandatory', |
|
14
|
|
|
'defaults', |
|
15
|
|
|
]; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(array $resources) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!isset($resources['resources'])) { |
|
20
|
|
|
throw new RuntimeException( |
|
21
|
|
|
'' |
|
22
|
|
|
); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
foreach ($resources['resources'] as $item) { |
|
26
|
|
|
if (isset($item['constraints'])) { |
|
27
|
|
|
foreach ($item['constraints'] as $name => $value) { |
|
28
|
|
|
if (!in_array($name, $this->allowed)) { |
|
29
|
|
|
throw new RuntimeException( |
|
30
|
|
|
'Invalid constraint' |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->resources = $resources; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function countResources() |
|
41
|
|
|
{ |
|
42
|
|
|
return count( |
|
43
|
|
|
$this->resources['resources'] |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function create($resource, array $constraints) |
|
48
|
|
|
{ |
|
49
|
|
|
foreach ($constraints as $name => $value) { |
|
50
|
|
View Code Duplication |
if (!isset($this->resources['resources'][$resource]['constraints']['allowed'])) { |
|
|
|
|
|
|
51
|
|
|
throw new RuntimeException( |
|
52
|
|
|
'Not allowed `' . $name . '` constraint' |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
if (!in_array($name, $this->resources['resources'][$resource]['constraints']['allowed'])) { |
|
|
|
|
|
|
57
|
|
|
throw new RuntimeException( |
|
58
|
|
|
'Not allowed `' . $name . '` constraint' |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function allowed($resource) |
|
65
|
|
|
{ |
|
66
|
|
|
$allowed = []; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($this->allowed as $item) { |
|
69
|
|
|
if (isset($this->resources['resources'][$resource]['constraints'][$item])) { |
|
70
|
|
|
$allowed = array_merge( |
|
71
|
|
|
$allowed, |
|
72
|
|
|
$this->resources['resources'][$resource]['constraints'][$item] |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $allowed; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
public function mandatory($resource) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
if (isset($this->resources['resources'][$resource]['constraints']['mandatory'])) { |
|
83
|
|
|
return $this->resources['resources'][$resource]['constraints']['mandatory']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function defaults($resource) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if (isset($this->resources['resources'][$resource]['constraints']['defaults'])) { |
|
92
|
|
|
return $this->resources['resources'][$resource]['constraints']['defaults']; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return []; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.