Test Failed
Pull Request — master (#674)
by Orkhan
05:19
created

AuditableObserver::created()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Facades\Auditor;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OwenIt\Auditing\Auditor. 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...
7
8
class AuditableObserver
9
{
10
    /**
11
     * Is the model being restored?
12
     *
13
     * @var bool
14
     */
15
    public static $restoring = false;
16
17
    /**
18
     * Handle the retrieved event.
19
     *
20
     * @param \OwenIt\Auditing\Contracts\Auditable $model
21
     *
22
     * @return void
23
     */
24 6
    public function retrieved(Auditable $model)
25
    {
26 6
        Auditor::execute($model->setAuditEvent('retrieved'));
27 6
    }
28
29
    /**
30
     * Handle the created event.
31
     *
32
     * @param \OwenIt\Auditing\Contracts\Auditable $model
33
     *
34
     * @return void
35
     */
36 12
    public function created(Auditable $model)
37
    {
38 12
        Auditor::execute($model->setAuditEvent('created'));
39 12
    }
40
41
    /**
42
     * Handle the updated event.
43
     *
44
     * @param \OwenIt\Auditing\Contracts\Auditable $model
45
     *
46
     * @return void
47
     */
48
    public function updated(Auditable $model)
49
    {
50
        // Ignore the updated event when restoring
51
        if (!static::$restoring) {
52
            Auditor::execute($model->setAuditEvent('updated'));
53
        }
54
    }
55
56
    /**
57
     * Handle the deleted event.
58
     *
59
     * @param \OwenIt\Auditing\Contracts\Auditable $model
60
     *
61
     * @return void
62
     */
63
    public function deleted(Auditable $model)
64
    {
65
        Auditor::execute($model->setAuditEvent('deleted'));
66
    }
67
68
    /**
69
     * Handle the restoring event.
70
     *
71
     * @param \OwenIt\Auditing\Contracts\Auditable $model
72
     *
73
     * @return void
74
     */
75
    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

75
    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...
76
    {
77
        // When restoring a model, an updated event is also fired.
78
        // By keeping track of the main event that took place,
79
        // we avoid creating a second audit with wrong values
80
        static::$restoring = true;
81
    }
82
83
    /**
84
     * Handle the restored event.
85
     *
86
     * @param \OwenIt\Auditing\Contracts\Auditable $model
87
     *
88
     * @return void
89
     */
90
    public function restored(Auditable $model)
91
    {
92
        Auditor::execute($model->setAuditEvent('restored'));
93
94
        // Once the model is restored, we need to put everything back
95
        // as before, in case a legitimate update event is fired
96
        static::$restoring = false;
97
    }
98
}
99