ChangeByUser::bootChangeByUser()   B
last analyzed

Complexity

Conditions 11
Paths 1

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 36
rs 7.3166
cc 11
nc 1
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TobyMaxham\Helper\Models\Logs;
4
5
use App;
6
use Auth;
7
8
/**
9
 * @author Tobias Maxham <[email protected]>
10
 */
11
trait ChangeByUser
12
{
13
    /**
14
     * automatically boot laravel model traits...
15
     */
16
    public static function bootChangeByUser()
17
    {
18
        static::creating(function ($model) {
19
            if (App::runningInConsole() || ! Auth::check()) {
20
                return true;
21
            }
22
            if ($model->getCreatedByColumn()) {
23
                $model->{$model->getUpdatedByColumn()} = Auth::user()->getKey();
24
            }
25
            if ($model->getUpdatedByColumn()) {
26
                $model->{$model->getUpdatedByColumn()} = Auth::user()->getKey();
27
            }
28
29
            return true;
30
        });
31
32
        static::updating(function ($model) {
33
            if (App::runningInConsole() || ! Auth::check()) {
34
                return true;
35
            }
36
            if ($model->getUpdatedByColumn()) {
37
                $model->{$model->getUpdatedByColumn()} = Auth::user()->getKey();
38
            }
39
40
            return true;
41
        });
42
43
        static::deleting(function ($model) {
44
            if (App::runningInConsole() || ! Auth::check()) {
45
                return true;
46
            }
47
            if ($model->getDeletedByColumn()) {
48
                $model->{$model->getDeletedByColumn()} = Auth::user()->getKey();
49
            }
50
51
            return true;
52
        });
53
    }
54
55
    public function getCreatedByColumn(): string
56
    {
57
        return 'created_by';
58
    }
59
60
    public function getUpdatedByColumn(): string
61
    {
62
        return 'updated_by';
63
    }
64
65
    public function getDeletedByColumn(): string
66
    {
67
        return 'deleted_by';
68
    }
69
}
70