Test Setup Failed
Push — master ( 31905c...c65f95 )
by Kirill
02:41
created

Input::getQueryType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 string
86
     */
87
    public function getQueryType(): string
88
    {
89
        return $this->info->operation->operation;
90
    }
91
92
    /**
93
     * @return ResolveInfo
94
     */
95
    public function getResolveInfo(): ResolveInfo
96
    {
97
        return $this->info;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function all(): array
104
    {
105
        return $this->arguments;
106
    }
107
108
    /**
109
     * @param string $argument
110
     * @param null $default
111
     * @return mixed|null
112
     */
113
    public function get(string $argument, $default = null)
114
    {
115
        return $this->arguments[$argument] ?? $default;
116
    }
117
118
    /**
119
     * @param string $argument
120
     * @return bool
121
     */
122
    public function has(string $argument): bool
123
    {
124
        return array_key_exists($argument, $this->arguments);
125
    }
126
127
    /**
128
     * TODO
129
     * @return iterable
130
     */
131
    public function getRelations(): iterable
132
    {
133
        throw new \LogicException(__METHOD__ . ' not implemented yet');
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getFieldName(): string
140
    {
141
        return $this->info->fieldName;
142
    }
143
144
    public function hasRelation(string $name): bool
145
    {
146
        throw new \LogicException(__METHOD__ . ' not implemented yet');
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getPath(): string
153
    {
154
        if ($this->path === null) {
155
            $path = $this->info->path;
156
            $path[count($path) - 1] = $this->getFieldName();
157
158
            // Remove array indexes
159
            $path = array_filter($path, 'is_string');
160
161
            // Remove empty values
162
            $path = array_filter($path, 'trim');
163
164
            $this->path = implode(Route::ACTION_DEPTH_DELIMITER, $path);
165
        }
166
167
        return $this->path;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    public function __debugInfo(): array
174
    {
175
        return [
176
            'path' => $this->path,
177
            'arguments' => $this->arguments
178
        ];
179
    }
180
181
    /**
182
     * @return string
183
     * @throws \LogicException
184
     */
185
    public function getAlias(): string
186
    {
187
        return $this->info->path[count($this->info->path) - 1];
188
    }
189
190
    /**
191
     * @return bool
192
     * @throws \LogicException
193
     */
194
    public function hasAlias(): bool
195
    {
196
        return $this->getAlias() !== $this->getFieldName();
197
    }
198
}
199