Completed
Push — master ( 95ddf8...c25dc0 )
by Freek
06:20
created

LogsActivity::attributesToBeIgnored()   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 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Traits;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Activitylog\ActivityLogger;
7
use Illuminate\Database\Eloquent\Model;
8
use Spatie\Activitylog\Models\Activity;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
use Spatie\Activitylog\ActivitylogServiceProvider;
11
use Illuminate\Database\Eloquent\Relations\MorphMany;
12
13
trait LogsActivity
14
{
15
    use DetectsChanges;
16
17
    protected static function bootLogsActivity()
18
    {
19
        static::eventsToBeRecorded()->each(function ($eventName) {
20
            return static::$eventName(function (Model $model) use ($eventName) {
21
                if (! $model->shouldLogEvent($eventName)) {
22
                    return;
23
                }
24
25
                $description = $model->getDescriptionForEvent($eventName);
26
27
                $logName = $model->getLogNameToUse($eventName);
28
29
                if ($description == '') {
30
                    return;
31
                }
32
33
                app(ActivityLogger::class)
34
                    ->useLog($logName)
35
                    ->performedOn($model)
36
                    ->withProperties($model->attributeValuesToBeLogged($eventName))
37
                    ->log($description);
38
            });
39
        });
40
    }
41
42
    public function activity(): MorphMany
43
    {
44
        return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
    }
46
47
    public function getDescriptionForEvent(string $eventName): string
48
    {
49
        return $eventName;
50
    }
51
52
    public function getLogNameToUse(string $eventName = ''): string
0 ignored issues
show
Unused Code introduced by
The parameter $eventName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54
        return config('laravel-activitylog.default_log_name');
55
    }
56
57
    /*
58
     * Get the event names that should be recorded.
59
     */
60
    protected static function eventsToBeRecorded(): Collection
61
    {
62
        if (isset(static::$recordEvents)) {
63
            return collect(static::$recordEvents);
64
        }
65
66
        $events = collect([
67
            'created',
68
            'updated',
69
            'deleted',
70
        ]);
71
72
        if (collect(class_uses_recursive(__CLASS__))->contains(SoftDeletes::class)) {
73
            $events->push('restored');
74
        }
75
76
        return $events;
77
    }
78
79
    public function attributesToBeIgnored(): array
80
    {
81
        if (! isset(static::$ignoreChangedAttributes)) {
82
            return [];
83
        }
84
85
        return static::$ignoreChangedAttributes;
86
    }
87
88
    protected function shouldLogEvent(string $eventName): bool
89
    {
90
        if (! in_array($eventName, ['created', 'updated'])) {
91
            return true;
92
        }
93
94
        if (array_has($this->getDirty(), 'deleted_at')) {
0 ignored issues
show
Bug introduced by
It seems like getDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
95
            if ($this->getDirty()['deleted_at'] === null) {
0 ignored issues
show
Bug introduced by
It seems like getDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
                return false;
97
            }
98
        }
99
100
        //do not log update event if only ignored attributes are changed
101
        return (bool) count(array_except($this->getDirty(), $this->attributesToBeIgnored()));
0 ignored issues
show
Bug introduced by
It seems like getDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
102
    }
103
}
104