Completed
Push — master ( ed39e1...5b6f4d )
by Freek
01:43
created

Activity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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;
13
14
    public $guarded = [];
15
16
    protected $casts = [
17
        'properties' => 'collection',
18
    ];
19
20
    public function __construct(array $attributes = [])
21
    {
22
        $this->table = config('activitylog.table_name');
23
24
        parent::__construct($attributes);
25
    }
26
27
    public function subject(): MorphTo
28
    {
29
        if (config('activitylog.subject_returns_soft_deleted_models')) {
30
            return $this->morphTo()->withTrashed();
31
        }
32
33
        return $this->morphTo();
34
    }
35
36
    public function causer(): MorphTo
37
    {
38
        return $this->morphTo();
39
    }
40
41
    /**
42
     * Get the extra properties with the given name.
43
     *
44
     * @param string $propertyName
45
     *
46
     * @return mixed
47
     */
48
    public function getExtraProperty(string $propertyName)
49
    {
50
        return array_get($this->properties->toArray(), $propertyName);
51
    }
52
53
    public function changes(): Collection
54
    {
55
        if (! $this->properties instanceof Collection) {
56
            return new Collection();
57
        }
58
59
        return collect(array_filter($this->properties->toArray(), function ($key) {
60
            return in_array($key, ['attributes', 'old']);
61
        }, ARRAY_FILTER_USE_KEY));
62
    }
63
64
    public function scopeInLog(Builder $query, ...$logNames): Builder
65
    {
66
        if (is_array($logNames[0])) {
67
            $logNames = $logNames[0];
68
        }
69
70
        return $query->whereIn('log_name', $logNames);
71
    }
72
73
    /**
74
     * Scope a query to only include activities by a given causer.
75
     *
76
     * @param \Illuminate\Database\Eloquent\Builder $query
77
     * @param \Illuminate\Database\Eloquent\Model $causer
78
     *
79
     * @return \Illuminate\Database\Eloquent\Builder
80
     */
81
    public function scopeCausedBy(Builder $query, Model $causer): Builder
82
    {
83
        return $query
84
            ->where('causer_type', $causer->getMorphClass())
85
            ->where('causer_id', $causer->getKey());
86
    }
87
88
    /**
89
     * Scope a query to only include activities for a given subject.
90
     *
91
     * @param \Illuminate\Database\Eloquent\Builder $query
92
     * @param \Illuminate\Database\Eloquent\Model $subject
93
     *
94
     * @return \Illuminate\Database\Eloquent\Builder
95
     */
96
    public function scopeForSubject(Builder $query, Model $subject): Builder
97
    {
98
        return $query
99
            ->where('subject_type', $subject->getMorphClass())
100
            ->where('subject_id', $subject->getKey());
101
    }
102
}
103