Completed
Push — master ( 3b79f3...426227 )
by Kirill
03:11
created

FieldBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResolver() 0 9 1
A build() 0 16 2
A buildFields() 0 9 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\Builders;
11
12
use GraphQL\Type\Definition\FieldDefinition;
13
use Railt\Adapters\Webonyx\FieldResolver;
14
use Railt\Adapters\Webonyx\Registry;
15
use Railt\Http\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Railt\Http\RequestInterface 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...
16
use Railt\Reflection\Contracts\Dependent\Field\HasFields;
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...pendent\Field\HasFields 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...
17
use Railt\Reflection\Contracts\Dependent\FieldDefinition as ReflectionField;
0 ignored issues
show
Bug introduced by
The type Railt\Reflection\Contrac...pendent\FieldDefinition 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...
18
19
/**
20
 * @property ReflectionField $reflection
21
 */
22
class FieldBuilder extends DependentDefinitionBuilder
23
{
24
    /**
25
     * @param HasFields $type
26
     * @param Registry $registry
27
     * @return array
28
     * @throws \InvalidArgumentException
29
     */
30
    public static function buildFields(HasFields $type, Registry $registry): array
31
    {
32
        $result = [];
33
34
        foreach ($type->getFields() as $field) {
35
            $result[$field->getName()] = (new static($field, $registry))->build();
36
        }
37
38
        return $result;
39
    }
40
41
    /**
42
     * @return FieldDefinition
43
     * @throws \InvalidArgumentException
44
     */
45
    public function build(): FieldDefinition
46
    {
47
        $config = [
48
            'name'        => $this->reflection->getName(),
49
            'description' => $this->reflection->getDescription(),
50
            'type'        => $this->buildType(),
51
            'args'        => ArgumentBuilder::buildArguments($this->reflection, $this->getRegistry()),
52
            'resolve'     => $this->getResolver(),
53
            // complexity
54
        ];
55
56
        if ($this->reflection->isDeprecated()) {
57
            $config['deprecationReason'] = $this->reflection->getDeprecationReason();
58
        }
59
60
        return FieldDefinition::create($config);
61
    }
62
63
    /**
64
     * @return \Closure|null
65
     */
66
    private function getResolver(): ?\Closure
67
    {
68
        /** @var RequestInterface $request */
69
        $request = $this->resolve(RequestInterface::class);
70
71
        /** @var FieldResolver $resolver */
72
        $resolver = $this->resolve(FieldResolver::class);
73
74
        return $resolver->getCallback($request, $this->reflection);
75
    }
76
}
77