Passed
Push — master ( c6e62a...162b02 )
by Edward
03:02
created

ResultFactory::createSelectPathsResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Processor;
5
6
use function array_map;
7
use function count;
8
use Remorhaz\JSON\Data\Export\DecoderInterface;
9
use Remorhaz\JSON\Data\Export\EncoderInterface;
10
use Remorhaz\JSON\Data\Path\PathInterface;
11
use Remorhaz\JSON\Data\Value\NodeValueInterface;
12
use Remorhaz\JSON\Data\Value\ValueInterface;
13
use Remorhaz\JSON\Path\Value\ValueListInterface;
14
15
final class ResultFactory implements ResultFactoryInterface
16
{
17
18
    private $jsonEncoder;
19
20
    private $jsonDecoder;
21
22
    private $pathsEncoder;
23
24
    public function __construct(EncoderInterface $jsonEncoder, DecoderInterface $jsonDecoder, PathEncoder $pathEncoder)
25
    {
26
        $this->jsonEncoder = $jsonEncoder;
27
        $this->jsonDecoder = $jsonDecoder;
28
        $this->pathsEncoder = $pathEncoder;
29
    }
30
31
    public function createSelectResult(ValueListInterface $values): SelectResultInterface
32
    {
33
        return new SelectResult($this->jsonEncoder, $this->jsonDecoder, ...$values->getValues());
34
    }
35
36
    public function createSelectOneResult(ValueListInterface $values): SelectOneResultInterface
37
    {
38
        $value = $this->findSingleValue($values);
39
40
        return isset($value)
41
            ? new ExistingSelectOneResult(
42
                $this->jsonEncoder,
43
                $this->jsonDecoder,
44
                $values->getValue(0)
45
            )
46
            : new NonExistingSelectOneResult;
47
    }
48
49
    public function createSelectPathsResult(ValueListInterface $values): SelectPathsResultInterface
50
    {
51
        return new SelectPathsResult(
52
            $this->pathsEncoder,
53
            ...array_map([$this, 'getValuePath'], $values->getValues())
54
        );
55
    }
56
57
    private function getValuePath(ValueInterface $value): PathInterface
58
    {
59
        if (!$value instanceof NodeValueInterface) {
60
            throw new Exception\PathNotFoundInValueException($value);
61
        }
62
63
        return $value->getPath();
64
    }
65
66
    public function createSelectOnePathResult(ValueListInterface $values): SelectOnePathResultInterface
67
    {
68
        $value = $this->findSingleValue($values);
69
70
        return isset($values)
71
            ? new ExistingSelectOnePathResult($this->pathsEncoder, $this->getValuePath($value))
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $value of Remorhaz\JSON\Path\Proce...Factory::getValuePath() does only seem to accept Remorhaz\JSON\Data\Value\ValueInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            ? new ExistingSelectOnePathResult($this->pathsEncoder, $this->getValuePath(/** @scrutinizer ignore-type */ $value))
Loading history...
72
            : new NonExistingSelectOnePathResult;
73
    }
74
75
    private function findSingleValue(ValueListInterface $values): ?ValueInterface
76
    {
77
        switch (count($values->getValues())) {
78
            case 0:
79
                return null;
80
81
            case 1:
82
                return $values->getValue(0);
83
        }
84
85
        throw new Exception\MoreThanOneValueInListException($values);
86
    }
87
}
88