LogsActivity::bootLogsActivity()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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