These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Yajra\Auditable; |
||
4 | |||
5 | /** |
||
6 | * Class AuditableObserver |
||
7 | * |
||
8 | * @package Yajra\Auditable |
||
9 | */ |
||
10 | class AuditableTraitObserver |
||
11 | { |
||
12 | /** |
||
13 | * Model's creating event hook. |
||
14 | * |
||
15 | * @param \Yajra\Auditable\AuditableTrait $model |
||
16 | */ |
||
17 | public function creating($model) |
||
18 | { |
||
19 | if (! $model->created_by) { |
||
20 | $model->created_by = $this->getAuthenticatedUserId($model); |
||
21 | } |
||
22 | |||
23 | if (! $model->updated_by) { |
||
24 | $model->updated_by = $this->getAuthenticatedUserId($model); |
||
25 | } |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Get authenticated user id depending on model's auth guard. |
||
30 | * |
||
31 | * @param \Yajra\Auditable\AuditableTrait $model |
||
32 | * @return int |
||
33 | */ |
||
34 | protected function getAuthenticatedUserId($model) |
||
35 | { |
||
36 | return auth($model->getAuthGuard())->check() ? auth($model->getAuthGuard())->id() : 0; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Model's updating event hook. |
||
41 | * |
||
42 | * @param \Yajra\Auditable\AuditableTrait $model |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
43 | */ |
||
44 | public function updating($model) |
||
45 | { |
||
46 | if (! $model->updated_by) { |
||
47 | $model->updated_by = $this->getAuthenticatedUserId($model); |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 |