Passed
Push — master ( 843803...c7b481 )
by Ruud
06:53
created

UserStampObserver::getUsersPrimaryValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
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
        $model->{config('userstamps.created_by_column')} = $this->getUsersPrimaryValue();
19
        $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
20
    }
21
22
    /**
23
     * Handle to the User "updating" event.
24
     *
25
     * @param  \Illuminate\Database\Eloquent\Model  $model
26
     * @return void
27
     */
28
    public function updating(Model $model)
29
    {
30
        $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
31
    }
32
33
    /**
34
     * Handle to the User "deleting" event.
35
     *
36
     * @param  \Illuminate\Database\Eloquent\Model  $model
37
     * @return void
38
     */
39
    public function deleting(Model $model)
40
    {
41
        if ($model->usingSoftDeletes()) {
42
            $model->{config('userstamps.deleted_by_column')} = $this->getUsersPrimaryValue();
43
            $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
44
            $this->saveWithoutEventDispatching($model);
45
        }
46
    }
47
48
    /**
49
     * Handle to the User "restoring" event.
50
     *
51
     * @param  \Illuminate\Database\Eloquent\Model  $model
52
     * @return void
53
     */
54
    public function restoring(Model $model)
55
    {
56
        if ($model->usingSoftDeletes()) {
57
            $model->{config('userstamps.deleted_by_column')} = null;
58
            $model->{config('userstamps.updated_by_column')} = $this->getUsersPrimaryValue();
59
            $this->saveWithoutEventDispatching($model);
60
        }
61
    }
62
63
    /**
64
     * Saves a model by igoring all other event dispatchers.
65
     *
66
     * @param  \Illuminate\Database\Eloquent\Model  $model
67
     * @return bool
68
     */
69
    private function saveWithoutEventDispatching(Model $model)
70
    {
71
        $eventDispatcher = $model->getEventDispatcher();
72
73
        $model->unsetEventDispatcher();
74
        $saved = $model->save();
75
        $model->setEventDispatcher($eventDispatcher);
76
77
        return $saved;
78
    }
79
80
    /**
81
     * Returns the primary value for the logged in user or null when
82
     * there is no logged in user.
83
     *
84
     * @return int|null
85
     */
86
    private function getUsersPrimaryValue()
87
    {
88
        if (! Auth::check()) {
89
            return null;
90
        }
91
92
        if (config('userstamps.users_table_column_id_name') !== 'id') {
93
            return Auth::user()->{config('userstamps.users_table_column_id_name')};
94
        }
95
96
        return Auth::id();
97
    }
98
}
99