Completed
Push — master ( dfc95c...bc4718 )
by Iman
11:46 queued 08:45
created

RouteHooks   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 10
eloc 19
dl 0
loc 109
ccs 26
cts 27
cp 0.963
rs 10
c 0
b 0
f 0

9 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 whenYouCallAction() 0 13 2
A whenYouSendPost() 0 3 1
A authorizeRoute() 0 5 1
A authorizeURL() 0 9 1
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 40
    public function whenYouVisitUrl(...$url)
16
    {
17 40
        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 2
    public function whenYouSendPut(...$url)
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)
56
    {
57 1
        return $this->authorizeURL($url, 'DELETE');
58
    }
59
60
    /**
61
     * @param mixed ...$routeName
62
     *
63
     * @return YouShouldHave
64
     */
65 10
    public function whenYouReachRoute(...$routeName)
66
    {
67 10
        return $this->authorizeRoute($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($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($action);
88
    }
89
90
    /**
91
     * @param $value
92
     *
93
     * @return YouShouldHave
94
     */
95 58
    private function authorizeRoute($value)
96
    {
97 58
        $this->chain->eventManager = app(RouterEventManager::class)->init($value);
98
99 58
        return app(YouShouldHave::class);
100
    }
101
102
    /**
103
     * @param $url
104
     * @param $verb
105
     *
106
     * @return \Imanghafoori\HeyMan\YouShouldHave
107
     */
108 45
    private function authorizeURL($url, $verb): YouShouldHave
109
    {
110
        $removeSlash = function ($url) use ($verb) {
111 45
            return $verb.ltrim($url, '/');
112 45
        };
113
114 45
        $url = array_map($removeSlash, $this->normalizeInput($url));
115
116 45
        return $this->authorizeRoute($url);
117
    }
118
}
119