Completed
Push — master ( 029c72...fe15da )
by Arjay
11:21
created

src/AuditableTrait.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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\Builder;
6
7
/**
8
 * @property mixed creator
9
 * @property mixed updater
10
 * @property mixed deleter
11
 */
12
trait AuditableTrait
13
{
14
    /**
15
     * Boot the audit trait for a model.
16
     *
17
     * @return void
18
     */
19
    public static function bootAuditableTrait()
20
    {
21
        static::observe(new AuditableTraitObserver);
22
    }
23
24
    /**
25
     * Get user model who created the record.
26
     *
27
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
28
     */
29
    public function creator()
30
    {
31
        return $this->belongsTo($this->getUserClass(), $this->getCreatedByColumn())->withDefault();
32
    }
33
34
    /**
35
     * Get user class.
36
     *
37
     * @return string
38
     */
39
    protected function getUserClass()
40
    {
41
        if (property_exists($this, 'auditUser')) {
42
            return $this->auditUser;
0 ignored issues
show
The property auditUser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        }
44
45
        return config('auth.providers.users.model', 'App\User');
46
    }
47
48
    /**
49
     * Get column name for created by.
50
     *
51
     * @return string
52
     */
53
    public function getCreatedByColumn()
54
    {
55
        return defined('static::CREATED_BY') ? static::CREATED_BY : 'created_by';
56
    }
57
58
    /**
59
     * Get user model who updated the record.
60
     *
61
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
62
     */
63
    public function updater()
64
    {
65
        return $this->belongsTo($this->getUserClass(), $this->getUpdatedByColumn())->withDefault();
66
    }
67
68
    /**
69
     * Get Laravel's user class instance.
70
     *
71
     * @return \Illuminate\Database\Eloquent\Model
72
     */
73
    public function getUserInstance()
74
    {
75
        $class = $this->getUserClass();
76
77
        return new $class;
78
    }
79
80
    /**
81
     * Get column name for updated by.
82
     *
83
     * @return string
84
     */
85
    public function getUpdatedByColumn()
86
    {
87
        return defined('static::UPDATED_BY') ? static::UPDATED_BY : 'updated_by';
88
    }
89
90
    /**
91
     * Get user model who deleted the record.
92
     *
93
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
94
     */
95
    public function deleter()
96
    {
97
        return $this->belongsTo($this->getUserClass(), $this->getDeletedByColumn())->withDefault();
98
    }
99
100
    /**
101
     * Get column name for deleted by.
102
     *
103
     * @return string
104
     */
105
    public function getDeletedByColumn()
106
    {
107
        return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by';
108
    }
109
110
    /**
111
     * Get created by user full name.
112
     *
113
     * @return string
114
     */
115
    public function getCreatedByNameAttribute()
116
    {
117
        if ($this->{$this->getCreatedByColumn()}) {
118
            return $this->creator->name;
119
        }
120
121
        return '';
122
    }
123
124
    /**
125
     * Get updated by user full name.
126
     *
127
     * @return string
128
     */
129
    public function getUpdatedByNameAttribute()
130
    {
131
        if ($this->{$this->getUpdatedByColumn()}) {
132
            return $this->updater->name;
133
        }
134
135
        return '';
136
    }
137
138
    /**
139
     * Get deleted by user full name.
140
     *
141
     * @return string
142
     */
143
    public function getDeletedByNameAttribute()
144
    {
145
        if ($this->{$this->getDeletedByColumn()}) {
146
            return $this->deleter->name;
147
        }
148
149
        return '';
150
    }
151
152
    /**
153
     * Query scope to limit results to own records.
154
     *
155
     * @param \Illuminate\Database\Eloquent\Builder $query
156
     * @return \Illuminate\Database\Eloquent\Builder
157
     */
158
    public function scopeOwned(Builder $query)
159
    {
160
        return $query->where($this->getQualifiedUserIdColumn(), auth()->id());
161
    }
162
163
    /**
164
     * Get qualified column name for user id.
165
     *
166
     * @return string
167
     */
168
    public function getQualifiedUserIdColumn()
169
    {
170
        return $this->getTable() . '.' . $this->getUserInstance()->getKey();
171
    }
172
}
173