Passed
Push — master ( b4dbcd...f875e6 )
by Edward
05:05
created

Query   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 50
ccs 24
cts 24
cp 1
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSource() 0 3 1
A getCapabilities() 0 5 1
A __invoke() 0 23 2
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
            $callback = $this
30 3
                ->callbackBuilder
31 3
                ->getCallback();
32
33
            $args = [
34 3
                (new NodeValueListBuilder)
35 3
                    ->addValue($rootNode, 0)
36 3
                    ->build(),
37 3
                $runtime->getValueListFetcher(),
38 3
                $runtime->getEvaluator(),
39 3
                $runtime->getLiteralFactory(),
40 3
                $runtime->getMatcherFactory(),
41
            ];
42
43 3
            return call_user_func($callback, ...$args);
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