Completed
Push — master ( e22a75...ab6153 )
by Iman
04:58
created

RouterEventManager::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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 $target;
10
11
    private $value;
12
13
    private $routeNames = [];
14
15
    private $actions = [];
16
17
    private $urls = [];
18
19
    /**
20
     * RouteConditionApplier constructor.
21
     *
22
     * @param $target
23
     * @param $value
24
     */
25 38
    public function init($target, $value)
26
    {
27 38
        $this->target = $target;
28 38
        $this->value = $value;
29
30 38
        return $this;
31
    }
32
33 31
    public function getUrls($url)
34
    {
35 31
        return $this->resolveCallback($url, 'urls');
36
    }
37
38 19
    public function getRouteNames($routeName)
39
    {
40 19
        return $this->resolveCallback($routeName, 'routeNames');
41
    }
42
43 17
    public function getActions($action)
44
    {
45 17
        return $this->resolveCallback($action, 'actions');
46
    }
47
48
    /**
49
     * @param $callback
50
     */
51 30
    public function startGuarding(callable $callback)
52
    {
53 30
        foreach ($this->value as $value) {
54 30
            $this->{$this->target}[$value] = $callback;
55
        }
56 30
    }
57
58
    /**
59
     * @param $action
60
     * @param $type
61
     *
62
     * @return \Closure
63
     */
64 31
    private function resolveCallback($action, $type): \Closure
65
    {
66 31
        if (array_key_exists($action, $this->{$type})) {
67 24
            return $this->{$type}[$action];
68
        }
69
70 20
        foreach ($this->{$type} as $pattern => $callback) {
71 8
            if (Str::is($pattern, $action)) {
72 8
                return $callback;
73
            }
74
        }
75
76
        return function () {
77 19
        };
78
    }
79
}
80