Completed
Pull Request — master (#70)
by Simone
02:34
created

MandatoryConditional::check()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 14
Ratio 51.85 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 14
loc 27
rs 4.909
cc 9
eloc 16
nc 6
nop 1
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 RuntimeException;
15
use Sensorario\Resources\Resource;
16
use Sensorario\Resources\Validators\Interfaces\Validator;
17
18
final class MandatoryConditional implements Validator
19
{
20
    public static function check(Resource $resource)
21
    {
22
        foreach ($resource->mandatory() as $key => $value) {
23
            if (isset($value['when']['has_value'])) {
24
                $propertyName = $value['when']['property'];
25
                $propertyValue = $value['when']['has_value'];
26
27
                if (is_array($propertyValue)) {
28
                    foreach ($propertyValue as $value) {
29 View Code Duplication
                        if ($resource->get($propertyName) === $value && $resource->hasNotProperty($key)) {
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...
30
                            throw new RuntimeException(
31
                                'When property `' . $key . '` has value '
32
                                . '`' . $value . '` also `' . $key . '` is mandatory'
33
                            );
34
                        }
35
                    }
36 View Code Duplication
                } else {
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...
37
                    if ($resource->get($propertyName) === $propertyValue && $resource->hasNotProperty($key)) {
38
                        throw new RuntimeException(
39
                            'When property `' . $key . '` has value '
40
                            . '`' . $propertyValue . '` also `' . $key . '` is mandatory'
41
                        );
42
                    }
43
                }
44
            }
45
        }
46
    }
47
}
48