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 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Support\Facades\Schema; |
||
7 | |||
8 | class AuditableTraitObserver |
||
9 | { |
||
10 | /** |
||
11 | * Model's creating event hook. |
||
12 | * |
||
13 | * @param Model $model |
||
14 | */ |
||
15 | public function creating(Model $model) |
||
16 | { |
||
17 | $createdBy = $model->getCreatedByColumn(); |
||
18 | $updatedBy = $model->getUpdatedByColumn(); |
||
19 | |||
20 | if (! $model->$createdBy) { |
||
21 | $model->$createdBy = $this->getAuthenticatedUserId(); |
||
22 | } |
||
23 | |||
24 | if (! $model->$updatedBy) { |
||
25 | $model->$updatedBy = $this->getAuthenticatedUserId(); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Get authenticated user id depending on model's auth guard. |
||
31 | * |
||
32 | * @return int |
||
33 | */ |
||
34 | protected function getAuthenticatedUserId() |
||
35 | { |
||
36 | return auth()->check() ? auth()->id() : null; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Model's updating event hook. |
||
41 | * |
||
42 | * @param Model $model |
||
43 | */ |
||
44 | View Code Duplication | public function updating(Model $model) |
|
0 ignored issues
–
show
|
|||
45 | { |
||
46 | $updatedBy = $model->getUpdatedByColumn(); |
||
47 | |||
48 | if (! $model->isDirty($updatedBy)) { |
||
49 | $model->$updatedBy = $this->getAuthenticatedUserId(); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Set updatedBy column on save if value is not the same. |
||
55 | * |
||
56 | * @param \Illuminate\Database\Eloquent\Model $model |
||
57 | */ |
||
58 | View Code Duplication | public function saved(Model $model) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
59 | { |
||
60 | $updatedBy = $model->getUpdatedByColumn(); |
||
61 | |||
62 | if ($model->$updatedBy <> $this->getAuthenticatedUserId()) { |
||
63 | $model->$updatedBy = $this->getAuthenticatedUserId(); |
||
64 | $model->save(); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.