Completed
Push — master ( 7a107a...5cd32c )
by Rafael
04:36
created

GraphQLDataCollector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 18.75%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 101
ccs 6
cts 32
cp 0.1875
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEndpoint() 0 3 1
A isInputObject() 0 3 1
A getName() 0 3 1
A isObject() 0 3 1
A reset() 0 3 1
A arrayToData() 0 3 1
A isTypeAvailable() 0 3 1
A collect() 0 9 2
A isInterface() 0 3 1
A isOperationAvailable() 0 7 2
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\DataCollector;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
16
use Symfony\Component\VarDumper\Cloner\Data;
17
use Ynlo\GraphQLBundle\Definition\DefinitionInterface;
18
use Ynlo\GraphQLBundle\Definition\InputObjectDefinition;
19
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
20
use Ynlo\GraphQLBundle\Definition\MutationDefinition;
21
use Ynlo\GraphQLBundle\Definition\ObjectDefinition;
22
use Ynlo\GraphQLBundle\Definition\QueryDefinition;
23
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
24
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
25
use Ynlo\GraphQLBundle\Extension\EndpointNotValidException;
26
use Ynlo\GraphQLBundle\Security\EndpointResolver;
27
28
class GraphQLDataCollector extends DataCollector
29
{
30
    /**
31
     * @var DefinitionRegistry
32
     */
33
    protected $registry;
34
35
    /**
36
     * @var EndpointResolver
37
     */
38
    protected $endpointResolver;
39
40
    /**
41
     * GraphQLDataCollector constructor.
42
     *
43
     * @param DefinitionRegistry $registry
44
     * @param EndpointResolver   $endpointResolver
45
     */
46 22
    public function __construct(DefinitionRegistry $registry, EndpointResolver $endpointResolver)
47
    {
48 22
        $this->registry = $registry;
49 22
        $this->endpointResolver = $endpointResolver;
50 22
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 22
    public function getName()
56
    {
57 22
        return 'graphql';
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function reset()
64
    {
65
        $this->data = [];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function collect(Request $request, Response $response, \Exception $exception = null)
72
    {
73
        try {
74
            $name = $this->endpointResolver->resolveEndpoint($request);
75
            $this->data = [
76
                'defaultEndpoint' => $this->registry->getEndpoint(),
77
                'endpoint' => $this->registry->getEndpoint($name),
78
            ];
79
        } catch (EndpointNotValidException $exception) {
80
            //do nothing
81
        }
82
    }
83
84
    public function isOperationAvailable(QueryDefinition $definition)
85
    {
86
        if ($definition instanceof MutationDefinition) {
87
            return $this->data['endpoint']->hasMutation($definition->getName());
88
        }
89
90
        return $this->data['endpoint']->hasQuery($definition->getName());
91
    }
92
93
    public function isTypeAvailable(DefinitionInterface $definition)
94
    {
95
        return $this->data['endpoint']->hasType($definition->getName());
96
    }
97
98
    /**
99
     * @return Endpoint
100
     */
101
    public function getEndpoint()
102
    {
103
        return $this->data['defaultEndpoint'] ?? null;
104
    }
105
106
    public function isInputObject($definition)
107
    {
108
        return $definition instanceof InputObjectDefinition;
109
    }
110
111
    public function isInterface($definition)
112
    {
113
        return $definition instanceof InterfaceDefinition;
114
    }
115
116
    public function isObject($definition)
117
    {
118
        return $definition instanceof ObjectDefinition;
119
    }
120
121
    /**
122
     * @param array $data
123
     *
124
     * @return Data
125
     */
126
    public static function arrayToData(array $data)
127
    {
128
        return new Data($data);
129
    }
130
}
131