Test Failed
Pull Request — master (#604)
by recca
08:21
created

Auditor   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 73.32%

Importance

Changes 5
Bugs 3 Features 0
Metric Value
eloc 25
c 5
b 3
f 0
dl 0
loc 87
ccs 22
cts 30
cp 0.7332
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createDatabaseDriver() 0 3 1
A fireAuditingEvent() 0 5 1
A auditDriver() 0 9 2
A createDriver() 0 10 3
A getDefaultDriver() 0 3 1
A execute() 0 18 4
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
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...
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
    public function getDefaultDriver()
20
    {
21
        return 'database';
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 6
    protected function createDriver($driver)
28
    {
29
        try {
30 6
            return parent::createDriver($driver);
31
        } catch (InvalidArgumentException $exception) {
32
            if (class_exists($driver)) {
33
                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
            throw $exception;
37
        }
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 6
    public function auditDriver(Auditable $model): AuditDriver
44
    {
45 6
        $driver = $this->driver($model->getAuditDriver());
46
47 6
        if (!$driver instanceof AuditDriver) {
48
            throw new AuditingException('The driver must implement the AuditDriver contract');
49
        }
50
51 6
        return $driver;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 6
    public function execute(Auditable $model)
58
    {
59 6
        if (!$model->readyForAuditing()) {
60 2
            return;
61
        }
62
63 6
        $driver = $this->auditDriver($model);
64
65 6
        if (!$this->fireAuditingEvent($model, $driver)) {
66
            return;
67
        }
68
69 6
        if ($audit = $driver->audit($model)) {
70 6
            $driver->prune($model);
71
        }
72
73 6
        $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 6
            new Audited($model, $driver, $audit)
75
        );
76 6
    }
77
78
    /**
79
     * Create an instance of the Database audit driver.
80
     *
81
     * @return \OwenIt\Auditing\Drivers\Database
82
     */
83 6
    protected function createDatabaseDriver(): Database
84
    {
85 6
        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 6
    protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool
97
    {
98 6
        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 6
            new Auditing($model, $driver)
100 6
        ) !== false;
101
    }
102
}
103