Completed
Push — master ( 685eb3...3e4363 )
by Freek
02:33
created

LogsActivity::bootLogsActivity()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 1
nop 0
dl 0
loc 20
rs 9.4285
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\MorphTo;
7
use Illuminate\Support\Collection;
8
use Spatie\Activitylog\ActivityLogger;
9
10
trait LogsActivity
11
{
12
    use DetectsChanges;
13
14
    protected static function bootLogsActivity()
15
    {
16
        collect(static::eventsToBeRecorded())->each(function ($eventName) {
17
18
            return static::$eventName(function (Model $model) use ($eventName) {
19
20
                $description = $model->getDescriptionForEvent($eventName);
21
22
                if ($description == '') {
23
                    return;
24
                }
25
26
                app(ActivityLogger::class)
27
                    ->performedOn($model)
28
                    ->withProperties($model->getPropertiesToBeLogged())
29
                    ->log($description);
30
            });
31
32
        });
33
    }
34
35
    public function causesActivity(): MorphTo
36
    {
37
        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...
38
    }
39
40
    public function getDescriptionForEvent(string $eventName): string
41
    {
42
        return $eventName;
43
    }
44
45
    /*
46
     * Get the event names that should be recorded.
47
     */
48
    protected static function eventsToBeRecorded(): Collection
49
    {
50
        if (isset(static::$recordEvents)) {
51
            return collect(static::$recordEvents);
52
        }
53
54
        return collect([
55
            'created',
56
            'updated',
57
            'deleted',
58
        ]);
59
    }
60
}
61