Completed
Push — master ( 7a0335...001456 )
by Arjay
10:38
created

AuditableTrait::getDeletedByNameAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Yajra\Auditable;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
trait AuditableTrait
8
{
9
    /**
10
     * Boot the audit trait for a model.
11
     *
12
     * @return void
13
     */
14
    public static function bootAuditableTrait()
15
    {
16
        static::observe(new AuditableTraitObserver);
17
    }
18
19
    /**
20
     * Get user model who created the record.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
23
     */
24
    public function creator()
25
    {
26
        return $this->belongsTo($this->getUserInstance(), $this->getCreatedByColumn());
0 ignored issues
show
Bug introduced by Arjay Angeles
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
27
    }
28
29
    /**
30
     * Get column name for created by.
31
     *
32
     * @return string
33
     */
34
    public function getCreatedByColumn()
35
    {
36
        return defined('static::CREATED_BY') ? static::CREATED_BY : 'created_by';
37
    }
38
39
    /**
40
     * Get user model who updated the record.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
43
     */
44
    public function updater()
45
    {
46
        return $this->belongsTo($this->getUserInstance(), $this->getUpdatedByColumn());
0 ignored issues
show
Bug introduced by Arjay Angeles
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
47
    }
48
49
    /**
50
     * Get column name for updated by.
51
     *
52
     * @return string
53
     */
54
    public function getUpdatedByColumn()
55
    {
56
        return defined('static::UPDATED_BY') ? static::UPDATED_BY : 'updated_by';
57
    }
58
59
    /**
60
     * Get user model who deleted the record.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    public function deleter()
65
    {
66
        return $this->belongsTo($this->getUserInstance(), $this->getDeletedByColumn());
0 ignored issues
show
Bug introduced by Charlie Gilman
It seems like belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
    }
68
69
    /**
70
     * Get column name for deleted by.
71
     *
72
     * @return string
73
     */
74
    public function getDeletedByColumn()
75
    {
76
        return defined('static::DELETED_BY') ? static::DELETED_BY : 'deleted_by';
77
    }
78
79
    /**
80
     * Get created by user full name.
81
     *
82
     * @return string
83
     */
84
    public function getCreatedByNameAttribute()
85
    {
86
        if ($this->{$this->getCreatedByColumn()}) {
87
            return $this->creator->first_name . ' ' . $this->creator->last_name;
0 ignored issues
show
Bug introduced by Arjay Angeles
The property creator 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...
88
        }
89
90
        return '';
91
    }
92
93
    /**
94
     * Get Laravel's user class instance.
95
     *
96
     * @return \Illuminate\Database\Eloquent\Model
97
     */
98
    public function getUserInstance()
99
    {
100
        $class = config('auth.providers.users.model', 'App\User');
101
102
        return new $class;
103
    }
104
105
    /**
106
     * Get updated by user full name.
107
     *
108
     * @return string
109
     */
110
    public function getUpdatedByNameAttribute()
111
    {
112
        if ($this->{$this->getUpdatedByColumn()}) {
113
            return $this->updater->first_name . ' ' . $this->updater->last_name;
0 ignored issues
show
Bug introduced by Arjay Angeles
The property updater 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...
114
        }
115
116
        return '';
117
    }
118
119
    /**
120
     * Get deleted by user full name.
121
     *
122
     * @return string
123
     */
124
    public function getDeletedByNameAttribute()
125
    {
126
        if ($this->{$this->getDeletedByColumn()}) {
127
            return $this->deleter->first_name . ' ' . $this->deleter->last_name;
0 ignored issues
show
Bug introduced by Charlie Gilman
The property deleter 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...
128
        }
129
130
        return '';
131
    }
132
133
    /**
134
     * Query scope to limit results to own records.
135
     *
136
     * @param \Illuminate\Database\Eloquent\Builder $query
137
     * @return \Illuminate\Database\Eloquent\Builder
138
     */
139
    public function scopeOwned(Builder $query)
140
    {
141
        return $query->where($this->getQualifiedUserIdColumn(), auth()->id());
142
    }
143
144
    /**
145
     * Get qualified column name for user id.
146
     *
147
     * @return string
148
     */
149
    public function getQualifiedUserIdColumn()
150
    {
151
        return $this->getTable() . '.' . $this->getUserInstance()->getKey();
0 ignored issues
show
Bug introduced by Arjay Angeles
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
152
    }
153
}
154