ActionLogTrait::storeActionLog()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 2
dl 0
loc 29
ccs 0
cts 24
cp 0
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Database\Eloquent\ActionLog;
12
13
use Auth;
14
15
trait ActionLogTrait
16
{
17
18
    public $actionLogging = true;
19
20
    /**
21
     * Boot the trait for the model.
22
     *
23
     * @return void
24
     */
25
    public static function bootActionLogTrait()
26
    {
27
28
        static::created(function ($model) {
29
            if (!empty($model->actionLogging)) {
30
                static::storeActionLog($model, 'create');
31
            }
32
        });
33
34
        static::updated(function ($model) {
35
            if (!empty($model->actionLogging)) {
36
                static::storeActionLog($model, 'update');
37
            }
38
        });
39
40
        static::deleted(function ($model) {
41
            if (!empty($model->actionLogging)) {
42
                static::storeActionLog($model, 'delete');
43
            }
44
        });
45
46
        if (method_exists(static::class, 'restored')) {
47
            static::restored(function ($model) {
48
                if (!empty($model->actionLogging)) {
49
                    static::storeActionLog($model, 'restore');
50
                }
51
            });
52
        }
53
    }
54
55
    public function withoutActionLogging()
56
    {
57
        $this->actionLogging = false;
58
        return $this;
59
    }
60
61
    protected static function storeActionLog($model, $action = 'read')
62
    {
63
        $actionLog = static::getActionLogModelInstance();
64
        $user_id   = Auth::user() ? Auth::user()->id : '';
65
66
        $before_data = $model->original;
67
        $after_data  = $model->attributes;
68
69
        $lang = app()->getLocale();
70
71
        $className = get_class($model);
72
73
        $data = [
74
            'user_id'      => $user_id,
75
            'lang'         => $lang,
76
            'entity'       => $className,
77
            'scope'        => app()->getScope(),
78
            'action'       => $action,
79
            'before_data'  => $before_data,
80
            'after_data'   => $after_data,
81
            'request_data' => app('request')->all(),
82
            'url'          => app('request')->fullUrl(),
83
            'referer'      => app('request')->server->get('HTTP_REFERER'),
84
            'ip_address'   => app('request')->ip(),
85
            'user_agent'   => app('request')->server->get('HTTP_USER_AGENT'),
86
        ];
87
88
        $actionLog->create($data);
89
    }
90
91
    protected static function getActionLogModelInstance()
92
    {
93
        $model = ActionLog::class;
94
        if (class_exists(\App\Models\ActionLog::class)) {
95
            $model = \App\Models\ActionLog::class;
96
        }
97
        return new $model;
98
    }
99
100
}
101