Completed
Push — master ( 0df42c...ab08ca )
by Iman
10:11
created

RouterEventManager::wrapForIgnorance()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies;
4
5
use Illuminate\Support\Str;
6
7
class RouterEventManager
8
{
9
    private $routeInfo;
10
11
    private $routeChains = [];
12
13 45
    public function findMatchingCallbacks(array $matchedRoute): array
14
    {
15 45
        $matchedCallbacks = [];
16 45
        foreach ($this->routeChains as $routeInfo => $callBacks) {
17 44
            foreach ($matchedRoute as $info) {
18 44
                if (Str::is($routeInfo, $info)) {
19 44
                    $matchedCallbacks[] = $this->wrapCallbacksForIgnore($callBacks);
20
                }
21
            }
22
        }
23
24 45
        return $matchedCallbacks;
25
    }
26
27
    /**
28
     * @param $callbacks
29
     *
30
     * @return array
31
     */
32
    private function wrapCallbacksForIgnore(array $callbacks): array
33
    {
34 56
        return array_map(function ($callback) {
35
            return $this->wrapForIgnorance($callback);
36 56
        }, $callbacks);
37
    }
38 56
39
    /**
40
     * RouterEventManager constructor.
41
     *
42
     * @param $routeInfo
43
     *
44 48
     * @return RouterEventManager
45
     */
46 48
    public function init(array $routeInfo): RouterEventManager
47 48
    {
48
        $this->routeInfo = $routeInfo;
49 48
50
        return $this;
51
    }
52
53
    /**
54
     * @param $callback
55
     */
56 41
    public function startGuarding(callable $callback)
57
    {
58
        foreach ($this->routeInfo as $routeInfo) {
59
            $this->routeChains[$routeInfo][] = $callback;
60 41
        }
61 32
    }
62
63 41
    private function wrapForIgnorance(callable $callback): callable
64 41
    {
65
        return function () use ($callback) {
66
            if (! config('heyman_ignore_route', false)) {
67
                $callback();
68
            }
69
        };
70
    }
71
}
72