Completed
Pull Request — master (#641)
by
unknown
02:41
created

LogsActivity::shouldSubmitEmptyLogs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Traits;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Spatie\Activitylog\ActivityLogger;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\SoftDeletes;
10
use Spatie\Activitylog\ActivitylogServiceProvider;
11
use Illuminate\Database\Eloquent\Relations\MorphMany;
12
13
trait LogsActivity
14
{
15
    use DetectsChanges;
16
17
    protected $enableLoggingModelsEvents = true;
18
19 216
    protected static function bootLogsActivity()
20
    {
21
        static::eventsToBeRecorded()->each(function ($eventName) {
22
            return static::$eventName(function (Model $model) use ($eventName) {
23 212
                if (! $model->shouldLogEvent($eventName)) {
24 12
                    return;
25
                }
26
27 208
                $description = $model->getDescriptionForEvent($eventName);
28
29 208
                $logName = $model->getLogNameToUse($eventName);
30
31 208
                if ($description == '') {
32
                    return;
33
                }
34
35 208
                $attrs = $model->attributeValuesToBeLogged($eventName);
36
37 208
                if ($model->getLogIpAddress()) {
38
                    $attrs = array_merge($attrs, ["ip" => $_SERVER["REMOTE_ADDR"] ?? ""]);
39
                }
40
41 208
                if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) {
42 4
                    return;
43
                }
44
45 208
                $logger = app(ActivityLogger::class)
46 208
                    ->useLog($logName)
47 208
                    ->performedOn($model)
48 208
                    ->withProperties($attrs);
49
50 208
                if (method_exists($model, 'tapActivity')) {
51 8
                    $logger->tap([$model, 'tapActivity'], $eventName);
52
                }
53
54 208
                $logger->log($description);
55 216
            });
56 216
        });
57 216
    }
58
59 72
    public function shouldSubmitEmptyLogs(): bool
60
    {
61 72
        return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs;
62
    }
63
64 208
    public function getLogIpAddress(): bool
65
    {
66 208
        return ! isset(static::$logIpAdress) ? false : static::$logIpAdress;
67
    }
68
69 208
    public function isLogEmpty($attrs): bool
70
    {
71 208
        return empty($attrs['attributes'] ?? []) && empty($attrs['old'] ?? []);
72
    }
73
74 12
    public function disableLogging()
75
    {
76 12
        $this->enableLoggingModelsEvents = false;
77
78 12
        return $this;
79
    }
80
81 4
    public function enableLogging()
82
    {
83 4
        $this->enableLoggingModelsEvents = true;
84
85 4
        return $this;
86
    }
87
88 28
    public function activities(): MorphMany
89
    {
90 28
        return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), '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...
91
    }
92
93 204
    public function getDescriptionForEvent(string $eventName): string
94
    {
95 204
        return $eventName;
96
    }
97
98 204
    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...
99
    {
100 204
        if (isset(static::$logName)) {
101 4
            return static::$logName;
102
        }
103
104 200
        return config('activitylog.default_log_name');
105
    }
106
107
    /*
108
     * Get the event names that should be recorded.
109
     */
110 216
    protected static function eventsToBeRecorded(): Collection
111
    {
112 216
        if (isset(static::$recordEvents)) {
113
            return collect(static::$recordEvents);
114
        }
115
116 216
        $events = collect([
117 216
            'created',
118
            'updated',
119
            'deleted',
120
        ]);
121
122 216
        if (collect(class_uses_recursive(static::class))->contains(SoftDeletes::class)) {
123 76
            $events->push('restored');
124
        }
125
126 216
        return $events;
127
    }
128
129 208
    public function attributesToBeIgnored(): array
130
    {
131 208
        if (! isset(static::$ignoreChangedAttributes)) {
132 208
            return [];
133
        }
134
135
        return static::$ignoreChangedAttributes;
136
    }
137
138 212
    protected function shouldLogEvent(string $eventName): bool
139
    {
140 212
        if (! $this->enableLoggingModelsEvents) {
141 8
            return false;
142
        }
143
144 208
        if (! in_array($eventName, ['created', 'updated'])) {
145 24
            return true;
146
        }
147
148 208
        if (Arr::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...
149 4
            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...
150 4
                return false;
151
            }
152
        }
153
154
        //do not log update event if only ignored attributes are changed
155 208
        return (bool) count(Arr::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...
156
    }
157
}
158