Completed
Pull Request — master (#64)
by Simone
05:24
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
        'allowedValues',
14
        'defaults',
15
        'mandatory',
16
        'rules',
17
    ];
18
19
    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...
20
21
    public function __construct(array $resources)
22
    {
23
        if (!isset($resources['resources'])) {
24
            throw new RuntimeException(
25
                'resources element is not defined'
26
            );
27
        }
28
29
        $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...
30
31
        foreach ($resources['resources'] as $item) {
32
            if (isset($item['constraints'])) {
33
                foreach ($item['constraints'] as $name => $value) {
34
                    if (!in_array($name, $this->allowed)) {
35
                        throw new RuntimeException(
36
                            'Invalid constraint: '
37
                            . 'name ' . $name
38
                            . '; value ' . $value
39
                        );
40
                    }
41
                }
42
            }
43
        }
44
45
        foreach ($resources['resources'] as $item) {
46
            if (isset($item['rewrite'])) {
47
                foreach ($item['rewrite'] as $field => $rewriteRule) {
48
                    $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...
49
                }
50
            }
51
        }
52
53
        $this->resources = $resources;
54
    }
55
56
    public function countResources()
57
    {
58
        return count(
59
            $this->resources['resources']
60
        );
61
    }
62
63
    public function create($resource, array $constraints)
64
    {
65
        foreach ($constraints as $name => $value) {
66 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...
67
                throw new RuntimeException(
68
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
69
                );
70
            }
71
72 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...
73
                throw new RuntimeException(
74
                    'Not allowed `' . $name . '` constraint with value `' . $value . '`'
75
                );
76
            }
77
        }
78
    }
79
80
    public function allowed($resource)
81
    {
82
        $allowed = [];
83
84
        foreach ($this->allowed as $item) {
85
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
86
                $allowed = array_merge(
87
                    $allowed,
88
                    $this->resources['resources'][$resource]['constraints'][$item]
89
                );
90
            }
91
        }
92
93
        return $allowed;
94
    }
95
96 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...
97
    {
98
        if (isset($this->resources['resources'][$resource]['constraints']['mandatory'])) {
99
            return $this->resources['resources'][$resource]['constraints']['mandatory'];
100
        }
101
102
        return [];
103
    }
104
105 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...
106
    {
107
        if (isset($this->resources['resources'][$resource]['constraints']['defaults'])) {
108
            return $this->resources['resources'][$resource]['constraints']['defaults'];
109
        }
110
111
        return [];
112
    }
113
114 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...
115
    {
116
        if (isset($this->resources['resources'][$resource]['constraints']['rules'])) {
117
            return $this->resources['resources'][$resource]['constraints']['rules'];
118
        }
119
120
        return [];
121
    }
122
123 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...
124
    {
125
        if (isset($this->resources['resources'][$resource]['constraints']['allowedValues'])) {
126
            return $this->resources['resources'][$resource]['constraints']['allowedValues'];
127
        }
128
129
        return [];
130
    }
131
132
    public function rewrites()
133
    {
134
        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...
135
    }
136
}
137