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

AuditableTraitObserver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 28.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 17
loc 60
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 13 3
A getAuthenticatedUserId() 0 4 2
A updating() 8 8 2
A saved() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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