Completed
Pull Request — master (#326)
by
unknown
04:00 queued 02:38
created

Activity::getSubjectNameAttribute()   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 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Models;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Relations\MorphTo;
9
10
class Activity extends Model
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('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 changes(): Collection
47
    {
48
        if (! $this->properties instanceof Collection) {
49
            return new Collection();
50
        }
51
52
        return collect(array_filter($this->properties->toArray(), function ($key) {
53
            return in_array($key, ['attributes', 'old']);
54
        }, ARRAY_FILTER_USE_KEY));
55
    }
56
57
    public function scopeInLog(Builder $query, ...$logNames): Builder
58
    {
59
        if (is_array($logNames[0])) {
60
            $logNames = $logNames[0];
61
        }
62
63
        return $query->whereIn('log_name', $logNames);
64
    }
65
66
    /**
67
     * Scope a query to only include activities by a given causer.
68
     *
69
     * @param \Illuminate\Database\Eloquent\Builder $query
70
     * @param \Illuminate\Database\Eloquent\Model $causer
71
     *
72
     * @return \Illuminate\Database\Eloquent\Builder
73
     */
74
    public function scopeCausedBy(Builder $query, Model $causer): Builder
75
    {
76
        return $query
77
            ->where('causer_type', $causer->getMorphClass())
78
            ->where('causer_id', $causer->getKey());
79
    }
80
81
    /**
82
     * Scope a query to only include activities for a given subject.
83
     *
84
     * @param \Illuminate\Database\Eloquent\Builder $query
85
     * @param \Illuminate\Database\Eloquent\Model $subject
86
     *
87
     * @return \Illuminate\Database\Eloquent\Builder
88
     */
89
    public function scopeForSubject(Builder $query, Model $subject): Builder
90
    {
91
        return $query
92
            ->where('subject_type', $subject->getMorphClass())
93
            ->where('subject_id', $subject->getKey());
94
    }
95
96
    /**
97
     * Get the Subject Class Name without Namespace.
98
     * @return string
99
     */
100
    public function getSubjectNameAttribute()
101
    {
102
        return (new \ReflectionClass($this->subject))->getShortName();
103
    }
104
}
105