Completed
Push — master ( 4ca583...a67af2 )
by Song
02:43
created

HasHooks::callDeleting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Concerns;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Symfony\Component\HttpFoundation\Response;
8
9
trait HasHooks
10
{
11
    /**
12
     * Supported hooks: submitted, editing, saving, saved, deleting, deleted.
13
     *
14
     * @var array
15
     */
16
    protected $hooks = [];
17
18
    /**
19
     * Initialization closure array.
20
     *
21
     * @var []Closure
22
     */
23
    protected static $initCallbacks;
24
25
    /**
26
     * Initialize with user pre-defined default disables, etc.
27
     *
28
     * @param Closure $callback
29
     */
30
    public static function init(Closure $callback = null)
31
    {
32
        static::$initCallbacks[] = $callback;
33
    }
34
35
    /**
36
     * Call the initialization closure array in sequence.
37
     */
38
    protected function callInitCallbacks()
39
    {
40
        if (empty(static::$initCallbacks)) {
41
            return;
42
        }
43
44
        foreach (static::$initCallbacks as $callback) {
45
            $callback($this);
46
        }
47
    }
48
49
    /**
50
     * Register a hook.
51
     *
52
     * @param string  $name
53
     * @param Closure $callback
54
     *
55
     * @return $this
56
     */
57
    protected function registerHook($name, Closure $callback)
58
    {
59
        $this->hooks[$name][] = $callback;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Call hooks by giving name.
66
     *
67
     * @param string $name
68
     * @param array  $parameters
69
     *
70
     * @return Response
71
     */
72
    protected function callHooks($name, $parameters = [])
73
    {
74
        $hooks = Arr::get($this->hooks, $name, []);
75
76
        foreach ($hooks as $func) {
77
            if (!$func instanceof Closure) {
78
                continue;
79
            }
80
81
            $response = call_user_func($func, $this, $parameters);
82
83
            if ($response instanceof Response) {
84
                return $response;
85
            }
86
        }
87
    }
88
89
    /**
90
     * Set after getting editing model callback.
91
     *
92
     * @param Closure $callback
93
     *
94
     * @return $this
95
     */
96
    public function editing(Closure $callback)
97
    {
98
        return $this->registerHook('editing', $callback);
99
    }
100
101
    /**
102
     * Set submitted callback.
103
     *
104
     * @param Closure $callback
105
     *
106
     * @return $this
107
     */
108
    public function submitted(Closure $callback)
109
    {
110
        return $this->registerHook('submitted', $callback);
111
    }
112
113
    /**
114
     * Set saving callback.
115
     *
116
     * @param Closure $callback
117
     *
118
     * @return $this
119
     */
120
    public function saving(Closure $callback)
121
    {
122
        return $this->registerHook('saving', $callback);
123
    }
124
125
    /**
126
     * Set saved callback.
127
     *
128
     * @param Closure $callback
129
     *
130
     * @return $this
131
     */
132
    public function saved(Closure $callback)
133
    {
134
        return $this->registerHook('saved', $callback);
135
    }
136
137
    /**
138
     * @param Closure $callback
139
     *
140
     * @return $this
141
     */
142
    public function deleting(Closure $callback)
143
    {
144
        return $this->registerHook('deleting', $callback);
145
    }
146
147
    /**
148
     * @param Closure $callback
149
     *
150
     * @return $this
151
     */
152
    public function deleted(Closure $callback)
153
    {
154
        return $this->registerHook('deleted', $callback);
155
    }
156
157
    /**
158
     * Call editing callbacks.
159
     *
160
     * @return mixed
161
     */
162
    protected function callEditing()
163
    {
164
        return $this->callHooks('editing');
165
    }
166
167
    /**
168
     * Call submitted callback.
169
     *
170
     * @return mixed
171
     */
172
    protected function callSubmitted()
173
    {
174
        return $this->callHooks('submitted');
175
    }
176
177
    /**
178
     * Call saving callback.
179
     *
180
     * @return mixed
181
     */
182
    protected function callSaving()
183
    {
184
        return $this->callHooks('saving');
185
    }
186
187
    /**
188
     * Callback after saving a Model.
189
     *
190
     * @return mixed|null
191
     */
192
    protected function callSaved()
193
    {
194
        return $this->callHooks('saved');
195
    }
196
197
    /**
198
     * Call hooks when deleting.
199
     *
200
     * @param mixed $id
201
     *
202
     * @return mixed
203
     */
204
    protected function callDeleting($id)
205
    {
206
        return $this->callHooks('deleting', $id);
207
    }
208
209
    /**
210
     * @return mixed
211
     */
212
    protected function callDeleted()
213
    {
214
        return $this->callHooks('deleted');
215
    }
216
}
217