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 ( 4720e0...497033 )
by Jérémiah
20:36
created

BatchParser::getParsedBody()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 7
Ratio 24.14 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 29
ccs 7
cts 7
cp 1
rs 8.5806
cc 4
eloc 18
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Overblog\GraphQLBundle\Request;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
7
8
class BatchParser implements ParserInterface
9
{
10
    use UploadParserTrait;
11
12
    const PARAM_ID = 'id';
13
14
    private static $queriesDefaultValue = [
15
        self::PARAM_ID => null,
16
        self::PARAM_QUERY => null,
17
        self::PARAM_VARIABLES => null,
18
    ];
19
20
    /**
21
     * @param Request $request
22
     *
23 6
     * @return array
24
     */
25
    public function parse(Request $request)
26 6
    {
27
        // Extracts the GraphQL request parameters
28 4
        $queries = $this->getParsedBody($request);
29 1
30
        if (empty($queries)) {
31
            throw new BadRequestHttpException('Must provide at least one valid query.');
32 3
        }
33 3
34
        foreach ($queries as $i => &$query) {
35 3
            $query = array_filter($query) + self::$queriesDefaultValue;
36 3
37
            if (!is_string($query[static::PARAM_QUERY])) {
38
                throw new BadRequestHttpException(sprintf('%s is not a valid query', json_encode($query[static::PARAM_QUERY])));
39
            }
40 2
        }
41
42
        return $queries;
43
    }
44
45
    /**
46
     * Gets the body from the request.
47
     *
48
     * @param Request $request
49
     *
50 6
     * @return array
51
     */
52 6
    private function getParsedBody(Request $request)
53
    {
54
        $contentType = explode(';', $request->headers->get('content-type'), 2)[0];
55 6
56 1
        // JSON object
57
        switch ($contentType) {
58 View Code Duplication
            case static::CONTENT_TYPE_JSON:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59 5
                $parsedBody = json_decode($request->getContent(), true);
60
61 5
                if (JSON_ERROR_NONE !== json_last_error()) {
62 1
                    throw new BadRequestHttpException('POST body sent invalid JSON');
63
                }
64
                break;
65 4
66
            case static::CONTENT_TYPE_FORM_DATA:
67
                $parsedBody = $this->handleUploadedFiles($request->request->all(), $request->files->all());
68
                break;
69
70
            default:
71
                throw new BadRequestHttpException(sprintf(
72
                    'Batching parser only accepts "%s" or "%s" content-type but got %s.',
73
                    static::CONTENT_TYPE_JSON,
74
                    static::CONTENT_TYPE_FORM_DATA,
75
                    json_encode($contentType)
76
                ));
77
        }
78
79
        return $parsedBody;
80
    }
81
}
82