BaseObserver::pivotUpdating()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 4
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alive
5
 * Date: 12/14/17
6
 * Time: 6:49 AM
7
 */
8
9
/** User Manual
10
 *  Base Observer Help to create event listener simple
11
 * 1- Create your own Observer file and locate it into app/Observers
12
 * 2- Extend it from BaseObserver form this Package
13
 * 3- Override boot method in your Model what you want to track events
14
 * 4- Put following code into "boot" of your model after "parent::boot();"
15
 *          Order2::observe(Order2Observer::class);
16
 * 5- add what event you want into event service provider with this convention
17
 * 'App\Events\{Model}{Method}Event' => [
18
 * 'App\Listeners\{Model}{Method}Listener',
19
 * ],
20
 * 6- generate events
21
 * 7- extend all event with 'BaseModelEvent' or 'BaseModelPivotEvent' and remove all class code
22
 * 8- get model in listener class with following command
23
 * $event->getModel();
24
 * 9- add observer class into boot method of model
25
 */
26
27
namespace Alive2212\LaravelModelEventService\Observers;
28
29
use Alive2212\LaravelMessageService\MessageService;
30
use App\Resources\StringHelper;
31
32
class BaseObserver
33
{
34
    protected $eventNamePrefix = 'App\\Events\\';
35
36
    /**
37
     * @param $model
38
     */
39
    public function creating($model)
40
    {
41
        $this->callEvent($model);
42
    }
43
44
    /**
45
     * @param $params
46
     */
47
    public function callEvent(...$params)
48
    {
49
        $eventName = $this->getEventListenerName();
50
        $this->messageHandler($params);
51
        if (class_exists($eventName)) {
52
            event(new $eventName($params));
53
        }
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getEventListenerName()
60
    {
61
        $calledClass = (str_replace('Observer', '', collect(explode('\\', get_class($this)))->last()));
62
        $calledMethod = (new StringHelper())->upperFirstLetter(debug_backtrace()[2]['function']);
63
        $eventName = $this->eventNamePrefix . $calledClass . $calledMethod . 'Event';
64
        return (string)$eventName;
65
    }
66
67
    /**
68
     * @param $model
69
     */
70
    public function created($model)
71
    {
72
        $this->callEvent($model);
73
    }
74
75
    /**
76
     * @param $model
77
     */
78
    public function updating($model)
79
    {
80
        $this->callEvent($model);
81
    }
82
83
    /**
84
     * @param $model
85
     */
86
    public function updated($model)
87
    {
88
        $this->callEvent($model);
89
    }
90
91
    /**
92
     * @param $model
93
     */
94
    public function saving($model)
95
    {
96
        $this->callEvent($model);
97
    }
98
99
    /**
100
     * @param $model
101
     */
102
    public function saved($model)
103
    {
104
        $this->callEvent($model);
105
    }
106
107
    /**
108
     * @param $model
109
     */
110
    public function deleting($model)
111
    {
112
        $this->callEvent($model);
113
    }
114
115
    /**
116
     * @param $model
117
     */
118
    public function deleted($model)
119
    {
120
        $this->callEvent($model);
121
    }
122
123
    /**
124
     * @param $model
125
     */
126
    public function restoring($model)
127
    {
128
        $this->callEvent($model);
129
    }
130
131
    /**
132
     * @param $model
133
     */
134
    public function restored($model)
135
    {
136
        $this->callEvent($model);
137
    }
138
139
    /**
140
     * For many to many relation
141
     * Before do it
142
     * Attaching
143
     *
144
     * @param $model
145
     * @param $relationName
146
     * @param $pivotIds
147
     * @param $pivotIdsAttributes
148
     */
149
    public function pivotAttaching($model, $relationName, $pivotIds, $pivotIdsAttributes)
150
    {
151
        $this->callEvent($model, $relationName, $pivotIds, $pivotIdsAttributes);
152
    }
153
154
    /**
155
     * For many to many relation
156
     * Before do it
157
     * Detaching
158
     *
159
     * @param $model
160
     * @param $relationName
161
     * @param $pivotIds
162
     * @param $pivotIdsAttributes
163
     */
164
    public function pivotDetaching($model, $relationName, $pivotIds, $pivotIdsAttributes)
165
    {
166
        $this->callEvent($model, $relationName, $pivotIds, $pivotIdsAttributes);
167
    }
168
169
    /**
170
     * For many to many relation
171
     * Before do it
172
     * Updating
173
     *
174
     * @param $model
175
     * @param $relationName
176
     * @param $pivotIds
177
     * @param $pivotIdsAttributes
178
     */
179
    public function pivotUpdating($model, $relationName, $pivotIds, $pivotIdsAttributes)
180
    {
181
        $this->callEvent($model, $relationName, $pivotIds, $pivotIdsAttributes);
182
    }
183
184
    /**
185
     * For many to many relation
186
     * After it done
187
     * Attached
188
     *
189
     * @param $model
190
     * @param $relationName
191
     * @param $pivotIds
192
     * @param $pivotIdsAttributes
193
     */
194
    public function pivotAttached($model, $relationName, $pivotIds, $pivotIdsAttributes)
195
    {
196
        $this->callEvent($model, $relationName, $pivotIds, $pivotIdsAttributes);
197
    }
198
199
    /**
200
     * For many to many relation
201
     * After it done
202
     * Detached
203
     *
204
     * @param $model
205
     * @param $relationName
206
     * @param $pivotIds
207
     * @param $pivotIdsAttributes
208
     */
209
    public function pivotDetached($model, $relationName, $pivotIds, $pivotIdsAttributes)
210
    {
211
        $this->callEvent($model, $relationName, $pivotIds, $pivotIdsAttributes);
212
    }
213
214
    /**
215
     * For many to many relation
216
     * After it done
217
     * Updated
218
     *
219
     * @param $model
220
     * @param $relationName
221
     * @param $pivotIds
222
     * @param $pivotIdsAttributes
223
     */
224
    public function pivotUpdated($model, $relationName, $pivotIds, $pivotIdsAttributes)
225
    {
226
        $this->callEvent($model, $relationName, $pivotIds, $pivotIdsAttributes);
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getEventNamePrefix()
233
    {
234
        return $this->eventNamePrefix;
235
    }
236
237
    /**
238
     * @param string $eventNamePrefix
239
     */
240
    public function setEventNamePrefix($eventNamePrefix)
241
    {
242
        $this->eventNamePrefix = $eventNamePrefix;
243
    }
244
245
    /**
246
     * @param $params
247
     */
248
    public function messageHandler($params)
249
    {
250
        $messageService = new MessageService();
251
        $eventType = (new StringHelper())->upperFirstLetter(debug_backtrace()[2]['function']);
252
        $messageService->handle($params[0], $eventType);
253
    }
254
}
255