Passed
Push — master ( ec8827...deea55 )
by Edward
02:26
created

TestOperation::matches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Patch\Operation;
5
6
use Remorhaz\JSON\Data\Comparator\ComparatorInterface;
7
use Remorhaz\JSON\Data\Value\NodeValueInterface;
8
use Remorhaz\JSON\Pointer\Processor\ProcessorInterface as PointerProcessorInterface;
9
use Remorhaz\JSON\Pointer\Processor\Result\ResultInterface as PointerResultInterface;
10
use Remorhaz\JSON\Pointer\Query\QueryInterface as PointerQueryInterface;
11
12
final class TestOperation implements OperationInterface
13
{
14
15
    private $index;
16
17
    private $pathPointer;
18
19
    private $value;
20
21
    private $equalComparator;
22
23
    public function __construct(
24
        int $index,
25
        PointerQueryInterface $pathPointer,
26
        NodeValueInterface $value,
27
        ComparatorInterface $equalComparator
28
    ) {
29
        $this->index = $index;
30
        $this->pathPointer = $pathPointer;
31
        $this->value = $value;
32
        $this->equalComparator = $equalComparator;
33
    }
34
35
    public function apply(NodeValueInterface $input, PointerProcessorInterface $pointerProcessor): NodeValueInterface
36
    {
37
        $selectResult = $pointerProcessor->select($this->pathPointer, $input);
38
39
        if ($this->matches($selectResult)) {
40
            return $input;
41
        }
42
43
        throw new Exception\TestFailedException($this->index, $input, $this->pathPointer->getSource(), $this->value);
44
    }
45
46
    private function matches(PointerResultInterface $selectResult): bool
47
    {
48
        if (!$selectResult->exists()) {
49
            return false;
50
        }
51
52
        return $this
53
            ->equalComparator
54
            ->compare($selectResult->get(), $this->value);
55
    }
56
}
57