LazyQuery::getLoadedQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Patch\Query;
5
6
use Iterator;
7
use Remorhaz\JSON\Data\Export\ValueDecoderInterface;
8
use Remorhaz\JSON\Data\Export\ValueEncoderInterface;
9
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
10
use Remorhaz\JSON\Data\Value\NodeValueInterface;
11
use Remorhaz\JSON\Patch\Operation\OperationFactoryInterface;
12
use Remorhaz\JSON\Patch\Result\ResultInterface;
13
use Remorhaz\JSON\Pointer\Processor\ProcessorInterface as PointerProcessorInterface;
14
use Throwable;
15
16
final class LazyQuery implements QueryInterface
17
{
18
19
    private $operationFactory;
20
21
    private $encoder;
22
23
    private $decoder;
24
25
    private $patch;
26
27
    private $loadedQuery;
28
29 3
    public function __construct(
30
        OperationFactoryInterface $operationFactory,
31
        ValueEncoderInterface $encoder,
32
        ValueDecoderInterface $decoder,
33
        NodeValueInterface $patch
34
    ) {
35 3
        $this->operationFactory = $operationFactory;
36 3
        $this->encoder = $encoder;
37 3
        $this->decoder = $decoder;
38 3
        $this->patch = $patch;
39 3
    }
40
41 3
    public function __invoke(NodeValueInterface $data, PointerProcessorInterface $pointerProcessor): ResultInterface
42
    {
43 3
        return $this->getLoadedQuery()($data, $pointerProcessor);
44
    }
45
46 3
    private function getLoadedQuery(): QueryInterface
47
    {
48 3
        if (!isset($this->loadedQuery)) {
49 3
            $this->loadedQuery = $this->loadQuery();
50
        }
51
52 1
        return $this->loadedQuery;
53
    }
54
55 3
    private function loadQuery(): QueryInterface
56
    {
57 3
        $operations = [];
58 3
        foreach ($this->createOperationDataIterator($this->patch) as $index => $operationData) {
59
            try {
60
                $operations[] = $this
61 2
                    ->operationFactory
62 2
                    ->fromJson($operationData, $index);
0 ignored issues
show
Bug introduced by
It seems like $index can also be of type double and null and string and true; however, parameter $index of Remorhaz\JSON\Patch\Oper...ryInterface::fromJson() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                    ->fromJson($operationData, /** @scrutinizer ignore-type */ $index);
Loading history...
63 1
            } catch (Throwable $e) {
64 1
                throw new Exception\OperationNotLoadedException($index, $this->patch, $e);
0 ignored issues
show
Bug introduced by
It seems like $index can also be of type double and null and string and true; however, parameter $index of Remorhaz\JSON\Patch\Quer...xception::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
                throw new Exception\OperationNotLoadedException(/** @scrutinizer ignore-type */ $index, $this->patch, $e);
Loading history...
65
            }
66
        }
67
68 1
        return new Query($this->encoder, $this->decoder, ...$operations);
69
    }
70
71 3
    private function createOperationDataIterator(NodeValueInterface $patch): Iterator
72
    {
73 3
        if ($patch instanceof ArrayValueInterface) {
74 2
            return $patch->createChildIterator();
75
        }
76
77 1
        throw new Exception\InvalidPatchException($patch);
78
    }
79
}
80