Completed
Push — master ( b7cfb7...fb974b )
by Freek
02:10
created

Activity::scopeOnLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
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
        return $this->morphTo();
23
    }
24
25
    public function causer(): MorphTo
26
    {
27
        return $this->morphTo();
28
    }
29
30
    /**
31
     * Get the extra properties with the given name.
32
     *
33
     * @param string $propertyName
34
     *
35
     * @return mixed
36
     */
37
    public function getExtraProperty(string $propertyName)
38
    {
39
        return array_get($this->properties, $propertyName);
40
    }
41
42
    public function getChangesAttribute(): Collection
43
    {
44
        return $this->properties->filter(function ($value, $key) {
45
            return in_array($key, ['attributes', 'old']);
46
        });
47
    }
48
    
49
    public function scopeOnLog(Builder $query, ...$logNames): Builder
50
    {
51
        if (is_array($logNames[0])) {
52
            $logNames = $logNames[0];
53
        }
54
55
        return $query->whereIn('log_name', $logNames);
56
    }
57
}
58