Completed
Push — master ( 300eda...15c856 )
by Rafael
09:26
created

ReorderDefinitionsPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sortQueries() 0 14 3
A configureEndpoint() 0 4 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Plugin;
12
13
use Ynlo\GraphQLBundle\Definition\MutationDefinition;
14
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
15
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
16
17
/**
18
 * Reorder queries & mutations by name based on related node
19
 */
20
class ReorderDefinitionsPlugin extends AbstractDefinitionPlugin
21
{
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function configureEndpoint(Endpoint $endpoint): void
26
    {
27
        $endpoint->setQueries($this->sortQueries($endpoint->allQueries()));
28
        $endpoint->setMutations($this->sortQueries($endpoint->allMutations()));
29
    }
30
31
    /**
32
     * @param QueryDefinition[]|MutationDefinition[] $queries
33
     */
34
    private function sortQueries($queries): array
35
    {
36
        $sortedQueries = [];
37
        foreach ($queries as $query) {
38
            $name = $query->getName();
39
            $node = $query->getType();
40
            if ($query->hasMeta('node')) {
41
                $node = $query->getMeta('node');
42
            }
43
            $sortedQueries[$node.'_'.$name] = $query;
44
        }
45
        ksort($sortedQueries);
46
47
        return $sortedQueries;
48
    }
49
}
50