Passed
Push — master ( f061a0...b4f1c6 )
by Yonel Ceruto
05:53
created

ExplorerController::explorer()   C

Complexity

Conditions 13
Paths 21

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 36
nc 21
nop 1
dl 0
loc 51
ccs 0
cts 44
cp 0
crap 182
rs 5.6547
c 0
b 0
f 0

How to fix   Long Method    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 Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\Form\FormError;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Ynlo\GraphQLBundle\GraphiQL\AuthenticationFailedException;
18
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLAuthenticationProviderInterface;
19
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLRequest;
20
21
class ExplorerController extends AbstractController
22
{
23
    private $config;
24
    private $provider;
25
26
    public function __construct(array $config, GraphiQLAuthenticationProviderInterface $provider = null)
27
    {
28
        $this->config = $config;
29
        $this->provider = $provider;
30
    }
31
32
    public function explorer(Request $request): Response
33
    {
34
        $form = null;
35
        $authenticationError = null;
36
        $isAuthenticated = false;
37
38
        if ($this->provider) {
39
            if ($this->provider->requireUserData()) {
40
                $builder = $this->createFormBuilder();
41
                $this->provider->buildUserForm($builder);
42
                $form = $builder->getForm();
43
            }
44
45
            if ($request->get('logout')) {
46
                $this->provider->logout();
47
48
                return $this->redirectToRoute('api_explore');
49
            }
50
51
            $form->handleRequest($request);
52
            $response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
53
54
            try {
55
                if ($form && $form->isSubmitted() && $form->isValid()) {
56
                    $this->provider->login($form);
57
                } elseif (!$form) {
58
                    $this->provider->login();
59
                }
60
            } catch (AuthenticationFailedException $exception) {
61
                if ($form) {
62
                    $form->addError(new FormError($exception->getMessage()));
63
                } else {
64
                    $authenticationError = $exception->getMessage();
65
                }
66
            }
67
68
            $isAuthenticated = $this->provider->isAuthenticated();
69
        }
70
71
        return $this->render($this->config['template'], [
72
            'form' => $form ? $form->createView() : null,
73
            'isAuthenticated' => $isAuthenticated,
74
            'title' => $this->config['title'],
75
            'authenticationEnabled' => (bool) $this->provider,
76
            'authenticationRequired' => $this->config['authentication']['required'],
77
            'authenticationError' => $authenticationError,
78
            'hasAuthenticationError' => $authenticationError || ($form && $form->getErrors(true)->count()),
0 ignored issues
show
Bug Best Practice introduced by
The expression $authenticationError of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
            'loginMessage' => $this->config['authentication']['login_message'],
80
            'dataWarningMessage' => $this->config['data_warning_message'],
81
            'dataWarningDismissible' => $this->config['data_warning_dismissible'],
82
            'dataWarningStyle' => $this->config['data_warning_style'],
83
        ]);
84
    }
85
86
    public function graphiQL()
87
    {
88
        $request = new GraphiQLRequest(
89
            $this->generateUrl('api_root'),
90
            [],
91
            [
92
                'Accept' => 'application/json',
93
                'Content-Type' => 'application/json',
94
            ]
95
        );
96
        if ($this->provider) {
97
            $this->provider->prepareRequest($request);
98
        }
99
100
        return $this->render('@YnloGraphQL/graphiql.html.twig', [
101
            'url' => $request->getUrl(),
102
            'method' => 'post',
103
            'headers' => $request->getHeaders(),
104
        ]);
105
    }
106
}
107