UserStampObserver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 25
c 4
b 0
f 0
dl 0
loc 96
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updating() 0 4 2
A getUsersPrimaryValue() 0 11 3
A saveWithoutEventDispatching() 0 9 1
A deleting() 0 6 2
A creating() 0 8 3
A restoring() 0 6 2
1
<?php
2
3
namespace Sqits\UserStamps\Observers;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Auth;
7
8
class UserStampObserver
9
{
10
    /**
11
     * Handle to the User "creating" event.
12
     *
13
     * @param  \Illuminate\Database\Eloquent\Model  $model
14
     * @return void
15
     */
16
    public function creating(Model $model)
17
    {
18
        if ($model->isClean(config('userstamps.created_by_column'))) {
19
            $model->{config('userstamps.created_by_column')} = $this->getUsersPrimaryValue();
20
        }
21
22
        if ($model->isClean(config('userstamps.updated_by_column'))) {
23
            $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
24
        }
25
    }
26
27
    /**
28
     * Handle to the User "updating" event.
29
     *
30
     * @param  \Illuminate\Database\Eloquent\Model  $model
31
     * @return void
32
     */
33
    public function updating(Model $model)
34
    {
35
        if ($model->isClean(config('userstamps.updated_by_column'))) {
36
            $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
37
        }
38
    }
39
40
    /**
41
     * Handle to the User "deleting" event.
42
     *
43
     * @param  \Illuminate\Database\Eloquent\Model  $model
44
     * @return void
45
     */
46
    public function deleting(Model $model)
47
    {
48
        if ($model->usingSoftDeletes()) {
49
            $model->{config('userstamps.deleted_by_column')} = $this->getUsersPrimaryValue();
50
            $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
51
            $this->saveWithoutEventDispatching($model);
52
        }
53
    }
54
55
    /**
56
     * Handle to the User "restoring" event.
57
     *
58
     * @param  \Illuminate\Database\Eloquent\Model  $model
59
     * @return void
60
     */
61
    public function restoring(Model $model)
62
    {
63
        if ($model->usingSoftDeletes()) {
64
            $model->{config('userstamps.deleted_by_column')} = null;
65
            $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
66
            $this->saveWithoutEventDispatching($model);
67
        }
68
    }
69
70
    /**
71
     * Saves a model by ignoring all other event dispatchers.
72
     *
73
     * @param  \Illuminate\Database\Eloquent\Model  $model
74
     * @return bool
75
     */
76
    private function saveWithoutEventDispatching(Model $model)
77
    {
78
        $eventDispatcher = $model->getEventDispatcher();
79
80
        $model->unsetEventDispatcher();
81
        $saved = $model->save();
82
        $model->setEventDispatcher($eventDispatcher);
83
84
        return $saved;
85
    }
86
87
    /**
88
     * Returns the primary value for the logged in user or null when
89
     * there is no logged in user.
90
     *
91
     * @return int|null
92
     */
93
    private function getUsersPrimaryValue()
94
    {
95
        if (! Auth::check()) {
96
            return null;
97
        }
98
99
        if (config('userstamps.users_table_column_id_name') !== 'id') {
100
            return Auth::user()->{config('userstamps.users_table_column_id_name')};
101
        }
102
103
        return Auth::id();
104
    }
105
}
106