Completed
Push — master ( c633e4...51e624 )
by Song
02:23
created

HasHooks::saving()   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;
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
     * Register a hook.
20
     *
21
     * @param string $name
22
     * @param Closure $callback
23
     *
24
     * @return $this
25
     */
26
    protected function registerHook($name, Closure $callback)
27
    {
28
        $this->hooks[$name][] = $callback;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Call hooks by giving name.
35
     *
36
     * @param string $name
37
     *
38
     * @return Response
39
     */
40
    protected function callHooks($name)
41
    {
42
        $hooks = Arr::get($this->hooks, $name, []);
43
44
        foreach ($hooks as $func) {
45
46
            if (!$func instanceof Closure) {
47
                continue;
48
            }
49
50
            $response = call_user_func($func, $this);
51
52
            if ($response instanceof Response) {
53
                return $response;
54
            }
55
        }
56
    }
57
58
    /**
59
     * Set after getting editing model callback.
60
     *
61
     * @param Closure $callback
62
     *
63
     * @return $this
64
     */
65
    public function editing(Closure $callback)
66
    {
67
        return $this->registerHook('editing', $callback);
68
    }
69
70
    /**
71
     * Set submitted callback.
72
     *
73
     * @param Closure $callback
74
     *
75
     * @return $this
76
     */
77
    public function submitted(Closure $callback)
78
    {
79
        return $this->registerHook('submitted', $callback);
80
    }
81
82
    /**
83
     * Set saving callback.
84
     *
85
     * @param Closure $callback
86
     *
87
     * @return $this
88
     */
89
    public function saving(Closure $callback)
90
    {
91
        return $this->registerHook('saving', $callback);
92
    }
93
94
    /**
95
     * Set saved callback.
96
     *
97
     * @param Closure $callback
98
     *
99
     * @return $this
100
     */
101
    public function saved(Closure $callback)
102
    {
103
        return $this->registerHook('saved', $callback);
104
    }
105
106
    /**
107
     * @param Closure $callback
108
     *
109
     * @return $this
110
     */
111
    public function deleting(Closure $callback)
112
    {
113
        return $this->registerHook('deleting', $callback);
114
    }
115
116
    /**
117
     * @param Closure $callback
118
     *
119
     * @return $this
120
     */
121
    public function deleted(Closure $callback)
122
    {
123
        return $this->registerHook('deleted', $callback);
124
    }
125
126
    /**
127
     * Call editing callbacks.
128
     *
129
     * @return mixed
130
     */
131
    protected function callEditing()
132
    {
133
        return $this->callHooks('editing');
134
    }
135
136
    /**
137
     * Call submitted callback.
138
     *
139
     * @return mixed
140
     */
141
    protected function callSubmitted()
142
    {
143
        return $this->callHooks('submitted');
144
    }
145
146
    /**
147
     * Call saving callback.
148
     *
149
     * @return mixed
150
     */
151
    protected function callSaving()
152
    {
153
        return $this->callHooks('saving');
154
    }
155
156
    /**
157
     * Callback after saving a Model.
158
     *
159
     * @return mixed|null
160
     */
161
    protected function callSaved()
162
    {
163
        return $this->callHooks('saved');
164
    }
165
166
    /**
167
     * Call hooks when deleting.
168
     *
169
     * @return mixed
170
     */
171
    protected function callDeleting()
172
    {
173
        return $this->callHooks('deleting');
174
    }
175
176
    /**
177
     * @return mixed
178
     */
179
    protected function callDeleted()
180
    {
181
        return $this->callHooks('deleted');
182
    }
183
}