ResultFactory   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 76
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createSelectOneResult() 0 3 1
A createSelectPathsResult() 0 5 1
A createSelectResult() 0 3 1
A createValueResult() 0 5 2
A getValuePath() 0 7 2
A createSelectOnePathResult() 0 10 3
A findSingleValue() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Processor\Result;
6
7
use Remorhaz\JSON\Data\Export\ValueDecoderInterface;
8
use Remorhaz\JSON\Data\Export\ValueEncoderInterface;
9
use Remorhaz\JSON\Data\Path\PathInterface;
10
use Remorhaz\JSON\Path\Processor\PathEncoderInterface;
11
use Remorhaz\JSON\Data\Value\NodeValueInterface;
12
use Remorhaz\JSON\Data\Value\ValueInterface;
13
use Remorhaz\JSON\Path\Value\ValueListInterface;
14
15
use function array_map;
16
use function count;
17
18
final class ResultFactory implements ResultFactoryInterface
19
{
20
21
    private $jsonEncoder;
22
23
    private $jsonDecoder;
24
25
    private $pathsEncoder;
26
27 19
    public function __construct(
28
        ValueEncoderInterface $jsonEncoder,
29
        ValueDecoderInterface $jsonDecoder,
30
        PathEncoderInterface $pathEncoder
31
    ) {
32 19
        $this->jsonEncoder = $jsonEncoder;
33 19
        $this->jsonDecoder = $jsonDecoder;
34 19
        $this->pathsEncoder = $pathEncoder;
35 19
    }
36
37 3
    public function createSelectResult(ValueListInterface $values): SelectResultInterface
38
    {
39 3
        return new SelectResult($this->jsonEncoder, $this->jsonDecoder, ...$values->getValues());
40
    }
41
42 5
    public function createSelectOneResult(ValueListInterface $values): ValueResultInterface
43
    {
44 5
        return $this->createValueResult($this->findSingleValue($values));
45
    }
46
47 2
    public function createSelectPathsResult(ValueListInterface $values): SelectPathsResultInterface
48
    {
49 2
        return new SelectPathsResult(
50 2
            $this->pathsEncoder,
51 2
            ...array_map([$this, 'getValuePath'], $values->getValues())
52
        );
53
    }
54
55 5
    private function getValuePath(ValueInterface $value): PathInterface
56
    {
57 5
        if (!$value instanceof NodeValueInterface) {
58 2
            throw new Exception\PathNotFoundInValueException($value);
59
        }
60
61 3
        return $value->getPath();
62
    }
63
64 5
    public function createSelectOnePathResult(ValueListInterface $values): SelectOnePathResultInterface
65
    {
66 5
        $value = $this->findSingleValue($values);
67 4
        $path = isset($value)
68 3
            ? $this->getValuePath($value)
69 3
            : null;
70
71 3
        return isset($path)
72 2
            ? new ExistingSelectOnePathResult($this->pathsEncoder, $path)
73 3
            : new NonExistingSelectOnePathResult();
74
    }
75
76 10
    private function findSingleValue(ValueListInterface $values): ?ValueInterface
77
    {
78 10
        switch (count($values->getValues())) {
79 10
            case 0:
80 2
                return null;
81
82 8
            case 1:
83 6
                return $values->getValue(0);
84
        }
85
86 2
        throw new Exception\MoreThanOneValueInListException($values);
87
    }
88
89 8
    public function createValueResult(?ValueInterface $value): ValueResultInterface
90
    {
91 8
        return isset($value)
92 6
            ? new ExistingValueResult($this->jsonEncoder, $this->jsonDecoder, $value)
93 8
            : new NonExistingValueResult();
94
    }
95
}
96