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

AuditableTraitObserver::getAuthenticatedUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
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
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