OptionalDebugBar::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace MaksimM\ConditionalDebugBar\Http\Middleware;
4
5
use Barryvdh\Debugbar\LaravelDebugbar;
6
use Illuminate\Contracts\Container\Container;
7
8
class OptionalDebugBar
9
{
10
    /**
11
     * The App container.
12
     *
13
     * @var Container
14
     */
15
    protected $container;
16
17
    /**
18
     * The URIs that should be excluded.
19
     *
20
     * @var array
21
     */
22
    protected $except = [];
23
24
    /**
25
     * Create a new middleware instance.
26
     *
27
     * @param Container $container
28
     */
29
    public function __construct(Container $container)
30
    {
31
        $this->container = $container;
32
        $this->except = config('debugbar.except') ?: [];
33
    }
34
35
    /**
36
     * Handle an incoming request.
37
     *
38
     * @param \Symfony\Component\HttpFoundation\Request $request
39
     * @param \Closure                                  $next
40
     *
41
     * @throws \Exception
42
     *
43
     * @return mixed
44
     */
45
    public function handle($request, \Closure $next)
46
    {
47
        if (class_exists(LaravelDebugbar::class)) {
48
            //except array option support
49
            if ($this->inExceptArray($request)) {
50
                return $next($request);
51
            }
52
            $debugBar = resolve(LaravelDebugbar::class);
53
            $bootValidator = resolve(config('conditional-debugbar.debugbar-boot-validator'));
54
            $debuggerPreviouslyEnabled = $debugBar->isEnabled();
55
            if ($debuggerPreviouslyEnabled && !$bootValidator->isInDebugMode()) {
0 ignored issues
show
Bug introduced by
The method isInDebugMode() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            if ($debuggerPreviouslyEnabled && !$bootValidator->/** @scrutinizer ignore-call */ isInDebugMode()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
                $debugBar->disable();
57
                session()->put('debugBarEnabled', false);
58
            } elseif (!$debuggerPreviouslyEnabled && $bootValidator->isInDebugMode()) {
59
                $debugBar->enable();
60
                session()->put('debugBarEnabled', true);
61
                $response = $next($request);
62
63
                return $debugBar->modifyResponse($request, $response);
64
            }
65
        }
66
67
        return $next($request);
68
    }
69
70
    /**
71
     * Determine if the request has a URI that should be ignored.
72
     *
73
     * @param \Illuminate\Http\Request $request
74
     *
75
     * @return bool
76
     */
77
    protected function inExceptArray($request)
78
    {
79
        foreach ($this->except as $except) {
80
            if ($except !== '/') {
81
                $except = trim($except, '/');
82
            }
83
84
            if ($request->is($except)) {
85
                return true;
86
            }
87
        }
88
89
        return false;
90
    }
91
}
92