Completed
Push — master ( 6f7871 )
by Simone
02:56
created

Container::rewrites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
        'allowedRanges',
14
        'allowedValues',
15
        'defaults',
16
        'mandatory',
17
        'rules',
18
    ];
19
20
    private $rewrite = [];
0 ignored issues
show
Unused Code introduced by
The property $rewrite is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
22
    public function __construct(array $resources)
23
    {
24
        if (!isset($resources['resources'])) {
25
            throw new RuntimeException(
26
                'resources element is not defined'
27
            );
28
        }
29
30
        $this->rewrites = [];
0 ignored issues
show
Bug introduced by
The property rewrites does not seem to exist. Did you mean rewrite?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31
        $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...
32
33
        foreach ($resources['resources'] as $item) {
34
            if (isset($item['constraints'])) {
35
                foreach ($item['constraints'] as $name => $value) {
36
                    if (!in_array($name, $this->allowed)) {
37
                        throw new RuntimeException(
38
                            'Invalid constraint: '
39
                            . 'name ' . $name
40
                            . '; value ' . $value
41
                        );
42
                    }
43
                }
44
            }
45
        }
46
47
        foreach ($resources['resources'] as $item) {
48
            if (isset($item['rewrite'])) {
49
                foreach ($item['rewrite'] as $field => $rewriteRule) {
50
                    $this->rewrites[$field] = $rewriteRule;
0 ignored issues
show
Bug introduced by
The property rewrites does not seem to exist. Did you mean rewrite?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
                }
52
            }
53
        }
54
55
        if (isset($resources['globals'])) {
56
            $this->globals = $resources['globals'];
57
        }
58
59
        $this->resources = $resources;
60
    }
61
62
    public function countResources()
63
    {
64
        return count(
65
            $this->resources['resources']
66
        );
67
    }
68
69
    public function create($resource, array $constraints)
70
    {
71
        foreach ($constraints as $name => $value) {
72 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...
73
                throw new RuntimeException(
74
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
75
                );
76
            }
77
78 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...
79
                throw new RuntimeException(
80
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
81
                );
82
            }
83
        }
84
    }
85
86
    public function allowed($resource)
87
    {
88
        $allowed = [];
89
90
        foreach ($this->allowed as $item) {
91
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
92
                $allowed = array_merge(
93
                    $allowed,
94
                    $this->resources['resources'][$resource]['constraints'][$item]
95
                );
96
            }
97
        }
98
99
        return $allowed;
100
    }
101
102 View Code Duplication
    public function mandatory($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104
        if (isset($this->resources['resources'][$resource]['constraints']['mandatory'])) {
105
            return $this->resources['resources'][$resource]['constraints']['mandatory'];
106
        }
107
108
        return [];
109
    }
110
111 View Code Duplication
    public function defaults($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
112
    {
113
        if (isset($this->resources['resources'][$resource]['constraints']['defaults'])) {
114
            return $this->resources['resources'][$resource]['constraints']['defaults'];
115
        }
116
117
        return [];
118
    }
119
120 View Code Duplication
    public function rules($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
121
    {
122
        if (isset($this->resources['resources'][$resource]['constraints']['rules'])) {
123
            return $this->resources['resources'][$resource]['constraints']['rules'];
124
        }
125
126
        return [];
127
    }
128
129 View Code Duplication
    public function allowedValues($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
130
    {
131
        if (isset($this->resources['resources'][$resource]['constraints']['allowedValues'])) {
132
            return $this->resources['resources'][$resource]['constraints']['allowedValues'];
133
        }
134
135
        return [];
136
    }
137
138
    public function rewrites()
139
    {
140
        return $this->rewrites;
0 ignored issues
show
Bug introduced by
The property rewrites does not seem to exist. Did you mean rewrite?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
141
    }
142
143 View Code Duplication
    public function ranges($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
144
    {
145
        if (isset($this->resources['resources'][$resource]['constraints']['allowedRanges'])) {
146
            return $this->resources['resources'][$resource]['constraints']['allowedRanges'];
147
        }
148
149
        return [];
150
    }
151
152
    public function globals()
153
    {
154
        return $this->globals;
155
    }
156
}
157