Completed
Push — master ( 098c56...133368 )
by Arjay
15:29
created

AuditableTraitObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 0
cbo 1
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 10 3
A getAuthenticatedUserId() 0 4 2
A updating() 0 6 2
1
<?php
2
3
namespace Yajra\Auditable;
4
5
/**
6
 * Class AuditableObserver
7
 *
8
 * @package Yajra\Auditable
9
 */
10
class AuditableTraitObserver
11
{
12
    /**
13
     * Model's creating event hook.
14
     *
15
     * @param \Yajra\Auditable\AuditableTrait $model
0 ignored issues
show
introduced by
The type AuditableTrait for parameter $model is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
16
     */
17
    public function creating($model)
18
    {
19
        if (! $model->created_by) {
20
            $model->created_by = $this->getAuthenticatedUserId($model);
21
        }
22
23
        if (! $model->updated_by) {
24
            $model->updated_by = $this->getAuthenticatedUserId($model);
25
        }
26
    }
27
28
    /**
29
     * Get authenticated user id depending on model's auth guard.
30
     *
31
     * @param mixed $model
32
     * @return int
33
     */
34
    protected function getAuthenticatedUserId($model)
35
    {
36
        return auth($model->getAuthGuard())->check() ? auth($model->getAuthGuard())->id() : 0;
37
    }
38
39
    /**
40
     * Model's updating event hook.
41
     *
42
     * @param \Yajra\Auditable\AuditableTrait $model
0 ignored issues
show
introduced by
The type AuditableTrait for parameter $model is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
43
     */
44
    public function updating($model)
45
    {
46
        if (! $model->updated_by) {
47
            $model->updated_by = $this->getAuthenticatedUserId($model);
48
        }
49
    }
50
}
51