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

Passed
Push — master ( a79a7e...138901 )
by Jérémiah
18:05
created

GraphController::addCORSHeadersIfNeeded()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 2
crap 3
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 52
    public function __construct(
38
        GraphQLRequest\ParserInterface $batchParser,
39
        GraphQLRequest\Executor $requestExecutor,
40
        GraphQLRequest\ParserInterface $requestParser,
41
        $shouldHandleCORS,
42
        $graphQLBatchingMethod
43
    ) {
44 52
        $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 52
        $this->requestExecutor = $requestExecutor;
46 52
        $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 52
        $this->shouldHandleCORS = $shouldHandleCORS;
48 52
        $this->useApolloBatchingMethod = 'apollo' === $graphQLBatchingMethod;
49 52
    }
50
51
    /**
52
     * @param Request     $request
53
     * @param string|null $schemaName
54
     *
55
     * @return JsonResponse|Response
56
     */
57 44
    public function endpointAction(Request $request, $schemaName = null)
58
    {
59 44
        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 8
    public function batchEndpointAction(Request $request, $schemaName = null)
69
    {
70 8
        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 52
    private function createResponse(Request $request, $schemaName, $batched)
81
    {
82 52
        if ('OPTIONS' === $request->getMethod()) {
83 2
            $response = new Response('', 200);
84
        } else {
85 50
            if (!in_array($request->getMethod(), ['POST', 'GET'])) {
86 1
                return new Response('', 405);
87
            }
88 49
            $payload = $this->processQuery($request, $schemaName, $batched);
89 38
            $response = new JsonResponse($payload, 200);
90
        }
91 40
        $this->addCORSHeadersIfNeeded($response, $request);
92
93 40
        return $response;
94
    }
95
96 40
    private function addCORSHeadersIfNeeded(Response $response, Request $request)
97
    {
98 40
        if ($this->shouldHandleCORS && $request->headers->has('Origin')) {
99 3
            $response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'), true);
100 3
            $response->headers->set('Access-Control-Allow-Credentials', 'true', true);
101 3
            $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization', true);
102 3
            $response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, GET, POST', true);
103 3
            $response->headers->set('Access-Control-Max-Age', 3600, true);
104
        }
105 40
    }
106
107
    /**
108
     * @param Request     $request
109
     * @param string|null $schemaName
110
     * @param bool        $batched
111
     *
112
     * @return array
113
     */
114 49
    private function processQuery(Request $request, $schemaName, $batched)
115
    {
116 49
        if ($batched) {
117 7
            $payload = $this->processBatchQuery($request, $schemaName);
118
        } else {
119 42
            $payload = $this->processNormalQuery($request, $schemaName);
120
        }
121
122 38
        return $payload;
123
    }
124
125
    /**
126
     * @param Request     $request
127
     * @param string|null $schemaName
128
     *
129
     * @return array
130
     */
131 7
    private function processBatchQuery(Request $request, $schemaName = null)
132
    {
133 7
        $queries = $this->batchParser->parse($request);
134 3
        $payloads = [];
135
136 3
        foreach ($queries as $query) {
137 3
            $payload = $this->requestExecutor
138 3
                ->execute($schemaName, ['query' => $query['query'], 'variables' => $query['variables']])
139 3
                ->toArray();
140 3
            if (!$this->useApolloBatchingMethod) {
141 3
                $payload = ['id' => $query['id'], 'payload' => $payload];
142
            }
143 3
            $payloads[] = $payload;
144
        }
145
146 3
        return $payloads;
147
    }
148
149
    /**
150
     * @param Request     $request
151
     * @param string|null $schemaName
152
     *
153
     * @return array
154
     */
155 42
    private function processNormalQuery(Request $request, $schemaName = null)
156
    {
157 42
        $params = $this->requestParser->parse($request);
158
159 38
        return $this->requestExecutor->execute($schemaName, $params)->toArray();
160
    }
161
}
162