1 | <?php |
||
2 | |||
3 | namespace OwenIt\Auditing; |
||
4 | |||
5 | use Illuminate\Support\Facades\Config; |
||
6 | use OwenIt\Auditing\Contracts\Auditable; |
||
7 | use OwenIt\Auditing\Events\DispatchAudit; |
||
8 | use OwenIt\Auditing\Events\DispatchingAudit; |
||
9 | use OwenIt\Auditing\Facades\Auditor; |
||
10 | |||
11 | class AuditableObserver |
||
12 | { |
||
13 | /** |
||
14 | * Is the model being restored? |
||
15 | * |
||
16 | * @var bool |
||
17 | */ |
||
18 | public static $restoring = false; |
||
19 | |||
20 | /** |
||
21 | * Handle the retrieved event. |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | public function retrieved(Auditable $model) |
||
26 | { |
||
27 | 5 | $this->dispatchAudit($model->setAuditEvent('retrieved')); |
|
28 | } |
||
29 | 5 | ||
30 | /** |
||
31 | * Handle the created event. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function created(Auditable $model) |
||
36 | { |
||
37 | $this->dispatchAudit($model->setAuditEvent('created')); |
||
38 | } |
||
39 | 15 | ||
40 | /** |
||
41 | 15 | * Handle the updated event. |
|
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public function updated(Auditable $model) |
||
46 | { |
||
47 | // Ignore the updated event when restoring |
||
48 | if (!static::$restoring) { |
||
49 | $this->dispatchAudit($model->setAuditEvent('updated')); |
||
50 | } |
||
51 | 4 | } |
|
52 | |||
53 | /** |
||
54 | 4 | * Handle the deleted event. |
|
55 | 3 | * |
|
56 | * @return void |
||
57 | */ |
||
58 | public function deleted(Auditable $model) |
||
59 | { |
||
60 | $this->dispatchAudit($model->setAuditEvent('deleted')); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Handle the restoring event. |
||
65 | * |
||
66 | 2 | * @return void |
|
67 | */ |
||
68 | 2 | public function restoring(Auditable $model) |
|
0 ignored issues
–
show
|
|||
69 | { |
||
70 | // When restoring a model, an updated event is also fired. |
||
71 | // By keeping track of the main event that took place, |
||
72 | // we avoid creating a second audit with wrong values |
||
73 | static::$restoring = true; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Handle the restored event. |
||
78 | 1 | * |
|
79 | * @return void |
||
80 | */ |
||
81 | public function restored(Auditable $model) |
||
82 | { |
||
83 | 1 | $this->dispatchAudit($model->setAuditEvent('restored')); |
|
84 | |||
85 | // Once the model is restored, we need to put everything back |
||
86 | // as before, in case a legitimate update event is fired |
||
87 | static::$restoring = false; |
||
88 | } |
||
89 | |||
90 | protected function dispatchAudit(Auditable $model): void |
||
91 | { |
||
92 | if (!$model->readyForAuditing()) { |
||
93 | 1 | return; |
|
94 | } |
||
95 | 1 | ||
96 | // @phpstan-ignore method.notFound |
||
97 | $model->preloadResolverData(); |
||
98 | if (!Config::get('audit.queue.enable', false)) { |
||
99 | 1 | Auditor::execute($model); |
|
100 | return; |
||
101 | } |
||
102 | 15 | ||
103 | if (!$this->fireDispatchingAuditEvent($model)) { |
||
104 | 15 | return; |
|
105 | 8 | } |
|
106 | |||
107 | // Unload the relations to prevent large amounts of unnecessary data from being serialized. |
||
108 | 15 | $model->withoutRelations(); |
|
109 | 15 | app()->make('events')->dispatch(new DispatchAudit($model)); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | 15 | * Fire the Auditing event. |
|
114 | */ |
||
115 | protected function fireDispatchingAuditEvent(Auditable $model): bool |
||
116 | { |
||
117 | return app()->make('events') |
||
118 | 15 | ->until(new DispatchingAudit($model)) !== false; |
|
119 | } |
||
120 | } |
||
121 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.