These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\Activitylog\Traits; |
||
4 | |||
5 | use Illuminate\Support\Collection; |
||
6 | use Spatie\Activitylog\ActivityLogger; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Spatie\Activitylog\Models\Activity; |
||
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 static function bootLogsActivity() |
||
18 | { |
||
19 | static::eventsToBeRecorded()->each(function ($eventName) { |
||
20 | return static::$eventName(function (Model $model) use ($eventName) { |
||
21 | if (! $model->shouldLogEvent($eventName)) { |
||
22 | return; |
||
23 | } |
||
24 | |||
25 | $description = $model->getDescriptionForEvent($eventName); |
||
26 | |||
27 | $logName = $model->getLogNameToUse($eventName); |
||
28 | |||
29 | if ($description == '') { |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | app(ActivityLogger::class) |
||
34 | ->useLog($logName) |
||
35 | ->performedOn($model) |
||
36 | ->withProperties($model->attributeValuesToBeLogged($eventName)) |
||
37 | ->log($description); |
||
38 | }); |
||
39 | }); |
||
40 | } |
||
41 | |||
42 | public function activity(): MorphMany |
||
43 | { |
||
44 | return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject'); |
||
45 | } |
||
46 | |||
47 | public function getDescriptionForEvent(string $eventName): string |
||
48 | { |
||
49 | return $eventName; |
||
50 | } |
||
51 | |||
52 | public function getLogNameToUse(string $eventName = ''): string |
||
0 ignored issues
–
show
|
|||
53 | { |
||
54 | return config('laravel-activitylog.default_log_name'); |
||
55 | } |
||
56 | |||
57 | /* |
||
58 | * Get the event names that should be recorded. |
||
59 | */ |
||
60 | protected static function eventsToBeRecorded(): Collection |
||
61 | { |
||
62 | if (isset(static::$recordEvents)) { |
||
63 | return collect(static::$recordEvents); |
||
64 | } |
||
65 | |||
66 | $events = collect([ |
||
67 | 'created', |
||
68 | 'updated', |
||
69 | 'deleted', |
||
70 | ]); |
||
71 | |||
72 | if (collect(class_uses_recursive(__CLASS__))->contains(SoftDeletes::class)) { |
||
73 | $events->push('restored'); |
||
74 | } |
||
75 | |||
76 | return $events; |
||
77 | } |
||
78 | |||
79 | public function attributesToBeIgnored(): array |
||
80 | { |
||
81 | if (! isset(static::$ignoreChangedAttributes)) { |
||
82 | return []; |
||
83 | } |
||
84 | |||
85 | return static::$ignoreChangedAttributes; |
||
86 | } |
||
87 | |||
88 | protected function shouldLogEvent(string $eventName): bool |
||
89 | { |
||
90 | if (! in_array($eventName, ['created', 'updated'])) { |
||
91 | return true; |
||
92 | } |
||
93 | |||
94 | if (array_has($this->getDirty(), 'deleted_at')) { |
||
95 | if ($this->getDirty()['deleted_at'] === null) { |
||
96 | return false; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | //do not log update event if only ignored attributes are changed |
||
101 | return (bool) count(array_except($this->getDirty(), $this->attributesToBeIgnored())); |
||
102 | } |
||
103 | } |
||
104 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.