Completed
Pull Request — master (#31)
by Iman
10:47 queued 08:04
created

RouteHooks   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 128
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A whenYouSendPut() 0 3 1
A whenYouSendPatch() 0 3 1
A whenYouReachRoute() 0 3 1
A whenYouSendDelete() 0 3 1
A whenYouVisitUrl() 0 3 1
A whenYouSendPost() 0 3 1
A whenYouCallAction() 0 5 1
A authorizeRoute() 0 5 1
A authorizeURL() 0 3 1
A normalizeUrl() 0 7 1
A normalizeAction() 0 11 2
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): YouShouldHave
16
    {
17 35
        return $this->authorizeURL($url, 'GET');
18
    }
19
20
    /**
21
     * @param mixed ...$url
22
     *
23
     * @return YouShouldHave
24
     */
25 1
    public function whenYouSendPost(...$url): YouShouldHave
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): YouShouldHave
36
    {
37 1
        return $this->authorizeURL($url, 'PATCH');
38
    }
39
40
    /**
41
     * @param mixed ...$url
42
     *
43
     * @return YouShouldHave
44
     */
45 2
    public function whenYouSendPut(...$url): YouShouldHave
46
    {
47 2
        return $this->authorizeURL($url, 'PUT');
48
    }
49
50
    /**
51
     * @param mixed ...$url
52
     *
53
     * @return YouShouldHave
54
     */
55 1
    public function whenYouSendDelete(...$url): YouShouldHave
56
    {
57 1
        return $this->authorizeURL($url, 'DELETE');
58
    }
59
60
    /**
61
     * @param mixed ...$routeName
62
     *
63
     * @return YouShouldHave
64
     */
65 11
    public function whenYouReachRoute(...$routeName): YouShouldHave
66
    {
67 11
        return $this->authorizeRoute($this->normalizeInput($routeName));
0 ignored issues
show
Bug introduced by
The method normalizeInput() does not exist on Imanghafoori\HeyMan\Hooks\RouteHooks. Did you maybe mean normalizeUrl()? ( 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($this->/** @scrutinizer ignore-call */ normalizeInput($routeName));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
    }
69
70
    /**
71
     * @param mixed ...$action
72
     *
73
     * @return YouShouldHave
74
     */
75 10
    public function whenYouCallAction(...$action): YouShouldHave
76
    {
77 10
        $action = $this->normalizeAction($action);
78
79 10
        return $this->authorizeRoute($action);
80
    }
81
82
    /**
83
     * @param $value
84
     *
85
     * @return YouShouldHave
86
     */
87 58
    private function authorizeRoute($value): YouShouldHave
88
    {
89 58
        $this->chain->eventManager = app(RouterEventManager::class)->init($value);
90
91 58
        return app(YouShouldHave::class);
92
    }
93
94
    /**
95
     * @param $url
96
     * @param $verb
97
     *
98
     * @return YouShouldHave
99
     */
100 40
    private function authorizeURL($url, $verb): YouShouldHave
101
    {
102 40
        return $this->authorizeRoute($this->normalizeUrl($url, $verb));
103
    }
104
105
    /**
106
     * @param $url
107
     * @param $verb
108
     *
109
     * @return array
110
     */
111 40
    private function normalizeUrl($url, $verb): array
112
    {
113
        $removeSlash = function ($url) use ($verb) {
114 40
            return $verb.ltrim($url, '/');
115 40
        };
116
117 40
        return array_map($removeSlash, $this->normalizeInput($url));
118
    }
119
120
    /**
121
     * @param $action
122
     *
123
     * @return array
124
     */
125 10
    private function normalizeAction($action): array
126
    {
127
        $addNamespace = function ($action) {
128 10
            if ($action = ltrim($action, '\\')) {
129 10
                return $action;
130
            }
131
132
            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

132
            return app()->/** @scrutinizer ignore-call */ getNamespace().'\\Http\\Controllers\\'.$action;
Loading history...
133 10
        };
134
135 10
        return array_map($addNamespace, $this->normalizeInput($action));
136
    }
137
}
138