Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
created

ExplorerController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
dl 0
loc 103
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B graphiQL() 0 26 3
A __construct() 0 4 1
C explorer() 0 63 15
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 Symfony\Component\Routing\Exception\RouteNotFoundException;
18
use Ynlo\GraphQLBundle\GraphiQL\AuthenticationFailedException;
19
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLAuthenticationProviderInterface;
20
use Ynlo\GraphQLBundle\GraphiQL\GraphiQLRequest;
21
22
class ExplorerController extends AbstractController
23
{
24
    private $config;
25
    private $provider;
26
27
    public function __construct(array $config, GraphiQLAuthenticationProviderInterface $provider = null)
28
    {
29
        $this->config = $config;
30
        $this->provider = $provider;
31
    }
32
33
    public function explorer(Request $request): Response
34
    {
35
        $form = null;
36
        $authenticationError = null;
37
        $isAuthenticated = false;
38
39
        if ($this->provider) {
40
            if ($this->provider->requireUserData()) {
41
                $builder = $this->createFormBuilder();
42
                $this->provider->buildUserForm($builder);
43
                $form = $builder->getForm();
44
            }
45
46
            if ($request->get('logout')) {
47
                $this->provider->logout();
48
49
                return $this->redirectToRoute('api_explore');
50
            }
51
52
            $form->handleRequest($request);
53
            $response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
54
55
            try {
56
                if ($form && $form->isSubmitted() && $form->isValid()) {
57
                    $this->provider->login($form);
58
                } elseif (!$form) {
59
                    $this->provider->login();
60
                }
61
            } catch (AuthenticationFailedException $exception) {
62
                if ($form) {
63
                    $form->addError(new FormError($exception->getMessage()));
64
                } else {
65
                    $authenticationError = $exception->getMessage();
66
                }
67
            }
68
69
            $isAuthenticated = $this->provider->isAuthenticated();
70
        }
71
72
        if ($this->config['documentation']['link'] ?? null) {
73
            try {
74
                $this->config['documentation']['link'] = $this->container
75
                    ->get('router')
76
                    ->generate($this->config['documentation']['link']);
77
            } catch (RouteNotFoundException $exception) {
78
                //do nothing, use the link as is
79
            }
80
        }
81
82
        return $this->render($this->config['template'], [
83
            'form' => $form ? $form->createView() : null,
84
            'favicon' => $this->config['favicon'] ?? null,
85
            'documentation' => $this->config['documentation'] ?? [],
86
            'isAuthenticated' => $isAuthenticated,
87
            'title' => $this->config['title'],
88
            'authenticationEnabled' => (bool) $this->provider,
89
            'authenticationRequired' => $this->config['authentication']['required'],
90
            'authenticationError' => $authenticationError,
91
            'hasAuthenticationError' => $authenticationError || ($form && $form->getErrors(true)->count()),
92
            'loginMessage' => $this->config['authentication']['login_message'],
93
            'dataWarningMessage' => $this->config['data_warning_message'],
94
            'dataWarningDismissible' => $this->config['data_warning_dismissible'],
95
            'dataWarningStyle' => $this->config['data_warning_style'],
96
        ]);
97
    }
98
99
    public function graphiQL()
100
    {
101
        $request = new GraphiQLRequest(
102
            $this->generateUrl('api_root'),
103
            [],
104
            [
105
                'Accept' => 'application/json',
106
                'Content-Type' => 'application/json',
107
            ]
108
        );
109
        if ($this->provider) {
110
            $this->provider->prepareRequest($request);
111
        }
112
113
        $defaultQuery = $this->config['default_query'];
114
        if ($defaultQuery) {
115
            //escape any simple quote
116
            //and convert newlines to javascript multiline concatenation
117
            $defaultQuery = str_replace(["'", "\n"], ["\'", "\\n' + \n'"], $this->config['default_query']);
118
        }
119
120
        return $this->render('@YnloGraphQL/graphiql.html.twig', [
121
            'url' => $request->getUrl(),
122
            'method' => 'post',
123
            'headers' => $request->getHeaders(),
124
            'defaultQuery' => $defaultQuery,
125
        ]);
126
    }
127
}
128