Completed
Push — master ( 280d59...d4e593 )
by Iman
10:04
created

RouteHooks::whenYouReachRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\Hooks;
4
5
use Imanghafoori\HeyMan\WatchingStrategies\RouterEventManager;
6
use Imanghafoori\HeyMan\YouShouldHave;
7
8
trait RouteHooks
9
{
10
    /**
11
     * @param mixed ...$url
12
     *
13
     * @return YouShouldHave
14
     */
15 35
    public function whenYouVisitUrl(...$url)
16
    {
17
        return $this->authorizeURL($url, 'GET');
18 35
    }
19 35
20
    /**
21 35
     * @param mixed ...$url
22
     *
23 35
     * @return YouShouldHave
24
     */
25
    public function whenYouSendPost(...$url)
26
    {
27
        return $this->authorizeURL($url, 'POST');
28
    }
29
30
    /**
31 8
     * @param mixed ...$url
32
     *
33 8
     * @return YouShouldHave
34
     */
35
    public function whenYouSendPatch(...$url)
36
    {
37
        return $this->authorizeURL($url, 'PATCH');
38
    }
39
40
    /**
41 9
     * @param mixed ...$url
42
     *
43
     * @return YouShouldHave
44 9
     */
45 9
    public function whenYouSendPut(...$url)
46
    {
47
        return $this->authorizeURL($url, 'PUT');
48
    }
49 9
50
    /**
51 9
     * @param mixed ...$url
52
     *
53 9
     * @return YouShouldHave
54
     */
55
    public function whenYouSendDelete(...$url)
56
    {
57
        return $this->authorizeURL($url, 'DELETE');
58
    }
59
60
    /**
61
     * @param mixed ...$routeName
62 48
     *
63
     * @return YouShouldHave
64 48
     */
65
    public function whenYouReachRoute(...$routeName)
66 48
    {
67
        return $this->authorizeRoute('routeNames', $this->normalizeInput($routeName));
0 ignored issues
show
Bug introduced by
It seems like normalizeInput() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        return $this->authorizeRoute('routeNames', $this->/** @scrutinizer ignore-call */ normalizeInput($routeName));
Loading history...
68
    }
69
70
    /**
71
     * @param mixed ...$action
72
     *
73
     * @return YouShouldHave
74
     */
75
    public function whenYouCallAction(...$action)
76
    {
77
        $addNamespace = function ($action) {
78
            if ($action = ltrim($action, '\\')) {
79
                return $action;
80
            }
81
82
            return app()->getNamespace().'\\Http\\Controllers\\'.$action;
0 ignored issues
show
introduced by
The method getNamespace() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
            return app()->/** @scrutinizer ignore-call */ getNamespace().'\\Http\\Controllers\\'.$action;
Loading history...
83
        };
84
85
        $action = array_map($addNamespace, $this->normalizeInput($action));
86
87
        return $this->authorizeRoute('actions', $action);
88
    }
89
90
    /**
91
     * @param $target
92
     * @param $value
93
     *
94
     * @return YouShouldHave
95
     */
96
    private function authorizeRoute($target, $value)
97
    {
98
        $this->chain->eventManager = app(RouterEventManager::class)->init($target, $value);
99
100
        return app(YouShouldHave::class);
101
    }
102
103
    /**
104
     * @param $url
105
     * @param $verb
106
     * @return \Imanghafoori\HeyMan\YouShouldHave
107
     */
108
    private function authorizeURL($url, $verb): \Imanghafoori\HeyMan\YouShouldHave
109
    {
110
        $removeSlash = function ($url) use ($verb) {
111
            return $verb.ltrim($url, '/');
112
        };
113
114
        $url = array_map($removeSlash, $this->normalizeInput($url));
115
116
        return $this->authorizeRoute('urls', $url);
117
    }
118
}
119