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
Pull Request — master (#208)
by Renato
08:09
created

GraphController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
crap 1
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
    /**
14
     * @var GraphQLRequest\BatchParser
15
     */
16
    private $batchParser;
17
18
    /**
19
     * @var GraphQLRequest\Executor
20
     */
21
    private $requestExecutor;
22
23
    /**
24
     * @var GraphQLRequest\Parser
25
     */
26
    private $requestParser;
27
28
    /**
29
     * @var bool
30
     */
31
    private $shouldHandleCORS;
32
33
    /**
34
     * @var string
35
     */
36
    private $graphQLBatchingMethod;
37
38 9
    public function __construct(
39
        GraphQLRequest\BatchParser $batchParser,
40
        GraphQLRequest\Executor $requestExecutor,
41
        GraphQLRequest\Parser $requestParser,
42
        $shouldHandleCORS,
43
        $graphQLBatchingMethod
44
    ) {
45 9
        $this->batchParser = $batchParser;
46 9
        $this->requestExecutor = $requestExecutor;
47 9
        $this->requestParser = $requestParser;
48 9
        $this->shouldHandleCORS = $shouldHandleCORS;
49 9
        $this->graphQLBatchingMethod = $graphQLBatchingMethod;
50 9
    }
51
52 30
    public function endpointAction(Request $request, $schemaName = null)
53
    {
54 30
        return $this->createResponse($request, $schemaName, false);
55
    }
56
57 7
    public function batchEndpointAction(Request $request, $schemaName = null)
58
    {
59 7
        return $this->createResponse($request, $schemaName, true);
60
    }
61
62 37
    private function createResponse(Request $request, $schemaName, $batched)
63
    {
64 37
        if ('OPTIONS' === $request->getMethod()) {
65 2
            $response = new Response('', 200);
66
        } else {
67 35
            if (!in_array($request->getMethod(), ['POST', 'GET'])) {
68 1
                return new Response('', 405);
69
            }
70
71 34
            if ($batched) {
72 6
                $payload = $this->processBatchQuery($request, $schemaName);
73
            } else {
74 28
                $payload = $this->processNormalQuery($request, $schemaName);
75
            }
76
77 25
            $response = new JsonResponse($payload, 200);
78
        }
79
80 27
        if ($this->shouldHandleCORS && $request->headers->has('Origin')) {
81 3
            $response->headers->set('Access-Control-Allow-Origin', $request->headers->get('Origin'), true);
82 3
            $response->headers->set('Access-Control-Allow-Credentials', 'true', true);
83 3
            $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Authorization', true);
84 3
            $response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, GET, POST', true);
85 3
            $response->headers->set('Access-Control-Max-Age', 3600, true);
86
        }
87
88 27
        return $response;
89
    }
90
91 6
    private function processBatchQuery(Request $request, $schemaName = null)
92
    {
93 6
        $queries = $this->batchParser->parse($request);
94 2
        $apolloBatching = 'apollo' === $this->graphQLBatchingMethod;
95 2
        $payloads = [];
96
97 2
        foreach ($queries as $query) {
98 2
            $payloadResult = $this->requestExecutor->execute(
99
                [
100 2
                    'query' => $query['query'],
101 2
                    'variables' => $query['variables'],
102
                ],
103 2
                [],
104 2
                $schemaName
105
            );
106 2
            $payloads[] = $apolloBatching ? $payloadResult->toArray() : ['id' => $query['id'], 'payload' => $payloadResult->toArray()];
107
        }
108
109 2
        return $payloads;
110
    }
111
112 28
    private function processNormalQuery(Request $request, $schemaName = null)
113
    {
114 28
        $params = $this->requestParser->parse($request);
115
116 24
        return $this->requestExecutor->execute($params, [], $schemaName)->toArray();
117
    }
118
}
119