Completed
Push — master ( 404992...c04a2b )
by Arjay
26:52 queued 11:52
created

AuditableTraitObserver::creating()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 3
nc 4
nop 1
crap 12
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
Duplication introduced by
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...
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
Duplication introduced by
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