Completed
Pull Request — master (#65)
by Simone
02:57
created

Container::globals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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