Completed
Push — master ( 153da6...30fdee )
by Arjay
03:20
created

AuditableTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 17.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 12
c 5
b 0
f 1
lcom 1
cbo 3
dl 20
loc 117
ccs 0
cts 49
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A bootAuditableTrait() 0 4 1
A creator() 0 4 1
A getCreatedByColumn() 0 4 1
A updater() 0 4 1
A getUpdatedByColumn() 0 4 1
A getCreatedByNameAttribute() 10 10 2
A getUserInstance() 0 6 1
A getUpdatedByNameAttribute() 10 10 2
A scopeOwned() 0 4 1
A getQualifiedUserIdColumn() 0 4 1

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\Builder;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
8
/**
9
 * Class AuditableTrait
10
 *
11
 * @property int created_by
12
 * @property int updated_by
13
 * @method string getTable()
14
 * @method void observe($observer)
15
 * @method BelongsTo belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
16
 * @package Yajra\Auditable
17
 */
18
trait AuditableTrait
19
{
20
    /**
21
     * Boot the audit trait for a model.
22
     *
23
     * @return void
24
     */
25
    public static function bootAuditableTrait()
26
    {
27
        static::observe(new AuditableTraitObserver);
28
    }
29
30
    /**
31
     * Get user model who created the record.
32
     *
33
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34
     */
35
    public function creator()
36
    {
37
        return $this->belongsTo(get_class(auth()->user()), $this->getCreatedByColumn());
38
    }
39
40
    /**
41
     * Get column name for created by.
42
     *
43
     * @return string
44
     */
45
    protected function getCreatedByColumn()
46
    {
47
        return 'created_by';
48
    }
49
50
    /**
51
     * Get user model who updated the record.
52
     *
53
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
54
     */
55
    public function updater()
56
    {
57
        return $this->belongsTo(get_class(auth()->user()), $this->getUpdatedByColumn());
58
    }
59
60
    /**
61
     * Get column name for updated by.
62
     *
63
     * @return string
64
     */
65
    protected function getUpdatedByColumn()
66
    {
67
        return 'updated_by';
68
    }
69
70
    /**
71
     * Get created by user full name.
72
     *
73
     * @return string
74
     */
75 View Code Duplication
    public function getCreatedByNameAttribute()
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...
76
    {
77
        if ($this->{$this->getCreatedByColumn()}) {
78
            $user = $this->getUserInstance()->find($this->{$this->getCreatedByColumn()});
79
80
            return $user->first_name . ' ' . $user->last_name;
81
        }
82
83
        return '';
84
    }
85
86
    /**
87
     * Get Laravel's user class instance.
88
     *
89
     * @return \Illuminate\Database\Eloquent\Model
90
     */
91
    public function getUserInstance()
92
    {
93
        $class = get_class(auth()->user());
94
95
        return new $class;
96
    }
97
98
    /**
99
     * Get updated by user full name.
100
     *
101
     * @return string
102
     */
103 View Code Duplication
    public function getUpdatedByNameAttribute()
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...
104
    {
105
        if ($this->{$this->getUpdatedByColumn()}) {
106
            $user = $this->getUserInstance()->find($this->{$this->getUpdatedByColumn()});
107
108
            return $user->first_name . ' ' . $user->last_name;
109
        }
110
111
        return '';
112
    }
113
114
    /**
115
     * Query scope to limit results to own records.
116
     *
117
     * @param \Illuminate\Database\Eloquent\Builder $query
118
     * @return \Illuminate\Database\Eloquent\Builder
119
     */
120
    public function scopeOwned(Builder $query)
121
    {
122
        return $query->where($this->getQualifiedUserIdColumn(), auth()->id());
123
    }
124
125
    /**
126
     * Get qualified column name for user id.
127
     *
128
     * @return string
129
     */
130
    public function getQualifiedUserIdColumn()
131
    {
132
        return $this->getTable() . '.' . $this->getUserInstance()->getKey();
133
    }
134
}
135