Passed
Push — master ( 429b06...f0c7c5 )
by Rafael
08:46
created

ExplorerController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
dl 0
loc 107
ccs 0
cts 12
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A graphiQLAction() 0 21 2
C explorerAction() 0 58 14
A getAuthenticationProvider() 0 9 2
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\Controller;
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
/**
22
 * Class ExplorerController
23
 */
24
class ExplorerController extends Controller
25
{
26
    /**
27
     * @param Request $request
28
     *
29
     * @return Response
30
     */
31
    public function explorerAction(Request $request)
32
    {
33
        $form = null;
34
        $authenticationError = null;
35
        $isAuthenticated = false;
36
        $config = $this->getParameter('graphql.graphiql');
37
38
        if ($provider = $this->getAuthenticationProvider()) {
39
            if ($provider->requireUserData()) {
40
                $builder = $this->createFormBuilder();
41
                $provider->buildUserForm($builder);
42
                $form = $builder->getForm();
43
            }
44
45
            if ($request->get('logout')) {
46
                $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
                    $provider->login($form);
57
                } elseif (!$form) {
58
                    $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 = $provider->isAuthenticated();
69
        } else {
70
            if ($config['authentication']['required']) {
71
                throw new \RuntimeException('Configure a valid provider to use GraphiQL with authentication');
72
            }
73
        }
74
75
        return $this->render(
76
            $config['template'],
77
            [
78
                'form' => $form ? $form->createView() : null,
79
                'isAuthenticated' => $isAuthenticated,
80
                'title' => $config['title'],
81
                'authenticationEnabled' => (bool) $provider,
82
                'authenticationRequired' => $config['authentication']['required'],
83
                'authenticationError' => $authenticationError,
84
                '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...
85
                'loginMessage' => $config['authentication']['login_message'],
86
                'dataWarningMessage' => $config['data_warning_message'],
87
                'dataWarningDismissible' => $config['data_warning_dismissible'],
88
                'dataWarningStyle' => $config['data_warning_style'],
89
            ]
90
        );
91
    }
92
93
    /**
94
     * @return Response
95
     */
96
    public function graphiQLAction()
97
    {
98
        $request = new GraphiQLRequest(
99
            $this->generateUrl('api_root'),
100
            [],
101
            [
102
                'Accept' => 'application/json',
103
                'Content-Type' => 'application/json',
104
            ]
105
        );
106
        if ($provider = $this->getAuthenticationProvider()) {
107
            $provider->prepareRequest($request);
108
        }
109
110
        $params = [
111
            'url' => $request->getUrl(),
112
            'method' => 'post',
113
            'headers' => $request->getHeaders(),
114
        ];
115
116
        return $this->render('@YnloGraphQL/graphiql.twig', $params);
117
    }
118
119
    /**
120
     * @return GraphiQLAuthenticationProviderInterface|object|null
121
     */
122
    protected function getAuthenticationProvider()
123
    {
124
        $providerName = $this->getParameter('graphql.graphiql_auth_provider');
125
126
        if ($providerName) {
127
            return $this->get($providerName);
128
        }
129
130
        return null;
131
    }
132
}
133