Issues (35)

src/Auditor.php (5 issues)

1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Support\Manager;
6
use InvalidArgumentException;
7
use OwenIt\Auditing\Contracts\Auditable;
0 ignored issues
show
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...
8
use OwenIt\Auditing\Contracts\AuditDriver;
9
use OwenIt\Auditing\Drivers\Database;
10
use OwenIt\Auditing\Events\Audited;
11
use OwenIt\Auditing\Events\Auditing;
12
use OwenIt\Auditing\Exceptions\AuditingException;
13
14
class Auditor extends Manager implements Contracts\Auditor
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public function getDefaultDriver()
20
    {
21 1
        return 'database';
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 50
    protected function createDriver($driver)
28
    {
29
        try {
30 50
            return parent::createDriver($driver);
31 2
        } catch (InvalidArgumentException $exception) {
32 2
            if (class_exists($driver)) {
33 1
                return $this->app->make($driver);
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Support\Manager::$app has been deprecated: Use the $container property instead. ( Ignorable by Annotation )

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

33
                return /** @scrutinizer ignore-deprecated */ $this->app->make($driver);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
34
            }
35
36 1
            throw $exception;
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 50
    public function auditDriver(Auditable $model): AuditDriver
44
    {
45 50
        $driver = $this->driver($model->getAuditDriver());
46
47 49
        if (!$driver instanceof AuditDriver) {
48 1
            throw new AuditingException('The driver must implement the AuditDriver contract');
49
        }
50
51 48
        return $driver;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 50
    public function execute(Auditable $model)
58
    {
59 50
        if (!$model->readyForAuditing()) {
60 25
            return;
61
        }
62
63 50
        $driver = $this->auditDriver($model);
64
65 48
        if (!$this->fireAuditingEvent($model, $driver)) {
66 1
            return;
67
        }
68
69 47
        if ($audit = $driver->audit($model)) {
70 47
            $driver->prune($model);
71
        }
72
73 47
        $this->app->make('events')->dispatch(
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Support\Manager::$app has been deprecated: Use the $container property instead. ( Ignorable by Annotation )

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

73
        /** @scrutinizer ignore-deprecated */ $this->app->make('events')->dispatch(

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
74 47
            new Audited($model, $driver, $audit)
75
        );
76 47
    }
77
78
    /**
79
     * Create an instance of the Database audit driver.
80
     *
81
     * @return \OwenIt\Auditing\Drivers\Database
82
     */
83 48
    protected function createDatabaseDriver(): Database
84
    {
85 48
        return $this->app->make(Database::class);
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Support\Manager::$app has been deprecated: Use the $container property instead. ( Ignorable by Annotation )

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

85
        return /** @scrutinizer ignore-deprecated */ $this->app->make(Database::class);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
86
    }
87
88
    /**
89
     * Fire the Auditing event.
90
     *
91
     * @param \OwenIt\Auditing\Contracts\Auditable   $model
92
     * @param \OwenIt\Auditing\Contracts\AuditDriver $driver
93
     *
94
     * @return bool
95
     */
96 48
    protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool
97
    {
98 48
        return $this->app->make('events')->until(
0 ignored issues
show
Deprecated Code introduced by
The property Illuminate\Support\Manager::$app has been deprecated: Use the $container property instead. ( Ignorable by Annotation )

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

98
        return /** @scrutinizer ignore-deprecated */ $this->app->make('events')->until(

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
99 48
            new Auditing($model, $driver)
100 48
        ) !== false;
101
    }
102
}
103