Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#333)
by Jérémiah
08:28 queued 05:48
created

GraphController::processNormalQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Overblog\GraphQLBundle\Controller;
4
5
use Overblog\GraphQLBundle\Request as GraphQLRequest;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class GraphController
11
{
12
    /**
13
     * @var GraphQLRequest\BatchParser
14
     */
15
    private $batchParser;
16
17
    /**
18
     * @var GraphQLRequest\Executor
19
     */
20
    private $requestExecutor;
21
22
    /**
23
     * @var GraphQLRequest\Parser
24
     */
25
    private $requestParser;
26
27
    /**
28
     * @var bool
29
     */
30
    private $shouldHandleCORS;
31
32
    /**
33
     * @var bool
34
     */
35
    private $useApolloBatchingMethod;
36
37
    public function __construct(
38
        GraphQLRequest\ParserInterface $batchParser,
39
        GraphQLRequest\Executor $requestExecutor,
40
        GraphQLRequest\ParserInterface $requestParser,
41
        $shouldHandleCORS,
42
        $graphQLBatchingMethod
43
    ) {
44
        $this->batchParser = $batchParser;
0 ignored issues
show
Documentation Bug introduced by
$batchParser is of type object<Overblog\GraphQLB...equest\ParserInterface>, but the property $batchParser was declared to be of type object<Overblog\GraphQLB...le\Request\BatchParser>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
45
        $this->requestExecutor = $requestExecutor;
46
        $this->requestParser = $requestParser;
0 ignored issues
show
Documentation Bug introduced by
$requestParser is of type object<Overblog\GraphQLB...equest\ParserInterface>, but the property $requestParser was declared to be of type object<Overblog\GraphQLBundle\Request\Parser>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
        $this->shouldHandleCORS = $shouldHandleCORS;
48
        $this->useApolloBatchingMethod = 'apollo' === $graphQLBatchingMethod;
49
    }
50
51
    /**
52
     * @param Request     $request
53
     * @param string|null $schemaName
54
     *
55
     * @return JsonResponse|Response
56
     */
57
    public function endpointAction(Request $request, $schemaName = null)
58
    {
59
        return $this->createResponse($request, $schemaName, false);
60
    }
61
62
    /**
63
     * @param Request     $request
64
     * @param string|null $schemaName
65
     *
66
     * @return JsonResponse|Response
67
     */
68
    public function batchEndpointAction(Request $request, $schemaName = null)
69
    {
70
        return $this->createResponse($request, $schemaName, true);
71
    }
72
73
    /**
74
     * @param Request     $request
75
     * @param string|null $schemaName
76
     * @param bool        $batched
77
     *
78
     * @return JsonResponse|Response
79
     */
80
    private function createResponse(Request $request, $schemaName, $batched)
81
    {
82
        if ('OPTIONS' === $request->getMethod()) {
83
            $response = new Response('', 200);
84
        } else {
85
            if (!in_array($request->getMethod(), ['POST', 'GET'])) {
86
                return new Response('', 405);
87
            }
88
            $payload = $this->processQuery($request, $schemaName, $batched);
89
            $response = new JsonResponse($payload, 200);
90
        }
91
        $this->addCORSHeadersIfNeeded($response, $request);
92
93
        return $response;
94
    }
95
96
    private function addCORSHeadersIfNeeded(Response $response, Request $request)
97
    {
98
        if ($this->shouldHandleCORS && $request->headers->has('Origin')) {
99
            $response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'), true);
100
            $response->headers->set('Access-Control-Allow-Credentials', 'true', true);
101
            $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization', true);
102
            $response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, GET, POST', true);
103
            $response->headers->set('Access-Control-Max-Age', 3600, true);
104
        }
105
    }
106
107
    /**
108
     * @param Request     $request
109
     * @param string|null $schemaName
110
     * @param bool        $batched
111
     *
112
     * @return array
113
     */
114
    private function processQuery(Request $request, $schemaName, $batched)
115
    {
116
        if ($batched) {
117
            $payload = $this->processBatchQuery($request, $schemaName);
118
        } else {
119
            $payload = $this->processNormalQuery($request, $schemaName);
120
        }
121
122
        return $payload;
123
    }
124
125
    /**
126
     * @param Request     $request
127
     * @param string|null $schemaName
128
     *
129
     * @return array
130
     */
131
    private function processBatchQuery(Request $request, $schemaName = null)
132
    {
133
        $queries = $this->batchParser->parse($request);
134
        $payloads = [];
135
136
        foreach ($queries as $query) {
137
            $payload = $this->requestExecutor
138
                ->execute($schemaName, ['query' => $query['query'], 'variables' => $query['variables']])
139
                ->toArray();
140
            if (!$this->useApolloBatchingMethod) {
141
                $payload = ['id' => $query['id'], 'payload' => $payload];
142
            }
143
            $payloads[] = $payload;
144
        }
145
146
        return $payloads;
147
    }
148
149
    /**
150
     * @param Request     $request
151
     * @param string|null $schemaName
152
     *
153
     * @return array
154
     */
155
    private function processNormalQuery(Request $request, $schemaName = null)
156
    {
157
        $params = $this->requestParser->parse($request);
158
159
        return $this->requestExecutor->execute($schemaName, $params)->toArray();
160
    }
161
}
162