Completed
Pull Request — master (#85)
by Simone
04:42
created

Container::rewrites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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