Completed
Pull Request — master (#107)
by Simone
02:20
created

Container::allowedValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Sensorario\Resources;
4
5
use \RuntimeException;
6
7
class Container
8
    extends ContainerBase
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
9
{
10
    private $resources;
11
12
    private $rewrites;
13
14
    private $allowed = [
15
        'allowed',
16
        'allowedRanges',
17
        'allowedValues',
18
        'defaults',
19
        'mandatory',
20
        'rules',
21
    ];
22
23
    public function __construct(array $resources)
24
    {
25
        if (!isset($resources['resources'])) {
26
            throw new RuntimeException(
27
                'resources element is not defined'
28
            );
29
        }
30
31
        $this->rewrites = [];
32
        $this->globals  = [];
0 ignored issues
show
Bug introduced by
The property globals does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
34
        $this->ensureValidConstraints($resources);
35
36
        foreach ($resources['resources'] as $item) {
37
            if (isset($item['rewrite'])) {
38
                foreach ($item['rewrite'] as $field => $rewriteRule) {
39
                    $this->rewrites[$field] = $rewriteRule;
40
                }
41
            }
42
        }
43
44
        if (isset($resources['globals'])) {
45
            $this->globals = $resources['globals'];
46
        }
47
48
        $this->resources = $resources;
49
    }
50
51
    private function ensureValidConstraints($resources)
52
    {
53
        foreach ($resources['resources'] as $item) {
54
            if (isset($item['constraints'])) {
55
                foreach ($item['constraints'] as $name => $value) {
56
                    if (!in_array($name, $this->allowed)) {
57
                        throw new RuntimeException(
58
                            'Invalid constraint: '
59
                            . 'name ' . $name
60
                            . '; value ' . $value
61
                        );
62
                    }
63
                }
64
            }
65
        }
66
    }
67
68
    public function countResources()
69
    {
70
        return count(
71
            $this->resources['resources']
72
        );
73
    }
74
75
    public function create($resource, array $constraints)
76
    {
77
        foreach ($constraints as $name => $value) {
78 View Code Duplication
            if (!isset($this->resources['resources'][$resource]['constraints']['allowed'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
79
                throw new RuntimeException(
80
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
81
                );
82
            }
83
84 View Code Duplication
            if (!in_array($name, $this->resources['resources'][$resource]['constraints']['allowed'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
85
                throw new RuntimeException(
86
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
87
                );
88
            }
89
        }
90
    }
91
92
    public function allowed($resource)
93
    {
94
        $allowed = [];
95
96
        foreach ($this->allowed as $item) {
97 View Code Duplication
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
98
                $allowed = array_merge(
99
                    $allowed,
100
                    $this->resources['resources'][$resource]['constraints'][$item]
101
                );
102
            }
103
        }
104
105
        return $allowed;
106
    }
107
108
    public function getConstraints(
109
        $constraintName,
110
        $resource
111
    ) {
112 View Code Duplication
        if (isset($this->resources['resources'][$resource]['constraints'][$constraintName])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
113
            return $this->resources['resources'][$resource]['constraints'][$constraintName];
114
        }
115
116
        return [];
117
    }
118
119
    public function rewrites()
120
    {
121
        return $this->rewrites;
122
    }
123
124
    public function globals()
125
    {
126
        return $this->globals;
127
    }
128
129
    public function mandatory($resource)
130
    {
131
        return $this->getConstraints(
132
            'mandatory',
133
            $resource
134
        );
135
    }
136
137
    public function defaults($resource)
138
    {
139
        return $this->getConstraints(
140
            'defaults',
141
            $resource
142
        );
143
    }
144
145
    public function rules($resource)
146
    {
147
        return $this->getConstraints(
148
            'rules',
149
            $resource
150
        );
151
    }
152
153
    public function allowedValues($resource)
154
    {
155
        return $this->getConstraints(
156
            'allowedValues',
157
            $resource
158
        );
159
    }
160
161
    public function ranges($resource)
162
    {
163
        return $this->getConstraints(
164
            'allowedRanges',
165
            $resource
166
        );
167
    }
168
}
169