Completed
Pull Request — master (#13)
by Rafael
06:46
created

CleanUpDefinitionPlugin::configureEndpoint()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 2
    public function configureEndpoint(Endpoint $endpoint): void
49
    {
50 2
        if ($endpoint->getName() === DefinitionRegistry::DEFAULT_ENDPOINT) {
51 1
            return;
52
        }
53
54 1
        $this->used = [];
55 1
        $this->endpoint = $endpoint;
56
57 1
        $this->processOperations($endpoint->allQueries());
58 1
        $this->processOperations($endpoint->allMutations());
59 1
        $this->processOperations($endpoint->allSubscriptions());
60
61 1
        foreach ($endpoint->allTypes() as $type) {
62 1
            if (!\in_array($type->getName(), $this->used)) {
63 1
                $endpoint->removeType($type->getName());
64
            }
65
        }
66 1
    }
67
68
    /**
69
     * @param QueryDefinition[] $operations
70
     */
71 1
    protected function processOperations($operations)
72
    {
73 1
        foreach ($operations as $operation) {
74 1
            $this->used($operation->getType());
75 1
            foreach ($operation->getArguments() as $argument) {
76
                $this->used($argument->getType());
77
            }
78
        }
79 1
    }
80
81
    /**
82
     * @param string|DefinitionInterface $definition
83
     */
84 1
    protected function used($definition): void
85
    {
86 1
        if (!$definition) {
87
            return;
88
        }
89
90 1
        if (\is_string($definition)) {
91 1
            if (!$this->endpoint->hasType($definition)) {
92 1
                return;
93
            }
94
95 1
            $definition = $this->endpoint->getType($definition);
96
        }
97
98 1
        if (\in_array($definition->getName(), $this->used)) {
99 1
            return;
100
        }
101
102 1
        $this->used[] = $definition->getName();
103 1
        if ($definition instanceof FieldsAwareDefinitionInterface) {
104 1
            foreach ($definition->getFields() as $field) {
105 1
                $this->used($field->getType());
106 1
                foreach ($field->getArguments() as $argument) {
107 1
                    $this->used($argument->getType());
108
                }
109
            }
110
        }
111
112 1
        if ($definition instanceof InterfaceDefinition) {
113 1
            foreach ($definition->getImplementors() as $implementor) {
114 1
                $this->used($implementor);
115
            }
116
        }
117
118 1
        if ($definition instanceof ImplementorInterface) {
119 1
            foreach ($definition->getInterfaces() as $interface) {
120 1
                $this->used($interface);
121
            }
122
        }
123 1
    }
124
}
125