Passed
Push — master ( 16cc56...d9bd57 )
by Iman
02:32
created

RouteEventManager::exec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies\Routes;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Routing\Events\RouteMatched;
8
9
final class RouteEventManager
10
{
11
    /**
12
     * @param $chainData
13
     *
14
     * @return void
15
     */
16
    public function startWatching($chainData)
17
    {
18 50
        Route::matched(function (RouteMatched $eventObj) use ($chainData) {
19
            $matchedRoute = [
20 50
                $eventObj->route->getName(),
21 50
                $eventObj->route->getActionName(),
22 50
                $eventObj->request->method().$eventObj->route->uri,
23
            ];
24 50
            $matchedCallbacks = [];
25 50
            foreach (array_filter($matchedRoute) as $info) {
26 50
                foreach ($chainData as $routeInfo => $callBacks) {
27 48
                    if (Str::is($routeInfo, $info)) {
28 50
                        $matchedCallbacks[] = array_pop($callBacks);
29
                    }
30
                }
31
            }
32
33 50
            $this->exec($matchedCallbacks);
34 50
        });
35 50
    }
36
37
    /**
38
     * @param array $closures
39
     */
40 50
    private function exec(array $closures)
41
    {
42 50
        foreach (array_flatten($closures) as $closure) {
43 44
            $closure();
44
        }
45 25
    }
46
}
47