GraphiQLTokenMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 8 4
1
<?php
2
3
namespace Digia\Lumen\GraphQL\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
/**
10
 * Class GraphiQLTokenMiddleware
11
 * @package Digia\Lumen\GraphQL\Http\Middleware
12
 */
13
class GraphiQLTokenMiddleware
14
{
15
16
    /**
17
     * @var boolean
18
     */
19
    private $enableGraphiql;
20
21
    /**
22
     * @var string|null
23
     */
24
    private $graphiqlToken;
25
26
    /**
27
     * GraphiQLTokenMiddleware constructor.
28
     *
29
     * @param bool        $enableGraphiql
30
     * @param string|null $graphiqlToken
31
     */
32
    public function __construct($enableGraphiql, $graphiqlToken)
33
    {
34
        $this->enableGraphiql = $enableGraphiql;
35
        $this->graphiqlToken  = $graphiqlToken;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     * @throws NotFoundHttpException if the interface is not enabled
41
     */
42
    public function handle(Request $request, Closure $next)
43
    {
44
        if ($this->enableGraphiql || ($this->graphiqlToken !== null && $request->get('token') === $this->graphiqlToken)) {
45
            return $next($request);
46
        }
47
48
        // Pretend the interface is not there if not enabled
49
        throw new NotFoundHttpException();
50
    }
51
}
52