Completed
Push — master ( 1084c2...91aba6 )
by Tom
16s queued 13s
created

Activity::scopeForEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\MorphTo;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
11
12
class Activity extends Model implements ActivityContract
13
{
14
    public $guarded = [];
15
16
    protected $casts = [
17
        'properties' => 'collection',
18
    ];
19
20 384
    public function __construct(array $attributes = [])
21
    {
22 384
        if (! isset($this->connection)) {
23 380
            $this->setConnection(config('activitylog.database_connection'));
24
        }
25
26 384
        if (! isset($this->table)) {
27 380
            $this->setTable(config('activitylog.table_name'));
28
        }
29
30 384
        parent::__construct($attributes);
31 384
    }
32
33 232
    public function subject(): MorphTo
34
    {
35 232
        if (config('activitylog.subject_returns_soft_deleted_models')) {
36 4
            return $this->morphTo()->withTrashed();
37
        }
38
39 228
        return $this->morphTo();
40
    }
41
42 48
    public function causer(): MorphTo
43
    {
44 48
        return $this->morphTo();
45
    }
46
47 16
    public function getExtraProperty(string $propertyName)
48
    {
49 16
        return Arr::get($this->properties->toArray(), $propertyName);
50
    }
51
52 148
    public function changes(): Collection
53
    {
54 148
        if (! $this->properties instanceof Collection) {
55
            return new Collection();
56
        }
57
58 148
        return $this->properties->only(['attributes', 'old']);
59
    }
60
61
    public function getChangesAttribute(): Collection
62
    {
63
        return $this->changes();
64
    }
65
66 16
    public function scopeInLog(Builder $query, ...$logNames): Builder
67
    {
68 16
        if (is_array($logNames[0])) {
69 4
            $logNames = $logNames[0];
70
        }
71
72 16
        return $query->whereIn('log_name', $logNames);
73
    }
74
75 8
    public function scopeCausedBy(Builder $query, Model $causer): Builder
76
    {
77
        return $query
78 8
            ->where('causer_type', $causer->getMorphClass())
79 8
            ->where('causer_id', $causer->getKey());
80
    }
81
82 8
    public function scopeForSubject(Builder $query, Model $subject): Builder
83
    {
84
        return $query
85 8
            ->where('subject_type', $subject->getMorphClass())
86 8
            ->where('subject_id', $subject->getKey());
87
    }
88
89
    public function scopeForEvent(Builder $query, string $event): Builder
90
    {
91
        return $query->where('event', $event);
92
    }
93
}
94