Completed
Push — master ( 94f95e...a17ed3 )
by Freek
02:08
created

LogsActivity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
B bootLogsActivity() 0 25 3
A causesActivity() 0 4 1
A getDescriptionForEvent() 0 4 1
A eventsToBeRecorded() 0 12 2
1
<?php
2
3
namespace Spatie\Activitylog\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\MorphTo;
7
use Spatie\Activitylog\ActivityLogger;
8
9
trait LogsActivity
10
{
11
    use DetectsChanges;
12
13
    protected static function bootLogsActivity()
14
    {
15
        collect(static::eventsToBeRecorded())->each(function ($eventName) {
16
17
            return static::$eventName(function (Model $model) use ($eventName) {
18
19
                $description = $model->getDescriptionForEvent($eventName);
20
21
                if ($description == '') {
22
                    return;
23
                }
24
25
                $extraProperties = [];
26
                if ($eventName != 'deleted') {
27
                    $extraProperties['changes'] = $model->getChangedValues();
28
                }
29
30
                app(ActivityLogger::class)
31
                    ->performedOn($model)
32
                    ->withExtraProperties($extraProperties)
33
                    ->log($description);
34
            });
35
36
        });
37
    }
38
39
    public function causesActivity(): MorphTo
40
    {
41
        return $this->morphTo();
0 ignored issues
show
Bug introduced by
It seems like morphTo() 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...
42
    }
43
44
    public function getDescriptionForEvent(string $eventName): string
45
    {
46
        return $eventName;
47
    }
48
49
    /*
50
     * Get the event names that should be recorded.
51
     */
52
    protected static function eventsToBeRecorded(): array
53
    {
54
        if (isset(static::$recordEvents)) {
55
            return static::$recordEvents;
56
        }
57
58
        return [
59
            'created',
60
            'updated',
61
            'deleted',
62
        ];
63
    }
64
}
65