Completed
Push — master ( b40d31...e65707 )
by Freek
02:42
created

Activity::scopeForSubject()   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
     * @return \Illuminate\Database\Eloquent\Builder
66
     */
67
    public function scopeByCauser(Builder $query, $causer): Builder
68
    {
69
        return $query->where('causer_id', $causer->getKey());
70
    }
71
72
    /**
73
     * Scope a query to only include activities for a give subject.
74
     *
75
     * @return \Illuminate\Database\Eloquent\Builder
76
     */
77
    public function scopeForSubject(Builder $query, $subject): Builder
78
    {
79
        return $query->where('subject_id', $subject->getKey());
80
    }
81
}
82