Completed
Push — master ( 7361a2...a254fd )
by Richan
01:26
created

BlameableObserver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 4 1
A restoring() 0 4 1
A saving() 0 4 1
A deleted() 0 8 3
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)
17
    {
18
        app(BlameableService::class)->setAttribute($model, 'createdBy');
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)
29
    {
30
        app(BlameableService::class)->setAttribute($model, 'deletedBy');
31
32
        if ($model->useSoftDeletes() && $model->isDirty()) {
33
            $model->silentUpdate();
34
        }
35
    }
36
37
    /**
38
     * Listening to any restoring events.
39
     *
40
     * @param \Illuminate\Database\Eloquent\Model $model
41
     *
42
     * @return void
43
     */
44
    public function restoring(Model $model)
45
    {
46
        app(BlameableService::class)->setAttribute($model, 'deletedBy', true);
47
    }
48
49
    /**
50
     * Listening to any saving events.
51
     *
52
     * @param \Illuminate\Database\Eloquent\Model $model
53
     *
54
     * @return void
55
     */
56
    public function saving(Model $model)
57
    {
58
        app(BlameableService::class)->setAttribute($model, 'updatedBy');
59
    }
60
}
61