Passed
Push — master ( 81686d...1595cb )
by Kirill
04:41
created

ValueFactory::create()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 1
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Backend;
13
14
use Phplrt\Source\Exception\NotAccessibleException;
15
use Railt\Dumper\Facade;
16
use Railt\SDL\Exception\TypeErrorException;
17
use Railt\SDL\Frontend\Ast\Node;
18
use Railt\TypeSystem\Value\BooleanValue;
19
use Railt\TypeSystem\Value\FloatValue;
20
use Railt\TypeSystem\Value\InputObjectValue;
21
use Railt\TypeSystem\Value\IntValue;
22
use Railt\TypeSystem\Value\ListValue;
0 ignored issues
show
Bug introduced by
The type Railt\TypeSystem\Value\ListValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Railt\TypeSystem\Value\NullValue;
24
use Railt\TypeSystem\Value\StringValue;
25
use Railt\TypeSystem\Value\ValueInterface;
26
27
/**
28
 * Class ValueFactory
29
 */
30
class ValueFactory
31
{
32
    /**
33
     * @param mixed $value
34
     * @param Node|null $context
35
     * @return ValueInterface
36
     * @throws TypeErrorException
37
     * @throws NotAccessibleException
38
     * @throws \RuntimeException
39
     */
40
    public function make($value, Node $context = null): ValueInterface
41
    {
42
        try {
43
            return $this->create($value);
44
        } catch (\Throwable $e) {
45
            throw new TypeErrorException($e->getMessage(), $context, $e);
46
        }
47
    }
48
49
    /**
50
     * @param mixed $value
51
     * @return ValueInterface
52
     * @throws \InvalidArgumentException
53
     * @throws \OverflowException
54
     */
55
    private function create($value): ValueInterface
56
    {
57
        switch (true) {
58
            case $value instanceof ValueInterface:
59
                return $value;
60
61
            case \is_bool($value):
62
                return BooleanValue::parse($value);
63
64
            case \is_float($value):
65
                return FloatValue::parse($value);
66
67
            case \is_int($value):
68
                return IntValue::parse($value);
69
70
            case $value === null:
71
                return NullValue::parse($value);
72
73
            case \is_string($value):
74
                return StringValue::parse($value);
75
76
            case \is_iterable($value):
77
                return $this->fromIterator($value);
78
79
            default:
80
                $error = 'Value of type %s can not be converted to GraphQL type';
81
82
                throw new \InvalidArgumentException(\sprintf($error, Facade::dump($value)));
83
        }
84
    }
85
86
    /**
87
     * @param iterable $iterator
88
     * @return ValueInterface
89
     * @throws \InvalidArgumentException
90
     * @throws \OverflowException
91
     */
92
    private function fromIterator(iterable $iterator): ValueInterface
93
    {
94
        [$result, $isObject] = [[], false];
95
96
        foreach ($iterator as $key => $value) {
97
            if (\is_string($key)) {
98
                $isObject = true;
99
            }
100
101
            if ($isObject) {
102
                $result[$key] = $this->create($value);
103
            } else {
104
                $result[] = $this->create($value);
105
            }
106
        }
107
108
        return $isObject
109
            ? InputObjectValue::parse($result)
110
            : ListValue::parse($result);
111
    }
112
}
113