BuildingContext   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSchemaDefinition() 0 3 1
A buildQueryType() 0 4 2
A buildSubscriptionType() 0 4 2
A buildMutationType() 0 4 2
A buildDirectives() 0 21 3
A buildTypes() 0 5 1
A __construct() 0 8 1
1
<?php
2
3
namespace Digia\GraphQL\Schema\Building;
4
5
use Digia\GraphQL\Language\Node\DirectiveDefinitionNode;
6
use Digia\GraphQL\Language\Node\NamedTypeNodeInterface;
7
use Digia\GraphQL\Language\Node\SchemaDefinitionNode;
8
use Digia\GraphQL\Language\Node\TypeSystemDefinitionNodeInterface;
9
use Digia\GraphQL\Schema\DefinitionBuilderInterface;
10
use Digia\GraphQL\Schema\Resolver\ResolverRegistryInterface;
11
use Digia\GraphQL\Type\Definition\Directive;
12
use Digia\GraphQL\Type\Definition\TypeInterface;
13
use function Digia\GraphQL\Util\arraySome;
14
15
class BuildingContext implements BuildingContextInterface
16
{
17
    /**
18
     * @var ResolverRegistryInterface
19
     */
20
    protected $resolverRegistry;
21
22
    /**
23
     * @var DefinitionBuilderInterface
24
     */
25
    protected $definitionBuilder;
26
27
    /**
28
     * @var BuildInfo
29
     */
30
    protected $info;
31
32
    /**
33
     * BuilderContext constructor.
34
     * @param ResolverRegistryInterface  $resolverRegistry
35
     * @param DefinitionBuilderInterface $definitionBuilder
36
     * @param BuildInfo                  $info
37
     */
38
    public function __construct(
39
        ResolverRegistryInterface $resolverRegistry,
40
        DefinitionBuilderInterface $definitionBuilder,
41
        BuildInfo $info
42
    ) {
43
        $this->resolverRegistry  = $resolverRegistry;
44
        $this->definitionBuilder = $definitionBuilder;
45
        $this->info              = $info;
46
    }
47
48
    /**
49
     * @return TypeInterface|null
50
     */
51
    public function buildQueryType(): ?TypeInterface
52
    {
53
        $definition = $this->info->getOperationTypeDefinition('query');
54
        return null !== $definition ? $this->definitionBuilder->buildType($definition) : null;
0 ignored issues
show
Bug introduced by
It seems like $definition can also be of type Digia\GraphQL\Language\N...ationTypeDefinitionNode; however, parameter $node of Digia\GraphQL\Schema\Def...rInterface::buildType() does only seem to accept Digia\GraphQL\Language\Node\NamedTypeNodeInterface, 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

54
        return null !== $definition ? $this->definitionBuilder->buildType(/** @scrutinizer ignore-type */ $definition) : null;
Loading history...
introduced by
The condition null !== $definition is always true.
Loading history...
55
    }
56
57
    /**
58
     * @return TypeInterface|null
59
     */
60
    public function buildMutationType(): ?TypeInterface
61
    {
62
        $definition = $this->info->getOperationTypeDefinition('mutation');
63
        return null !== $definition ? $this->definitionBuilder->buildType($definition) : null;
0 ignored issues
show
introduced by
The condition null !== $definition is always true.
Loading history...
Bug introduced by
It seems like $definition can also be of type Digia\GraphQL\Language\N...ationTypeDefinitionNode; however, parameter $node of Digia\GraphQL\Schema\Def...rInterface::buildType() does only seem to accept Digia\GraphQL\Language\Node\NamedTypeNodeInterface, 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

63
        return null !== $definition ? $this->definitionBuilder->buildType(/** @scrutinizer ignore-type */ $definition) : null;
Loading history...
64
    }
65
66
    /**
67
     * @return TypeInterface|null
68
     */
69
    public function buildSubscriptionType(): ?TypeInterface
70
    {
71
        $definition = $this->info->getOperationTypeDefinition('subscription');
72
        return null !== $definition ? $this->definitionBuilder->buildType($definition) : null;
0 ignored issues
show
Bug introduced by
It seems like $definition can also be of type Digia\GraphQL\Language\N...ationTypeDefinitionNode; however, parameter $node of Digia\GraphQL\Schema\Def...rInterface::buildType() does only seem to accept Digia\GraphQL\Language\Node\NamedTypeNodeInterface, 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
        return null !== $definition ? $this->definitionBuilder->buildType(/** @scrutinizer ignore-type */ $definition) : null;
Loading history...
introduced by
The condition null !== $definition is always true.
Loading history...
73
    }
74
75
    /**
76
     * @return TypeInterface[]
77
     */
78
    public function buildTypes(): array
79
    {
80
        return \array_map(function (NamedTypeNodeInterface $definition) {
81
            return $this->definitionBuilder->buildType($definition);
82
        }, \array_values($this->info->getTypeDefinitionMap()));
83
    }
84
85
    /**
86
     * @return Directive[]
87
     */
88
    public function buildDirectives(): array
89
    {
90
        $directives = \array_map(function (DirectiveDefinitionNode $definition) {
91
            return $this->definitionBuilder->buildDirective($definition);
92
        }, $this->info->getDirectiveDefinitions());
93
94
        $specifiedDirectivesMap = [
95
            'skip'       => SkipDirective(),
96
            'include'    => IncludeDirective(),
97
            'deprecated' => DeprecatedDirective(),
98
        ];
99
100
        foreach ($specifiedDirectivesMap as $name => $directive) {
101
            if (!arraySome($directives, function (Directive $directive) use ($name) {
102
                return $directive->getName() === $name;
103
            })) {
104
                $directives[] = $directive;
105
            }
106
        }
107
108
        return $directives;
109
    }
110
111
    /**
112
     * @return SchemaDefinitionNode|null
113
     */
114
    public function getSchemaDefinition(): ?SchemaDefinitionNode
115
    {
116
        return $this->info->getSchemaDefinition();
117
    }
118
}
119