Completed
Push — master ( 7361a2...a254fd )
by Richan
01:26
created

BlameableTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A blameable() 0 8 2
A bootBlameableTrait() 0 4 1
A buildBlameableScope() 0 8 2
A creator() 0 7 1
A updater() 0 7 1
A scopeCreatedBy() 0 4 1
A scopeUpdatedBy() 0 4 1
A silentUpdate() 0 7 1
A useSoftDeletes() 0 4 1
belongsTo() 0 1 ?
getDirty() 0 1 ?
getKeyName() 0 1 ?
getKey() 0 1 ?
newQueryWithoutScopes() 0 1 ?
1
<?php
2
3
namespace RichanFongdasen\EloquentBlameable;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
9
trait BlameableTrait
10
{
11
    /**
12
     * Get any of override 'blameable attributes'.
13
     *
14
     * @return array
15
     */
16
    public function blameable()
17
    {
18
        if (property_exists($this, 'blameable')) {
19
            return (array) static::$blameable;
20
        }
21
22
        return [];
23
    }
24
25
    /**
26
     * Boot the Blameable service by attaching
27
     * a new observer into the current model object.
28
     *
29
     * @return void
30
     */
31
    public static function bootBlameableTrait()
32
    {
33
        static::observe(app(BlameableObserver::class));
34
    }
35
36
    /**
37
     * Build blameable query scope.
38
     *
39
     * @param \Illuminate\Database\Eloquent\Builder $query
40
     * @param mixed                                 $userId
41
     * @param string                                $key
42
     *
43
     * @return \Illuminate\Database\Eloquent\Builder
44
     */
45
    private function buildBlameableScope(Builder $query, $userId, $key)
46
    {
47
        if ($userId instanceof Model) {
48
            $userId = $userId->getKey();
49
        }
50
51
        return $query->where(app(BlameableService::class)->getConfiguration($this, $key), $userId);
52
    }
53
54
    /**
55
     * Get the user who created the record.
56
     *
57
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
58
     */
59
    public function creator()
60
    {
61
        return $this->belongsTo(
62
            app(BlameableService::class)->getConfiguration($this, 'user'),
63
            app(BlameableService::class)->getConfiguration($this, 'createdBy')
64
        );
65
    }
66
67
    /**
68
     * Get the user who updated the record for the last time.
69
     *
70
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
71
     */
72
    public function updater()
73
    {
74
        return $this->belongsTo(
75
            app(BlameableService::class)->getConfiguration($this, 'user'),
76
            app(BlameableService::class)->getConfiguration($this, 'updatedBy')
77
        );
78
    }
79
80
    /**
81
     * createdBy Query Scope.
82
     *
83
     * @param \Illuminate\Database\Eloquent\Builder $query
84
     * @param mixed                                 $userId
85
     *
86
     * @return \Illuminate\Database\Eloquent\Builder
87
     */
88
    public function scopeCreatedBy(Builder $query, $userId)
89
    {
90
        return $this->buildBlameableScope($query, $userId, 'createdBy');
91
    }
92
93
    /**
94
     * updatedBy Query Scope.
95
     *
96
     * @param \Illuminate\Database\Eloquent\Builder $query
97
     * @param mixed                                 $userId
98
     *
99
     * @return \Illuminate\Database\Eloquent\Builder
100
     */
101
    public function scopeUpdatedBy(Builder $query, $userId)
102
    {
103
        return $this->buildBlameableScope($query, $userId, 'updatedBy');
104
    }
105
106
    /**
107
     * Silently update the model without firing any
108
     * events.
109
     *
110
     * @return int
111
     */
112
    public function silentUpdate()
113
    {
114
        return $this->newQueryWithoutScopes()
115
            ->where($this->getKeyName(), $this->getKey())
116
            ->getQuery()
117
            ->update($this->getDirty());
118
    }
119
120
    /**
121
     * Confirm if the current model uses SoftDeletes.
122
     *
123
     * @return bool
124
     */
125
    public function useSoftDeletes()
126
    {
127
        return in_array(SoftDeletes::class, class_uses($this));
128
    }
129
130
    /**
131
     * Define an inverse one-to-one or many relationship.
132
     *
133
     * @param string      $related
134
     * @param string|null $foreignKey
135
     * @param string|null $otherKey
136
     * @param string|null $relation
137
     *
138
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
139
     */
140
    abstract public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null);
141
142
    /**
143
     * Get the attributes that have been changed since last sync.
144
     *
145
     * @return array
146
     */
147
    abstract public function getDirty();
148
149
    /**
150
     * Get the primary key for the model.
151
     *
152
     * @return string
153
     */
154
    abstract public function getKeyName();
155
156
    /**
157
     * Get the value of the model's primary key.
158
     *
159
     * @return mixed
160
     */
161
    abstract public function getKey();
162
163
    /**
164
     * Get a new query builder that doesn't have any global scopes.
165
     *
166
     * @return \Illuminate\Database\Eloquent\Builder|static
167
     */
168
    abstract public function newQueryWithoutScopes();
169
}
170