1 | <?php |
||
2 | |||
3 | namespace Sfneal\CrudModelActions\Utils; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model as EloquentModel; |
||
6 | use Sfneal\Events\Event; |
||
7 | use Sfneal\Models\Model; |
||
8 | |||
9 | trait ModelEvents |
||
10 | { |
||
11 | /** |
||
12 | * @var Event Model Tracking Event to fire |
||
13 | */ |
||
14 | protected $trackingEvent; |
||
15 | |||
16 | /** |
||
17 | * @var Model|EloquentModel |
||
18 | */ |
||
19 | private $trackingEventModel; |
||
20 | |||
21 | /** |
||
22 | * @var bool Determine if the TrackingEvent was fired |
||
23 | */ |
||
24 | private $trackingEventWasFired; |
||
25 | |||
26 | /** |
||
27 | * Fire the trackingEvent. |
||
28 | * |
||
29 | * @return void |
||
30 | */ |
||
31 | protected function fireEvent() |
||
32 | { |
||
33 | if (isset($this->trackingEvent)) { |
||
34 | event(new $this->trackingEvent($this->trackingEventModel())); |
||
0 ignored issues
–
show
|
|||
35 | $this->trackingEventWasFired = true; |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Retrieve the Model to be used in the fireEvent() method. |
||
41 | * |
||
42 | * - optionally set a the $trackingEventModel property |
||
43 | * - useful for using a different model in the trackingEvent than the resolved/saved model |
||
44 | * |
||
45 | * @param EloquentModel|null $model |
||
46 | * @return Model|EloquentModel |
||
47 | */ |
||
48 | protected function trackingEventModel(EloquentModel $model = null): EloquentModel |
||
49 | { |
||
50 | if (isset($model)) { |
||
51 | $this->trackingEventModel = $model; |
||
52 | } |
||
53 | |||
54 | return $this->trackingEventModel ?? $this->model; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Determine if the TrackingEvent was fired. |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | protected function wasTrackingEventFired(): bool |
||
63 | { |
||
64 | return $this->trackingEventWasFired; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Set the $trackingEvent the default provided by the config file if null. |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | private function setTrackingEventFromConfig(): bool |
||
73 | { |
||
74 | // Set the $trackingEvent using the config |
||
75 | if (! isset($this->trackingEvent)) { |
||
76 | $this->trackingEvent = config('crud-model-actions.event'); |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | |||
81 | // Don't change |
||
82 | else { |
||
83 | return false; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.