Passed
Push — master ( 162b02...da0f52 )
by Edward
02:27
created

ResultFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 30
dl 0
loc 71
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createSelectOneResult() 0 11 2
A createSelectPathsResult() 0 5 1
A createSelectResult() 0 3 1
A getValuePath() 0 7 2
A createSelectOnePathResult() 0 7 2
A findSingleValue() 0 11 3
A __construct() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Processor\Result;
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\Processor\PathEncoder;
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
    public function __construct(EncoderInterface $jsonEncoder, DecoderInterface $jsonDecoder, PathEncoder $pathEncoder)
26
    {
27
        $this->jsonEncoder = $jsonEncoder;
28
        $this->jsonDecoder = $jsonDecoder;
29
        $this->pathsEncoder = $pathEncoder;
30
    }
31
32
    public function createSelectResult(ValueListInterface $values): SelectResultInterface
33
    {
34
        return new SelectResult($this->jsonEncoder, $this->jsonDecoder, ...$values->getValues());
35
    }
36
37
    public function createSelectOneResult(ValueListInterface $values): SelectOneResultInterface
38
    {
39
        $value = $this->findSingleValue($values);
40
41
        return isset($value)
42
            ? new ExistingSelectOneResult(
43
                $this->jsonEncoder,
44
                $this->jsonDecoder,
45
                $values->getValue(0)
46
            )
47
            : new NonExistingSelectOneResult;
48
    }
49
50
    public function createSelectPathsResult(ValueListInterface $values): SelectPathsResultInterface
51
    {
52
        return new SelectPathsResult(
53
            $this->pathsEncoder,
54
            ...array_map([$this, 'getValuePath'], $values->getValues())
55
        );
56
    }
57
58
    private function getValuePath(ValueInterface $value): PathInterface
59
    {
60
        if (!$value instanceof NodeValueInterface) {
61
            throw new Exception\PathNotFoundInValueException($value);
62
        }
63
64
        return $value->getPath();
65
    }
66
67
    public function createSelectOnePathResult(ValueListInterface $values): SelectOnePathResultInterface
68
    {
69
        $value = $this->findSingleValue($values);
70
71
        return isset($values)
72
            ? 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

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