Test Failed
Push — master ( a3e60c...72c09c )
by Richan
01:21
created

BlameableTrait::scopeCreatedBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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