Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

GraphQLDataCollector::collect()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.6666
cc 2
eloc 6
nc 3
nop 3
crap 6
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\InputObjectDefinition;
18
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
19
use Ynlo\GraphQLBundle\Definition\ObjectDefinition;
20
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
21
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
22
use Ynlo\GraphQLBundle\Extension\EndpointNotValidException;
23
use Ynlo\GraphQLBundle\Security\EndpointResolver;
24
25
class GraphQLDataCollector extends DataCollector
26
{
27
    /**
28
     * @var DefinitionRegistry
29
     */
30
    protected $registry;
31
32
    /**
33
     * @var EndpointResolver
34
     */
35
    protected $endpointResolver;
36
37
    /**
38
     * @var Endpoint
39
     */
40
    protected $endpoint;
41
42
    /**
43
     * GraphQLDataCollector constructor.
44
     *
45
     * @param DefinitionRegistry $registry
46
     * @param EndpointResolver   $endpointResolver
47
     */
48 22
    public function __construct(DefinitionRegistry $registry, EndpointResolver $endpointResolver)
49
    {
50 22
        $this->registry = $registry;
51 22
        $this->endpointResolver = $endpointResolver;
52 22
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 22
    public function getName()
58
    {
59 22
        return 'graphql';
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function reset()
66
    {
67
        $this->data = [];
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function collect(Request $request, Response $response, \Exception $exception = null)
74
    {
75
        try {
76
            $name = $this->endpointResolver->resolveEndpoint($request);
77
            $this->endpoint = $this->registry->getEndpoint($name);
78
            $this->data = [
79
                'endpoint' => $this->endpoint,
80
            ];
81
        } catch (EndpointNotValidException $exception) {
82
            //do nothing
83
        }
84
    }
85
86
    /**
87
     * @return Endpoint
88
     */
89
    public function getEndpoint()
90
    {
91
        return $this->data['endpoint'] ?? null;
92
    }
93
94
    public function isInputObject($definition)
95
    {
96
        return $definition instanceof InputObjectDefinition;
97
    }
98
99
    public function isInterface($definition)
100
    {
101
        return $definition instanceof InterfaceDefinition;
102
    }
103
104
    public function isObject($definition)
105
    {
106
        return $definition instanceof ObjectDefinition;
107
    }
108
109
    /**
110
     * @param array $data
111
     *
112
     * @return Data
113
     */
114
    public static function arrayToData(array $data)
115
    {
116
        return new Data($data);
117
    }
118
}
119