Completed
Push — master ( 657524...bde6a9 )
by Simone
19s
created

Container::allowed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
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
    public function __construct(array $resources)
20
    {
21
        if (!isset($resources['resources'])) {
22
            throw new RuntimeException(
23
                ''
24
            );
25
        }
26
27
        foreach ($resources['resources'] as $item) {
28
            if (isset($item['constraints'])) {
29
                foreach ($item['constraints'] as $name => $value) {
30
                    if (!in_array($name, $this->allowed)) {
31
                        throw new RuntimeException(
32
                            'Invalid constraint'
33
                        );
34
                    }
35
                }
36
            }
37
        }
38
39
        $this->resources = $resources;
40
    }
41
42
    public function countResources()
43
    {
44
        return count(
45
            $this->resources['resources']
46
        );
47
    }
48
49
    public function create($resource, array $constraints)
50
    {
51
        foreach ($constraints as $name => $value) {
52 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...
53
                throw new RuntimeException(
54
                    'Not allowed `' . $name . '` constraint'
55
                );
56
            }
57
58 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...
59
                throw new RuntimeException(
60
                    'Not allowed `' . $name . '` constraint'
61
                );
62
            }
63
        }
64
    }
65
66
    public function allowed($resource)
67
    {
68
        $allowed = [];
69
70
        foreach ($this->allowed as $item) {
71
            if (isset($this->resources['resources'][$resource]['constraints'][$item])) {
72
                $allowed = array_merge(
73
                    $allowed,
74
                    $this->resources['resources'][$resource]['constraints'][$item]
75
                );
76
            }
77
        }
78
79
        return $allowed;
80
    }
81
82 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...
83
    {
84
        if (isset($this->resources['resources'][$resource]['constraints']['mandatory'])) {
85
            return $this->resources['resources'][$resource]['constraints']['mandatory'];
86
        }
87
88
        return [];
89
    }
90
91 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...
92
    {
93
        if (isset($this->resources['resources'][$resource]['constraints']['defaults'])) {
94
            return $this->resources['resources'][$resource]['constraints']['defaults'];
95
        }
96
97
        return [];
98
    }
99
100 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...
101
    {
102
        if (isset($this->resources['resources'][$resource]['constraints']['rules'])) {
103
            return $this->resources['resources'][$resource]['constraints']['rules'];
104
        }
105
106
        return [];
107
    }
108
109 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...
110
    {
111
        if (isset($this->resources['resources'][$resource]['constraints']['allowedValues'])) {
112
            return $this->resources['resources'][$resource]['constraints']['allowedValues'];
113
        }
114
115
        return [];
116
    }
117
}
118