Completed
Push — master ( 5b38d7...e0573a )
by Simone
04:00 queued 01:02
created

Container::defaults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1

1 Method

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