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

RouteEventManager::startWatching()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 1
nop 1
crap 4
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