Completed
Push — master ( 210a64...050109 )
by Iman
05:21
created

MatchedRoute::writeDebugInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies\Routes;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Illuminate\Http\RedirectResponse;
9
10
class MatchedRoute
11
{
12
    public static $chainData;
13
14
    /**
15
     * Handle an incoming request.
16
     *
17
     * @param \Illuminate\Http\Request $request
18
     * @param \Closure                 $next
19
     *
20
     * @return mixed
21
     */
22 52
    public function handle($request, Closure $next)
23
    {
24 52
        $this->writeDebugInfo();
25
26 52
        $route = $request->route();
27 52
        $matchedRoute = $this->getMatchedRouteInto($route);
28 52
        $matchedCallbacks = [];
29 52
        foreach ($matchedRoute as $info) {
30 52
            foreach (static::$chainData as $routeInfo => $callBacks) {
31 50
                if (Str::is($routeInfo, $info)) {
32 52
                    $matchedCallbacks[] = array_pop($callBacks);
33
                }
34
            }
35
        }
36
37 52
        $this->exec($matchedCallbacks);
38
39 25
        return $next($request);
40
    }
41
42
    /**
43
     * @param array $closures
44
     */
45 52
    private function exec(array $closures)
46
    {
47 52
        foreach (Arr::flatten($closures) as $closure) {
48 41
            $closure();
49
        }
50 25
    }
51
52
    /**
53
     * @param $route
54
     *
55
     * @return array
56
     */
57 52
    private function getMatchedRouteInto($route): array
58
    {
59
        $matchedRoute = [
60 52
            $route->getName(),
61 52
            $route->getActionName(),
62 52
            app('request')->method().$route->uri,
63
        ];
64
65 52
        return array_filter($matchedRoute);
66
    }
67
68 50
    public function terminate($request, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

68
    public function terminate(/** @scrutinizer ignore-unused */ $request, $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70 50
        if (! ($debug = app()['heyman_reaction_is_happened_in_debug'] ?? false)) {
71 26
            return;
72
        }
73
74 24
        $this->writeForDebugbar($debug);
75
76 24
        if (is_a($response, RedirectResponse::class)) {
77 3
            $s = session();
78 3
            $s->flash('heyman.debug.info', $debug);
79 3
            $s->reflash();
80 3
            $s->save();
81
        }
82 24
    }
83
84 52
    private function writeDebugInfo()
85
    {
86 52
        if ($debug = session()->get('heyman.debug.info')) {
87
            $this->writeForDebugbar($debug);
88
        }
89 52
    }
90
91
    /**
92
     * @param bool $debug
93
     */
94 24
    private function writeForDebugbar($debug)
95
    {
96 24
        resolve('heyman.debugger')->addMessage('HeyMan Rule Matched in file: ');
97 24
        resolve('heyman.debugger')->addMessage($debug[0].' on line: '.$debug[1]);
98 24
        resolve('heyman.debugger')->addMessage($debug[2]);
99 24
    }
100
}
101