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 3
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 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Query;
6
7
use Remorhaz\JSON\Data\Value\NodeValueInterface;
8
use Remorhaz\JSON\Path\Value\ValueListInterface;
9
use Remorhaz\JSON\Path\Parser\ParserInterface;
10
use Remorhaz\JSON\Path\Runtime\RuntimeInterface;
11
12
final class LazyQuery implements QueryInterface
13
{
14
15
    private $loadedQuery;
16
17
    private $source;
18
19
    private $parser;
20
21
    private $astTranslator;
22
23 7
    private $callbackBuilder;
24
25 7
    public function __construct(
26 7
        string $source,
27 7
        ParserInterface $parser,
28 7
        AstTranslatorInterface $astTranslator,
29
        CallbackBuilderInterface $callbackBuilder
30 3
    ) {
31
        $this->source = $source;
32 3
        $this->parser = $parser;
33
        $this->astTranslator = $astTranslator;
34
        $this->callbackBuilder = $callbackBuilder;
35 3
    }
36
37
    public function __invoke(NodeValueInterface $rootNode, RuntimeInterface $runtime): ValueListInterface
38 3
    {
39 3
        return $this->getLoadedQuery()($rootNode, $runtime);
40
    }
41
42 1
    public function getCapabilities(): CapabilitiesInterface
43
    {
44 1
        return $this
45
            ->getLoadedQuery()
46
            ->getCapabilities();
47 6
    }
48
49 6
    public function getSource(): string
50 6
    {
51
        return $this->source;
52
    }
53 6
54
    private function getLoadedQuery(): QueryInterface
55
    {
56 6
        if (!isset($this->loadedQuery)) {
57
            $this->loadedQuery = $this->loadQuery();
58
        }
59 6
60 6
        return $this->loadedQuery;
61
    }
62
63 6
    private function loadQuery(): QueryInterface
64 6
    {
65
        $queryAst = $this
66
            ->parser
67
            ->buildQueryAst($this->source);
68
69
        return $this
70
            ->astTranslator
71
            ->buildQuery($this->source, $queryAst, $this->callbackBuilder);
72
    }
73
}
74