Passed
Pull Request — master (#102)
by Iman
13:28 queued 06:13
created

MatchedRoute::exec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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