Completed
Push — master ( 0ae3fd...89a74b )
by Iman
05:30
created

Forget::aboutFetching()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan;
4
5
use Imanghafoori\HeyMan\WatchingStrategies\EloquentEventsManager;
6
use Imanghafoori\HeyMan\WatchingStrategies\EventManager;
7
use Imanghafoori\HeyMan\WatchingStrategies\RouterEventManager;
8
use Imanghafoori\HeyMan\WatchingStrategies\ViewEventManager;
9
10
class Forget
11
{
12
    /**
13
     * @param $url
14
     *
15
     * @return array
16 9
     */
17
    private function normalizeInput(array $url): array
18 9
    {
19
        return is_array($url[0]) ? $url[0] : $url;
20
    }
21
22
    /**
23
     * Forget constructor.
24 9
     */
25
    public function __construct()
26 9
    {
27
    }
28 4
29
    public function aboutView(...$view)
30 4
    {
31 4
        resolve(ViewEventManager::class)->forgetAbout($this->normalizeInput($view));
32
    }
33
34
    public function aboutSaving(...$model)
35
    {
36
        resolve(EloquentEventsManager::class)->forgetAbout($this->normalizeInput($model), 'saving');
37
    }
38
39
    public function aboutDeleting(...$model)
40
    {
41
        resolve(EloquentEventsManager::class)->forgetAbout($this->normalizeInput($model), 'deleting');
42
    }
43
44
    public function aboutFetching(...$model)
45
    {
46
        resolve(EloquentEventsManager::class)->forgetAbout($this->normalizeInput($model), 'retrieved');
47
    }
48
49
    public function aboutCreating(...$model)
50
    {
51
        resolve(EloquentEventsManager::class)->forgetAbout($this->normalizeInput($model), 'creating');
52
    }
53 2
54
    public function aboutUpdating(...$model)
55 2
    {
56 2
        resolve(EloquentEventsManager::class)->forgetAbout($this->normalizeInput($model), 'updating');
57
    }
58 1
59
    public function aboutModel(...$model)
60 1
    {
61 1
        resolve(EloquentEventsManager::class)->forgetAbout($this->normalizeInput($model));
62
    }
63 1
64
    public function aboutEvent(...$events)
65 1
    {
66 1
        resolve(EventManager::class)->forgetAbout($this->normalizeInput($events));
67
    }
68 1
69
    public function aboutUrl(...$urls)
70 1
    {
71 1
        resolve(RouterEventManager::class)->forgetAbout($this->normalizeUrl($urls, 'GET'));
72
    }
73
74
    public function aboutRoute(...$route)
75
    {
76
        resolve(RouterEventManager::class)->forgetAbout($this->normalizeInput($route));
77
    }
78
79 1
    public function aboutAction(...$actions)
80
    {
81
        resolve(RouterEventManager::class)->forgetAbout($this->normalizeAction($actions));
82 1
    }
83 1
84
    /**
85 1
     * @param $url
86
     * @param $verb
87
     *
88
     * @return array
89
     */
90
    private function normalizeUrl($url, $verb): array
91
    {
92
        $removeSlash = function ($url) use ($verb) {
93 1
            return $verb.ltrim($url, '/');
94
        };
95
96 1
        return array_map($removeSlash, $this->normalizeInput($url));
97 1
    }
98
99
    /**
100
     * @param $action
101 1
     *
102
     * @return array
103 1
     */
104
    private function normalizeAction($action): array
105 1
    {
106
        $addNamespace = function ($action) {
107
            if ($action = ltrim($action, '\\')) {
108
                return $action;
109
            }
110
111
            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

111
            return app()->/** @scrutinizer ignore-call */ getNamespace().'\\Http\\Controllers\\'.$action;
Loading history...
112
        };
113
114
        $action = array_map($addNamespace, $this->normalizeInput($action));
115
116
        return $action;
117
    }
118
}
119