Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
created

CleanUpDefinitionPlugin::used()   C

Complexity

Conditions 11
Paths 27

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 18
nc 27
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*******************************************************************************
4
 *  This file is part of the GraphQL Bundle package.
5
 *
6
 *  (c) YnloUltratech <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 ******************************************************************************/
11
12
namespace Ynlo\GraphQLBundle\Definition\Plugin;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
16
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
17
use Ynlo\GraphQLBundle\Definition\ImplementorInterface;
18
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
19
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
20
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
21
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
22
23
/**
24
 * This plugin remove non used definitions
25
 */
26
class CleanUpDefinitionPlugin extends AbstractDefinitionPlugin
27
{
28
    /**
29
     * @var Endpoint
30
     */
31
    protected $endpoint;
32
33
    protected $used = [];
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function buildConfig(ArrayNodeDefinition $root): void
39
    {
40
        $root
41
            ->info('Remove non used definitions')
42
            ->scalarPrototype();
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function configureEndpoint(Endpoint $endpoint): void
49
    {
50
        if ($endpoint->getName() === DefinitionRegistry::DEFAULT_ENDPOINT) {
51
            return;
52
        }
53
54
        if ($endpoint->getName() !== 'pos') {
55
            return;
56
        }
57
58
        $this->endpoint = $endpoint;
59
60
        $this->processOperations($endpoint->allQueries());
61
        $this->processOperations($endpoint->allMutations());
62
63
        foreach ($endpoint->allTypes() as $type) {
64
            if (!\in_array($type->getName(), $this->used)) {
65
                $endpoint->removeType($type->getName());
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param QueryDefinition[] $operations
72
     */
73
    protected function processOperations($operations)
74
    {
75
        foreach ($operations as $operation) {
76
            $this->used($operation->getType());
77
            foreach ($operation->getArguments() as $argument) {
78
                $this->used($argument->getType());
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param string|DefinitionInterface $definition
85
     */
86
    protected function used($definition): void
87
    {
88
        if (\is_string($definition)) {
89
            if (!$this->endpoint->hasType($definition)) {
90
                return;
91
            }
92
93
            $definition = $this->endpoint->getType($definition);
94
        }
95
96
        if (\in_array($definition->getName(), $this->used)) {
97
            return;
98
        }
99
100
        $this->used[] = $definition->getName();
101
        if ($definition instanceof FieldsAwareDefinitionInterface) {
102
            foreach ($definition->getFields() as $field) {
103
                $this->used($field->getType());
104
                foreach ($field->getArguments() as $argument) {
105
                    $this->used($argument->getType());
106
                }
107
            }
108
        }
109
110
        if ($definition instanceof InterfaceDefinition) {
111
            foreach ($definition->getImplementors() as $implementor) {
112
                $this->used($implementor);
113
            }
114
        }
115
116
        if ($definition instanceof ImplementorInterface) {
117
            foreach ($definition->getInterfaces() as $interface) {
118
                $this->used($interface);
119
            }
120
        }
121
    }
122
}
123