Passed
Push — master ( 8e4f12...3d58fd )
by Edward
05:59
created

ResultFactory   A

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