Query::getCapabilities()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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