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
07:01 queued 27s
created

GraphController::batchEndpointAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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