owen-it /
laravel-auditing
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace OwenIt\Auditing; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Facades\Config; |
||||||
| 6 | use OwenIt\Auditing\Contracts\Auditable; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 7 | use OwenIt\Auditing\Events\DispatchAudit; |
||||||
| 8 | use OwenIt\Auditing\Events\DispatchingAudit; |
||||||
| 9 | use OwenIt\Auditing\Facades\Auditor; |
||||||
|
0 ignored issues
–
show
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
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
|
|||||||
| 10 | |||||||
| 11 | class AuditableObserver |
||||||
| 12 | { |
||||||
| 13 | /** |
||||||
| 14 | * Is the model being restored? |
||||||
| 15 | * |
||||||
| 16 | * @var bool |
||||||
| 17 | */ |
||||||
| 18 | public static $restoring = false; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * Handle the retrieved event. |
||||||
| 22 | * |
||||||
| 23 | * @return void |
||||||
| 24 | */ |
||||||
| 25 | public function retrieved(Auditable $model) |
||||||
| 26 | { |
||||||
| 27 | 5 | $this->dispatchAudit($model->setAuditEvent('retrieved')); |
|||||
| 28 | } |
||||||
| 29 | 5 | ||||||
| 30 | /** |
||||||
| 31 | * Handle the created event. |
||||||
| 32 | * |
||||||
| 33 | * @return void |
||||||
| 34 | */ |
||||||
| 35 | public function created(Auditable $model) |
||||||
| 36 | { |
||||||
| 37 | $this->dispatchAudit($model->setAuditEvent('created')); |
||||||
| 38 | } |
||||||
| 39 | 15 | ||||||
| 40 | /** |
||||||
| 41 | 15 | * Handle the updated event. |
|||||
| 42 | * |
||||||
| 43 | * @return void |
||||||
| 44 | */ |
||||||
| 45 | public function updated(Auditable $model) |
||||||
| 46 | { |
||||||
| 47 | // Ignore the updated event when restoring |
||||||
| 48 | if (!static::$restoring) { |
||||||
| 49 | $this->dispatchAudit($model->setAuditEvent('updated')); |
||||||
| 50 | } |
||||||
| 51 | 4 | } |
|||||
| 52 | |||||||
| 53 | /** |
||||||
| 54 | 4 | * Handle the deleted event. |
|||||
| 55 | 3 | * |
|||||
| 56 | * @return void |
||||||
| 57 | */ |
||||||
| 58 | public function deleted(Auditable $model) |
||||||
| 59 | { |
||||||
| 60 | $this->dispatchAudit($model->setAuditEvent('deleted')); |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * Handle the restoring event. |
||||||
| 65 | * |
||||||
| 66 | 2 | * @return void |
|||||
| 67 | */ |
||||||
| 68 | 2 | public function restoring(Auditable $model) |
|||||
|
0 ignored issues
–
show
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
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...
|
|||||||
| 69 | { |
||||||
| 70 | // When restoring a model, an updated event is also fired. |
||||||
| 71 | // By keeping track of the main event that took place, |
||||||
| 72 | // we avoid creating a second audit with wrong values |
||||||
| 73 | static::$restoring = true; |
||||||
| 74 | } |
||||||
| 75 | |||||||
| 76 | /** |
||||||
| 77 | * Handle the restored event. |
||||||
| 78 | 1 | * |
|||||
| 79 | * @return void |
||||||
| 80 | */ |
||||||
| 81 | public function restored(Auditable $model) |
||||||
| 82 | { |
||||||
| 83 | 1 | $this->dispatchAudit($model->setAuditEvent('restored')); |
|||||
| 84 | |||||||
| 85 | // Once the model is restored, we need to put everything back |
||||||
| 86 | // as before, in case a legitimate update event is fired |
||||||
| 87 | static::$restoring = false; |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | protected function dispatchAudit(Auditable $model): void |
||||||
| 91 | { |
||||||
| 92 | if (!$model->readyForAuditing()) { |
||||||
| 93 | 1 | return; |
|||||
| 94 | } |
||||||
| 95 | 1 | ||||||
| 96 | // @phpstan-ignore method.notFound |
||||||
| 97 | $model->preloadResolverData(); |
||||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||||
| 98 | if (!Config::get('audit.queue.enable', false)) { |
||||||
| 99 | 1 | Auditor::execute($model); |
|||||
| 100 | return; |
||||||
| 101 | } |
||||||
| 102 | 15 | ||||||
| 103 | if (!$this->fireDispatchingAuditEvent($model)) { |
||||||
| 104 | 15 | return; |
|||||
| 105 | 8 | } |
|||||
| 106 | |||||||
| 107 | // Unload the relations to prevent large amounts of unnecessary data from being serialized. |
||||||
| 108 | 15 | $model->withoutRelations(); |
|||||
|
0 ignored issues
–
show
The method
withoutRelations() 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
Loading history...
|
|||||||
| 109 | 15 | app()->make('events')->dispatch(new DispatchAudit($model)); |
|||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | /** |
||||||
| 113 | 15 | * Fire the Auditing event. |
|||||
| 114 | */ |
||||||
| 115 | protected function fireDispatchingAuditEvent(Auditable $model): bool |
||||||
| 116 | { |
||||||
| 117 | return app()->make('events') |
||||||
| 118 | 15 | ->until(new DispatchingAudit($model)) !== false; |
|||||
| 119 | } |
||||||
| 120 | } |
||||||
| 121 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: