Completed
Push — master ( 1e4f77...826c6e )
by Kirill
02:10
created

Input   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 2
dl 0
loc 163
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
C resolveArguments() 0 27 7
A __construct() 0 5 1
A getResolveInfo() 0 4 1
A all() 0 4 1
A get() 0 4 1
A has() 0 4 1
A getRelations() 0 4 1
A getFieldName() 0 4 1
A hasRelation() 0 4 1
A getPath() 0 11 2
A __debugInfo() 0 7 1
A getAlias() 0 4 1
A hasAlias() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Adapters\Webonyx;
11
12
use GraphQL\Type\Definition\ResolveInfo;
13
use Railt\Adapters\InputInterface;
14
use Railt\Reflection\Abstraction\ArgumentInterface;
15
use Railt\Reflection\Abstraction\Common\HasArgumentsInterface;
16
use Railt\Routing\Route;
17
18
/**
19
 * Class Input
20
 * @package Railt\Adapters\Webonyx
21
 */
22
class Input implements InputInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    private $arguments = [];
28
29
    /**
30
     * @var ResolveInfo
31
     */
32
    private $info;
33
34
    /**
35
     * @var null|string
36
     */
37
    private $path;
38
39
    /**
40
     * @param HasArgumentsInterface $node
41
     * @param array $input
42
     * @return array
43
     * @throws \InvalidArgumentException
44
     */
45
    public static function resolveArguments(HasArgumentsInterface $node, array $input = []): array
46
    {
47
        $result = [];
48
49
        /** @var ArgumentInterface $default */
50
        foreach ($node->getArguments() as $default) {
51
            $name = $default->getName();
52
53
            if (
54
                !array_key_exists($name, $input) && // Empty argument
55
                !$default->hasDefaultValue() &&     // And has no default value
56
                $default->getType()->nonNull()      // And required
57
            ) {
58
                $message = 'Argument %s required for field %s';
59
                $field = $default->getParent()->getName();
60
                throw new \InvalidArgumentException(sprintf($message, $name, $field));
61
            }
62
63
            if (array_key_exists($name, $input)) {
64
                $result[$name] = $input[$name];
65
            } elseif ($default->hasDefaultValue()) {
66
                $result[$name] = $default->getDefaultValue();
67
            }
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * Request constructor.
75
     * @param array $arguments
76
     * @param ResolveInfo $info
77
     */
78
    public function __construct(array $arguments = [], ResolveInfo $info)
79
    {
80
        $this->info = $info;
81
        $this->arguments = $arguments;
82
    }
83
84
    /**
85
     * @return ResolveInfo
86
     */
87
    public function getResolveInfo(): ResolveInfo
88
    {
89
        return $this->info;
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function all(): array
96
    {
97
        return $this->arguments;
98
    }
99
100
    /**
101
     * @param string $argument
102
     * @param null $default
103
     * @return mixed|null
104
     */
105
    public function get(string $argument, $default = null)
106
    {
107
        return $this->arguments[$argument] ?? $default;
108
    }
109
110
    /**
111
     * @param string $argument
112
     * @return bool
113
     */
114
    public function has(string $argument): bool
115
    {
116
        return array_key_exists($argument, $this->arguments);
117
    }
118
119
    /**
120
     * TODO
121
     * @return iterable
122
     */
123
    public function getRelations(): iterable
124
    {
125
        throw new \LogicException(__METHOD__ . ' not implemented yet');
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getFieldName(): string
132
    {
133
        return $this->info->fieldName;
134
    }
135
136
    public function hasRelation(string $name): bool
137
    {
138
        throw new \LogicException(__METHOD__ . ' not implemented yet');
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getPath(): string
145
    {
146
        if ($this->path === null) {
147
            $path = $this->info->path;
148
            $path[count($path) - 1] = $this->info->fieldName;
149
150
            $this->path = implode(Route::DEFAULT_DELIMITER, $path);
151
        }
152
153
        return $this->path;
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    public function __debugInfo(): array
160
    {
161
        return [
162
            'path' => $this->path,
163
            'arguments' => $this->arguments
164
        ];
165
    }
166
167
    /**
168
     * @return string
169
     * @throws \LogicException
170
     */
171
    public function getAlias(): string
172
    {
173
        return $this->info->path[count($this->info->path) - 1];
174
    }
175
176
    /**
177
     * @return bool
178
     * @throws \LogicException
179
     */
180
    public function hasAlias(): bool
181
    {
182
        return $this->getAlias() !== $this->getFieldName();
183
    }
184
}
185