BlameableObserver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 3 1
A deleted() 0 9 5
A restoring() 0 3 1
A saving() 0 3 1
1
<?php
2
3
namespace RichanFongdasen\EloquentBlameable;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class BlameableObserver
8
{
9
    /**
10
     * Listening to any creating events.
11
     *
12
     * @param \Illuminate\Database\Eloquent\Model $model
13
     *
14
     * @return void
15
     */
16
    public function creating(Model $model): void
17
    {
18
        app(BlameableService::class)->setAttribute($model, 'createdBy', blameable_user($model));
19
    }
20
21
    /**
22
     * Listening to any deleted events.
23
     *
24
     * @param \Illuminate\Database\Eloquent\Model $model
25
     *
26
     * @return void
27
     */
28
    public function deleted(Model $model): void
29
    {
30
        app(BlameableService::class)->setAttribute($model, 'deletedBy', blameable_user($model));
31
32
        if (
33
            method_exists($model, 'useSoftDeletes') && method_exists($model, 'silentUpdate') &&
34
            $model->useSoftDeletes() && $model->isDirty()
35
        ) {
36
            $model->silentUpdate();
37
        }
38
    }
39
40
    /**
41
     * Listening to any restoring events.
42
     *
43
     * @param \Illuminate\Database\Eloquent\Model $model
44
     *
45
     * @return void
46
     */
47
    public function restoring(Model $model): void
48
    {
49
        app(BlameableService::class)->setAttribute($model, 'deletedBy', null);
50
    }
51
52
    /**
53
     * Listening to any saving events.
54
     *
55
     * @param \Illuminate\Database\Eloquent\Model $model
56
     *
57
     * @return void
58
     */
59
    public function saving(Model $model): void
60
    {
61
        app(BlameableService::class)->setAttribute($model, 'updatedBy', blameable_user($model));
62
    }
63
}
64