Passed
Push — dev ( f1f1e7...329389 )
by Fike
03:26
created

TileTreeValidator::validate()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 20

Duplication

Lines 9
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 5
nop 2
dl 9
loc 27
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Framework\Validation\Tile;
4
5
use AmaTeam\Image\Projection\Framework\Validation\ValidationException;
6
use AmaTeam\Image\Projection\Framework\Validation\ValidatorInterface;
7
8
class TileTreeValidator implements ValidatorInterface
9
{
10
    /**
11
     * @var string[]
12
     */
13
    private $faces;
14
    /**
15
     * @var ValidatorInterface
16
     */
17
    private $faceValidator;
18
19
    /**
20
     * @param string[] $faces
21
     * @param ValidatorInterface $faceValidator
22
     */
23
    public function __construct(
24
        array $faces,
25
        ValidatorInterface $faceValidator = null
26
    ) {
27
        $this->faces = $faces;
28
        $this->faceValidator = $faceValidator;
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function validate($value, array $path = [])
35
    {
36
        if (!is_array($value)) {
37
            throw new ValidationException('Is empty', $path);
38
        }
39
        $missingFaces = $this->faces;
40
        $extraFaces = [];
41
        foreach ($value as $face => $grid) {
42
            if (in_array($face, $this->faces)) {
43
                $missingFaces = array_diff($missingFaces, [$face]);
44
            } else {
45
                $extraFaces[] = $face;
46
            }
47
            if ($this->faceValidator) {
48
                $facePath = array_merge($path, [$face]);
49
                $this->faceValidator->validate($grid, $facePath);
50
            }
51
        }
52 View Code Duplication
        if (!empty($missingFaces)) {
1 ignored issue
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...
53
            $message = 'Following faces were not found: ' .
54
                implode(', ', $missingFaces);
55
            throw new ValidationException($message);
56
        }
57 View Code Duplication
        if (!empty($extraFaces)) {
1 ignored issue
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...
58
            $message = 'Following extra faces were found: ' .
59
                implode(', ', $extraFaces);
60
            throw new ValidationException($message);
61
        }
62
    }
63
}
64