Completed
Pull Request — master (#145)
by Simone
01:55
created

AllowedProperties::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources\Validators\Validators;
13
14
use Sensorario\Resources\Resource;
15
use Sensorario\Resources\Validators\Interfaces\Validator;
16
17
final class AllowedProperties implements Validator
18
{
19 27
    public function check(Resource $resource)
20
    {
21 27
        $this->resource = $resource;
0 ignored issues
show
Bug introduced by
The property resource 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...
22
23 27
        $this->buildAllowedProperties();
24
25 27
        if ($this->checkShouldBeSkipped()) {
26 2
            return;
27
        }
28
29 25
        $this->ensurePropertyIsAllowed();
30 24
    }
31
32 27
    private function buildAllowedProperties()
33
    {
34 27
        $this->allowed = array_merge(
0 ignored issues
show
Bug introduced by
The property allowed 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...
35 27
            $this->resource->allowed(),
36 27
            $this->resource->mandatory()
37
        );
38 27
    }
39
40 27
    private function checkShouldBeSkipped()
41
    {
42 27
        foreach ($this->allowed as $kk => $vv) {
43 25
            if (!is_numeric($kk) && isset($vv['when']) && $this->resource->hasProperty($vv['when']['property'])) {
44 2
                return true;
45
            }
46
        }
47
48 25
        return false;
49
    }
50
51 25
    public function ensurePropertyIsAllowed()
52
    {
53 25
        foreach ($this->resource->properties() as $key => $value) {
54 17
            if (!in_array($key, $this->allowed)) {
55 1
                throw new \Sensorario\Resources\Exceptions\NotAllowedKeyException(
56 1
                    "Key `" . get_class($this->resource)
57 1
                    . "::\$$key` with value `" . $value
58 1
                    . "` is not allowed"
59
                );
60
            }
61
        }
62 24
    }
63
}
64