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

AuditableTrait::bootAuditableTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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