Completed
Push — master ( d4e593...c1d04e )
by Iman
20:25 queued 13:41
created

RouteHooks::whenYouSendPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
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 37
    public function whenYouVisitUrl(...$url)
16
    {
17 37
        return $this->authorizeURL($url, 'GET');
18
    }
19
20
    /**
21
     * @param mixed ...$url
22
     *
23
     * @return YouShouldHave
24
     */
25 1
    public function whenYouSendPost(...$url)
26
    {
27 1
        return $this->authorizeURL($url, 'POST');
28
    }
29
30
    /**
31
     * @param mixed ...$url
32
     *
33
     * @return YouShouldHave
34
     */
35 1
    public function whenYouSendPatch(...$url)
36
    {
37 1
        return $this->authorizeURL($url, 'PATCH');
38
    }
39
40
    /**
41
     * @param mixed ...$url
42
     *
43
     * @return YouShouldHave
44
     */
45 1
    public function whenYouSendPut(...$url)
46
    {
47 1
        return $this->authorizeURL($url, 'PUT');
48
    }
49
50
    /**
51
     * @param mixed ...$url
52
     *
53
     * @return YouShouldHave
54
     */
55 1
    public function whenYouSendDelete(...$url)
56
    {
57 1
        return $this->authorizeURL($url, 'DELETE');
58
    }
59
60
    /**
61
     * @param mixed ...$routeName
62
     *
63
     * @return YouShouldHave
64
     */
65 8
    public function whenYouReachRoute(...$routeName)
66
    {
67 8
        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 9
    public function whenYouCallAction(...$action)
76
    {
77
        $addNamespace = function ($action) {
78 9
            if ($action = ltrim($action, '\\')) {
79 9
                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 9
        };
84
85 9
        $action = array_map($addNamespace, $this->normalizeInput($action));
86
87 9
        return $this->authorizeRoute('actions', $action);
88
    }
89
90
    /**
91
     * @param $target
92
     * @param $value
93
     *
94
     * @return YouShouldHave
95
     */
96 54
    private function authorizeRoute($target, $value)
97
    {
98 54
        $this->chain->eventManager = app(RouterEventManager::class)->init($target, $value);
99
100 54
        return app(YouShouldHave::class);
101
    }
102
103
    /**
104
     * @param $url
105
     * @param $verb
106
     *
107
     * @return \Imanghafoori\HeyMan\YouShouldHave
108
     */
109 41
    private function authorizeURL($url, $verb): \Imanghafoori\HeyMan\YouShouldHave
110
    {
111
        $removeSlash = function ($url) use ($verb) {
112 41
            return $verb.ltrim($url, '/');
113 41
        };
114
115 41
        $url = array_map($removeSlash, $this->normalizeInput($url));
116
117 41
        return $this->authorizeRoute('urls', $url);
118
    }
119
}
120