Completed
Push — master ( c45904...04afc3 )
by Iman
03:46
created

MatchedRoute::terminate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Imanghafoori\HeyMan\Plugins\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 53
    public function handle($request, Closure $next)
15
    {
16 53
        $this->writeDebugInfo();
17
18 53
        $route = $request->route();
19 53
        $matchedRoute = $this->getMatchedRouteInto($route);
20 53
        $matchedCallbacks = [];
21 53
        foreach ($matchedRoute as $info) {
22 53
            foreach (static::$chainData as $routeInfo => $callBacks) {
23 51
                if (Str::is($routeInfo, $info)) {
24 53
                    $matchedCallbacks[] = array_pop($callBacks);
25
                }
26
            }
27
        }
28
29 53
        $this->exec($matchedCallbacks, $route);
30
31 25
        return $next($request);
32
    }
33
34 53
    private function exec(array $closures, $route)
35
    {
36 53
        foreach (Arr::flatten($closures) as $closure) {
37 42
            $closure($route);
38
        }
39 25
    }
40
41 53
    private function getMatchedRouteInto($route)
42
    {
43
        $matchedRoute = [
44 53
            $route->getName(),
45 53
            $route->getActionName(),
46 53
            app('request')->method().$route->uri,
47
        ];
48
49 53
        return array_filter($matchedRoute);
50
    }
51
52 52
    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

52
    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...
53
    {
54 52
        if (! ($debug = app()['heyman_reaction_is_happened_in_debug'] ?? false)) {
55 27
            return;
56
        }
57
58 25
        $this->writeForDebugbar($debug);
59
60 25
        if (is_a($response, RedirectResponse::class)) {
61 3
            $s = session();
62 3
            $s->flash('heyman.debug.info', $debug);
63 3
            $s->reflash();
64 3
            $s->save();
65
        }
66 25
    }
67
68 53
    private function writeDebugInfo()
69
    {
70 53
        if ($debug = session()->get('heyman.debug.info')) {
71
            $this->writeForDebugbar($debug);
72
        }
73 53
    }
74
75 25
    private function writeForDebugbar($debug)
76
    {
77 25
        resolve('heyman.debugger')->addMessage('HeyMan Rule Matched in file: ');
78 25
        resolve('heyman.debugger')->addMessage($debug[0].' on line: '.$debug[1]);
79 25
        resolve('heyman.debugger')->addMessage($debug[2]);
80 25
    }
81
}
82