Test Failed
Push — master ( 1a9cc9...cac184 )
by Morten
25:35
created

AuditableObserver::fireDispatchingAuditEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use OwenIt\Auditing\Contracts\Auditable;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OwenIt\Auditing\Auditable. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use OwenIt\Auditing\Events\DispatchAudit;
7
use OwenIt\Auditing\Events\DispatchingAudit;
8
9
class AuditableObserver
10
{
11
    /**
12
     * Is the model being restored?
13
     *
14
     * @var bool
15
     */
16
    public static $restoring = false;
17
18
    /**
19
     * Handle the retrieved event.
20
     *
21
     * @param \OwenIt\Auditing\Contracts\Auditable $model
22
     *
23
     * @return void
24 54
     */
25
    public function retrieved(Auditable $model)
26 54
    {
27
        $this->dispatchAudit($model->setAuditEvent('retrieved'));
28
    }
29
30
    /**
31
     * Handle the created event.
32
     *
33
     * @param \OwenIt\Auditing\Contracts\Auditable $model
34
     *
35
     * @return void
36 134
     */
37
    public function created(Auditable $model)
38 134
    {
39
        $this->dispatchAudit($model->setAuditEvent('created'));
40
    }
41
42
    /**
43
     * Handle the updated event.
44
     *
45
     * @param \OwenIt\Auditing\Contracts\Auditable $model
46
     *
47
     * @return void
48 20
     */
49
    public function updated(Auditable $model)
50
    {
51 20
        // Ignore the updated event when restoring
52 18
        if (!static::$restoring) {
53
            $this->dispatchAudit($model->setAuditEvent('updated'));
54
        }
55
    }
56
57
    /**
58
     * Handle the deleted event.
59
     *
60
     * @param \OwenIt\Auditing\Contracts\Auditable $model
61
     *
62
     * @return void
63 6
     */
64
    public function deleted(Auditable $model)
65 6
    {
66
        $this->dispatchAudit($model->setAuditEvent('deleted'));
67
    }
68
69
    /**
70
     * Handle the restoring event.
71
     *
72
     * @param \OwenIt\Auditing\Contracts\Auditable $model
73
     *
74
     * @return void
75 4
     */
76
    public function restoring(Auditable $model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
    public function restoring(/** @scrutinizer ignore-unused */ Auditable $model)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        // When restoring a model, an updated event is also fired.
79
        // By keeping track of the main event that took place,
80 4
        // we avoid creating a second audit with wrong values
81
        static::$restoring = true;
82
    }
83
84
    /**
85
     * Handle the restored event.
86
     *
87
     * @param \OwenIt\Auditing\Contracts\Auditable $model
88
     *
89
     * @return void
90 4
     */
91
    public function restored(Auditable $model)
92 4
    {
93
        $this->dispatchAudit($model->setAuditEvent('restored'));
94
95
        // Once the model is restored, we need to put everything back
96 4
        // as before, in case a legitimate update event is fired
97
        static::$restoring = false;
98
    }
99
100
    protected function dispatchAudit(Auditable $model)
101
    {
102
        if (!$model->readyForAuditing() || !$this->fireDispatchingAuditEvent($model)) {
103
            return;
104
        }
105
106
        // Unload the relations to prevent large amounts of unnecessary data from being serialized.
107
        DispatchAudit::dispatch($model->preloadResolverData()->withoutRelations());
0 ignored issues
show
Bug introduced by
The method preloadResolverData() does not exist on OwenIt\Auditing\Contracts\Auditable. Since it exists in all sub-types, consider adding an abstract or default implementation to OwenIt\Auditing\Contracts\Auditable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        DispatchAudit::dispatch($model->/** @scrutinizer ignore-call */ preloadResolverData()->withoutRelations());
Loading history...
108
    }
109
110
    /**
111
     * Fire the Auditing event.
112
     *
113
     * @param \OwenIt\Auditing\Contracts\Auditable $model
114
     *
115
     * @return bool
116
     */
117
    protected function fireDispatchingAuditEvent(Auditable $model): bool
118
    {
119
        return app()->make('events')
120
                ->until(new DispatchingAudit($model)) !== false;
121
    }
122
}
123