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

RouteEventManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 14
dl 0
loc 35
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exec() 0 4 2
A startWatching() 0 18 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