Completed
Push — master ( 56ea51...5cce8c )
by Edward
06:13
created

Query::getCallbackCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Query;
5
6
use Remorhaz\JSON\Path\Value\NodeValueListBuilder;
7
use Throwable;
8
use function call_user_func;
9
use Remorhaz\JSON\Data\Value\NodeValueInterface;
10
use Remorhaz\JSON\Path\Value\ValueListInterface;
11
use Remorhaz\JSON\Path\Runtime\RuntimeInterface;
12
13
final class Query implements QueryInterface
14
{
15
16
    private $source;
17
18
    private $callbackBuilder;
19
20 4
    public function __construct(string $source, CallbackBuilderInterface $callbackBuilder)
21
    {
22 4
        $this->source = $source;
23 4
        $this->callbackBuilder = $callbackBuilder;
24 4
    }
25
26 2
    public function __invoke(NodeValueInterface $rootNode, RuntimeInterface $runtime): ValueListInterface
27
    {
28 2
        $input = (new NodeValueListBuilder)
29 2
            ->addValue($rootNode, 0)
30 2
            ->build();
31
32
        try {
33
            return call_user_func(
34
                $this
35 2
                    ->callbackBuilder
36 2
                    ->getCallback(),
37
                $input,
38 2
                $runtime->getValueListFetcher(),
39 2
                $runtime->getEvaluator(),
40 2
                $runtime->getLiteralFactory(),
41 2
                $runtime->getMatcherFactory(),
42
            );
43
        } catch (Throwable $e) {
44
            throw new Exception\QueryExecutionFailedException(
45
                $this->source,
46
                $this->callbackBuilder->getCallbackCode(),
47
                $e,
48
            );
49
        }
50
    }
51
52 1
    public function getCapabilities(): CapabilitiesInterface
53
    {
54
        return $this
55 1
            ->callbackBuilder
56 1
            ->getCapabilities();
57
    }
58
59 1
    public function getSource(): string
60
    {
61 1
        return $this->source;
62
    }
63
64
    public function getCallbackCode(): string
65
    {
66
        return $this
67
            ->callbackBuilder
68
            ->getCallbackCode();
69
    }
70
}
71