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 — 0.13 (#823)
by Evgenij
41:16 queued 43s
created

Parser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 50
c 2
b 0
f 0
dl 0
loc 116
ccs 48
cts 48
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 7 1
B getParsedBody() 0 44 8
A getParams() 0 37 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Request;
6
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
9
10
class Parser implements ParserInterface
11
{
12
    use UploadParserTrait;
13
14
    /**
15
     * @param Request $request
16
     *
17
     * @return array
18
     */
19 101
    public function parse(Request $request): array
20
    {
21
        // Extracts the GraphQL request parameters
22 101
        $parsedBody = $this->getParsedBody($request);
23 99
        $data = $this->getParams($request, $parsedBody);
24
25 97
        return $data;
26
    }
27
28
    /**
29
     * Gets the body from the request based on Content-Type header.
30
     *
31
     * @param Request $request
32
     *
33
     * @return array
34
     */
35 101
    private function getParsedBody(Request $request): array
36
    {
37 101
        $body = $request->getContent();
38 101
        $method = $request->getMethod();
39 101
        $contentType = \explode(';', (string) $request->headers->get('content-type'), 2)[0];
40
41
        switch ($contentType) {
42
            // Plain string
43 101
            case static::CONTENT_TYPE_GRAPHQL:
44 3
                $parsedBody = [static::PARAM_QUERY => $body];
45 3
                break;
46
47
            // JSON object
48 98
            case static::CONTENT_TYPE_JSON:
49 4
                if (empty($body)) {
50 2
                    if (Request::METHOD_GET === $method) {
51 1
                        $parsedBody = [];
52 1
                        break;
53
                    }
54 1
                    throw new BadRequestHttpException('The request content body must not be empty when using json content type request.');
55
                }
56
57 2
                $parsedBody = \json_decode($body, true);
58
59 2
                if (\JSON_ERROR_NONE !== \json_last_error()) {
60 1
                    throw new BadRequestHttpException('POST body sent invalid JSON');
61
                }
62 1
                break;
63
64
            // URL-encoded query-string
65 94
            case static::CONTENT_TYPE_FORM:
66 1
                $parsedBody = $request->request->all();
67 1
                break;
68
69 93
            case static::CONTENT_TYPE_FORM_DATA:
70 6
                $parsedBody = $this->handleUploadedFiles($request->request->all(), $request->files->all());
71 6
                break;
72
73
            default:
74 87
                $parsedBody = [];
75 87
                break;
76
        }
77
78 99
        return $parsedBody;
79
    }
80
81
    /**
82
     * Gets the GraphQL parameters from the request.
83
     *
84
     * @param Request $request
85
     * @param array   $data
86
     *
87
     * @return array
88
     */
89 99
    private function getParams(Request $request, array $data = []): array
90
    {
91
        // Add default request parameters
92 99
        $data = \array_filter($data) + [
93 99
                static::PARAM_QUERY => null,
94 99
                static::PARAM_VARIABLES => null,
95 99
                static::PARAM_OPERATION_NAME => null,
96
            ];
97
98
        // Use all query parameters, since starting from Symfony 6 there will be an exception accessing array parameters
99 99
        // via request->query->get(key), and another exception accessing non-array parameter via request->query->all(key)
100
        $queryParameters = $request->query->all();
101
102 99
        // Override request using query-string parameters
103 99
        $query = $queryParameters[static::PARAM_QUERY] ?? $data[static::PARAM_QUERY];
104 99
        $variables = $queryParameters[static::PARAM_VARIABLES] ?? $data[static::PARAM_VARIABLES];
105
        $operationName = $queryParameters[static::PARAM_OPERATION_NAME] ?? $data[static::PARAM_OPERATION_NAME];
106
107 99
        // `query` parameter is mandatory.
108 1
        if (empty($query)) {
109
            throw new BadRequestHttpException('Must provide query parameter');
110
        }
111
112
        // Variables can be defined using a JSON-encoded object.
113 98
        // If the parsing fails, an exception will be thrown.
114 33
        if (\is_string($variables)) {
115
            $variables = \json_decode($variables, true);
116 33
117 1
            if (\JSON_ERROR_NONE !== \json_last_error()) {
118
                throw new BadRequestHttpException('Variables are invalid JSON');
119
            }
120
        }
121
122 97
        return [
123 97
            static::PARAM_QUERY => $query,
124 97
            static::PARAM_VARIABLES => $variables,
125
            static::PARAM_OPERATION_NAME => $operationName,
126
        ];
127
    }
128
}
129