Completed
Push — master ( 137539...bddb3e )
by Freek
02:36
created

LogsActivity::getLogToUse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
19
            return static::$eventName(function (Model $model) use ($eventName) {
20
21
                $description = $model->getDescriptionForEvent($eventName);
22
23
                $logName = $model->getLogToUse();
24
25
                if ($description == '') {
26
                    return;
27
                }
28
29
                app(ActivityLogger::class)
30
                    ->useLog($logName)
31
                    ->performedOn($model)
32
                    ->withProperties($model->attributeValuesToBeLogged($eventName))
33
                    ->log($description);
34
            });
35
36
        });
37
    }
38
39
    public function activity(): MorphMany
40
    {
41
        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...
42
    }
43
44
    public function getDescriptionForEvent(string $eventName): string
45
    {
46
        return $eventName;
47
    }
48
49
    public function getLogToUse(): string
50
    {
51
        return config('laravel-activitylog.default_log_name');
52
    }
53
54
    /*
55
     * Get the event names that should be recorded.
56
     */
57
    protected static function eventsToBeRecorded(): Collection
58
    {
59
        if (isset(static::$recordEvents)) {
60
            return collect(static::$recordEvents);
61
        }
62
63
        return collect([
64
            'created',
65
            'updated',
66
            'deleted',
67
        ]);
68
    }
69
}
70