Completed
Push — master ( 514383...ca34c5 )
by Arjay
11:24 queued 34s
created

src/AuditableTraitObserver.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
16
     */
17
    public function creating($model)
18
    {
19
        if (! $model->created_by) {
20
            $model->created_by = $this->getAuthenticatedUserId();
21
        }
22
23
        if (! $model->updated_by) {
24
            $model->updated_by = $this->getAuthenticatedUserId();
25
        }
26
    }
27
28
    /**
29
     * Get authenticated user id depending on model's auth guard.
30
     *
31
     * @return int
32
     */
33
    protected function getAuthenticatedUserId()
34
    {
35
        return auth()->check() ? auth()->id() : 0;
36
    }
37
38
    /**
39
     * Model's updating event hook.
40
     *
41
     * @param \Yajra\Auditable\AuditableTrait $model
42
     */
43
    public function updating($model)
44
    {
45
        if (! $model->isDirty('updated_by')) {
0 ignored issues
show
It seems like isDirty() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
            $model->updated_by = $this->getAuthenticatedUserId();
47
        }
48
    }
49
}
50