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

AuditableTraitObserver::creating()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 4
nop 1
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 Arjay Angeles
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 Arjay Angeles
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