Passed
Push — master ( c34076...703e6b )
by Tobias
03:30
created

ChangeByUser::getCreatedByColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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