Passed
Push — master ( 5cce8c...cb0ced )
by Edward
04:53
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 5
    public function __construct(string $source, CallbackBuilderInterface $callbackBuilder)
21
    {
22 5
        $this->source = $source;
23 5
        $this->callbackBuilder = $callbackBuilder;
24 5
    }
25
26 3
    public function __invoke(NodeValueInterface $rootNode, RuntimeInterface $runtime): ValueListInterface
27
    {
28
        try {
29 3
            $input = (new NodeValueListBuilder)
30 3
                ->addValue($rootNode, 0)
31 3
                ->build();
32
            $callback = $this
33 3
                ->callbackBuilder
34 3
                ->getCallback();
35
36
            return call_user_func(
37 3
                $callback,
38
                $input,
39 3
                $runtime->getValueListFetcher(),
40 3
                $runtime->getEvaluator(),
41 3
                $runtime->getLiteralFactory(),
42 3
                $runtime->getMatcherFactory(),
43
            );
44 1
        } catch (Throwable $e) {
45 1
            throw new Exception\QueryExecutionFailedException(
46 1
                $this->source,
47 1
                $this->callbackBuilder->getCallbackCode(),
48
                $e,
49
            );
50
        }
51
    }
52
53 1
    public function getCapabilities(): CapabilitiesInterface
54
    {
55
        return $this
56 1
            ->callbackBuilder
57 1
            ->getCapabilities();
58
    }
59
60 1
    public function getSource(): string
61
    {
62 1
        return $this->source;
63
    }
64
}
65