Completed
Push — master ( 7749b7...460ddb )
by Freek
03:07
created

LogsActivity::shouldLogEvent()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Illuminate\Support\Collection;
8
use Spatie\Activitylog\ActivityLogger;
9
use Spatie\Activitylog\Models\Activity;
10
11
trait LogsActivity
12
{
13
    use DetectsChanges;
14
15
    protected static function bootLogsActivity()
16
    {
17
        static::eventsToBeRecorded()->each(function ($eventName) {
18
            return static::$eventName(function (Model $model) use ($eventName) {
19
                if (! $model->shouldLogEvent($eventName)) {
20
                    return;
21
                }
22
23
                $description = $model->getDescriptionForEvent($eventName);
24
25
                $logName = $model->getLogNameToUse($eventName);
26
27
                if ($description == '') {
28
                    return;
29
                }
30
31
                app(ActivityLogger::class)
32
                    ->useLog($logName)
33
                    ->performedOn($model)
34
                    ->withProperties($model->attributeValuesToBeLogged($eventName))
35
                    ->log($description);
36
            });
37
        });
38
    }
39
40
    public function activity(): MorphMany
41
    {
42
        return $this->morphMany(Activity::class, '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...
43
    }
44
45
    public function getDescriptionForEvent(string $eventName): string
46
    {
47
        return $eventName;
48
    }
49
50
    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...
51
    {
52
        return config('laravel-activitylog.default_log_name');
53
    }
54
55
    /*
56
     * Get the event names that should be recorded.
57
     */
58
    protected static function eventsToBeRecorded(): Collection
59
    {
60
        if (isset(static::$recordEvents)) {
61
            return collect(static::$recordEvents);
62
        }
63
64
        $events = collect([
65
            'created',
66
            'updated',
67
            'deleted',
68
        ]);
69
70
        if (collect(class_uses(__CLASS__))->contains(\Illuminate\Database\Eloquent\SoftDeletes::class)) {
71
            $events->push('restored');
72
        }
73
74
        return $events;
75
    }
76
77
    public function attributesToBeIgnored(): array
78
    {
79
        if (! isset(static::$ignoreChangedAttributes)) {
80
            return [];
81
        }
82
83
        return static::$ignoreChangedAttributes;
84
    }
85
86
    protected function shouldLogEvent(string $eventName): bool
87
    {
88
        if ($eventName === 'created' || $eventName === 'updated') {
89
            //do not log update event if model is restored
90
            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...
91
                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...
92
                    return false;
93
                }
94
            }
95
            //do not log update event if only ignored attributes are changed
96
            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...
97
        }
98
99
        return true;
100
    }
101
}
102