Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

GraphQLEndpointController::rootAction()   D

Complexity

Conditions 10
Paths 113

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 13.3199

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 113
nop 1
dl 0
loc 45
ccs 19
cts 28
cp 0.6786
crap 13.3199
rs 4.6769
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Controller;
12
13
use GraphQL\Error\Debug;
14
use GraphQL\GraphQL;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\HttpException;
20
use Ynlo\GraphQLBundle\Schema\SchemaCompiler;
21
22
/**
23
 * Class GraphQLEndpointController
24
 */
25
class GraphQLEndpointController extends Controller
26
{
27
    /**
28
     * @param Request $request
29
     *
30
     * @return JsonResponse
31
     */
32 14
    public function rootAction(Request $request): JsonResponse
33
    {
34 14
        $debugMode = $this->container->getParameter('kernel.debug');
35
36 14
        if (!$debugMode && $request->getMethod() !== Request::METHOD_POST) {
37
            throw new HttpException(Response::HTTP_BAD_REQUEST, 'The method should be POST to talk with GraphQL API');
38
        }
39
40 14
        $schema = $this->get(SchemaCompiler::class)->compile();
41
42 14
        $input = json_decode($request->getContent(), true);
43 14
        $query = $input['query'];
44 14
        $variableValues = isset($input['variables']) ? $input['variables'] : null;
45 14
        $operationName = isset($input['operationName']) ? $input['operationName'] : null;
46
47
        try {
48 14
            $schema->assertValid();
49 14
            $context = null;
50 14
            $result = GraphQL::executeQuery($schema, $query, $context, null, $variableValues, $operationName);
51
52 14
            $debug = false;
53 14
            if ($debugMode) {
54 14
                $debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE;
55
            }
56
57 14
            $output = $result->toArray($debug);
58 14
            $statusCode = Response::HTTP_OK;
59
60 14
            if (isset($output['errors'])) {
61 14
                $statusCode = Response::HTTP_BAD_REQUEST;
62
            }
63
        } catch (\Exception $e) {
64
            if ($this->has('logger')) {
65
                $this->get('logger')->error($e->getMessage(), $e->getTrace());
66
            }
67
            $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR;
68
            $output['errors']['message'] = $e->getMessage();
69
            $output['errors']['category'] = 'internal';
70
71
            if ($debugMode) {
72
                $output['errors']['trace'] = $e->getTraceAsString();
73
            }
74
        }
75
76 14
        return JsonResponse::create($output, $statusCode);
77
    }
78
}
79