Completed
Push — master ( e65707...e64f9c )
by Freek
02:08
created

Activity::scopeByCauser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Models;
4
5
use Eloquent;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Illuminate\Support\Collection;
9
10
class Activity extends Eloquent
11
{
12
    protected $table = 'activity_log';
13
14
    public $guarded = [];
15
16
    protected $casts = [
17
        'properties' => 'collection',
18
    ];
19
20
    public function subject(): MorphTo
21
    {
22
        if (config('laravel-activitylog.subject_returns_soft_deleted_models')) {
23
            return $this->morphTo()->withTrashed();
24
        }
25
26
        return $this->morphTo();
27
    }
28
29
    public function causer(): MorphTo
30
    {
31
        return $this->morphTo();
32
    }
33
34
    /**
35
     * Get the extra properties with the given name.
36
     *
37
     * @param string $propertyName
38
     *
39
     * @return mixed
40
     */
41
    public function getExtraProperty(string $propertyName)
42
    {
43
        return array_get($this->properties->toArray(), $propertyName);
44
    }
45
46
    public function getChangesAttribute(): Collection
47
    {
48
        return collect(array_filter($this->properties->toArray(), function ($key) {
49
            return in_array($key, ['attributes', 'old']);
50
        }, ARRAY_FILTER_USE_KEY));
51
    }
52
53
    public function scopeInLog(Builder $query, ...$logNames): Builder
54
    {
55
        if (is_array($logNames[0])) {
56
            $logNames = $logNames[0];
57
        }
58
59
        return $query->whereIn('log_name', $logNames);
60
    }
61
62
    /**
63
     * Scope a query to only include activities by a give causer.
64
     *
65
     * @param \Illuminate\Database\Eloquent\Builder $query
66
     * @param $causer
67
     *
68
     * @return \Illuminate\Database\Eloquent\Builder
69
     */
70
    public function scopeCausedBy(Builder $query, $causer): Builder
71
    {
72
        return $query->where('causer_id', $causer->getKey());
73
    }
74
75
    /**
76
     * Scope a query to only include activities for a give subject.
77
     *
78
     * @param \Illuminate\Database\Eloquent\Builder $query
79
     * @param $subject
80
     *
81
     * @return \Illuminate\Database\Eloquent\Builder
82
     */
83
    public function scopeForSubject(Builder $query, $subject): Builder
84
    {
85
        return $query->where('subject_id', $subject->getKey());
86
    }
87
}
88