Passed
Pull Request — master (#2956)
by Alan
04:34
created

GraphiQlAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\GraphQl\Action;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19
use Symfony\Component\Routing\RouterInterface;
20
use Twig\Environment as TwigEnvironment;
21
22
/**
23
 * GraphiQL entrypoint.
24
 *
25
 * @author Alan Poulain <[email protected]>
26
 */
27
final class GraphiQlAction
28
{
29
    private $twig;
30
    private $router;
31
    private $graphiqlEnabled;
32
    private $title;
33
34
    public function __construct(TwigEnvironment $twig, RouterInterface $router, bool $graphiqlEnabled = false, string $title = '')
35
    {
36
        $this->twig = $twig;
37
        $this->router = $router;
38
        $this->graphiqlEnabled = $graphiqlEnabled;
39
        $this->title = $title;
40
    }
41
42
    public function __invoke(Request $request): Response
43
    {
44
        if ($this->graphiqlEnabled) {
45
            return new Response($this->twig->render('@ApiPlatform/Graphiql/index.html.twig', [
46
                'title' => $this->title,
47
                'graphiql_data' => ['entrypoint' => $this->router->generate('api_graphql_entrypoint')],
48
            ]));
49
        }
50
51
        throw new BadRequestHttpException('GraphiQL is not enabled.');
52
    }
53
}
54