ChangeByUser::getDeletedByColumn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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