Passed
Pull Request — master (#712)
by Morten
15:08 queued 05:06
created

Auditor::fireAuditingEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OwenIt\Auditing;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Manager;
7
use InvalidArgumentException;
8
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...
9
use OwenIt\Auditing\Contracts\AuditDriver;
10
use OwenIt\Auditing\Drivers\Database;
11
use OwenIt\Auditing\Events\Audited;
12
use OwenIt\Auditing\Events\Auditing;
13
use OwenIt\Auditing\Exceptions\AuditingException;
14
15
class Auditor extends Manager implements Contracts\Auditor
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    public function getDefaultDriver()
21
    {
22 2
        return 'database';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 122
    protected function createDriver($driver)
29
    {
30
        try {
31 122
            return parent::createDriver($driver);
32 4
        } catch (InvalidArgumentException $exception) {
33 4
            if (class_exists($driver)) {
34 2
                return $this->container->make($driver);
35
            }
36
37 2
            throw $exception;
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 122
    public function auditDriver(Auditable $model): AuditDriver
45
    {
46 122
        $driver = $this->driver($model->getAuditDriver());
47
48 120
        if (!$driver instanceof AuditDriver) {
49 2
            throw new AuditingException('The driver must implement the AuditDriver contract');
50
        }
51
52 118
        return $driver;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 122
    public function execute(Auditable $model)
59
    {
60 122
        if (!$model->readyForAuditing()) {
61 60
            return;
62
        }
63
64 122
        $driver = $this->auditDriver($model);
65
66 118
        if (!$this->fireAuditingEvent($model, $driver)) {
67 2
            return;
68
        }
69
70
        // If we want to avoid storing Audits with empty old_values & new_values, return null here.
71 116
        if (!Config::get('audit.empty_values')) {
72
            if (
73 4
                empty($model->toAudit()['new_values']) &&
74 4
                empty($model->toAudit()['old_values']) &&
75 4
                !in_array($model->getAuditEvent(), Config::get('audit.allowed_empty_values'))
76
            ) {
77 2
                return;
78
            }
79
        }
80
81 116
        $audit = $driver->audit($model);
82 116
        if (!$audit) {
83
            return;
84
        }
85
86 116
        $driver->prune($model);
87
88 116
        $this->container->make('events')->dispatch(
89 116
            new Audited($model, $driver, $audit)
90
        );
91
    }
92
93
    /**
94
     * Create an instance of the Database audit driver.
95
     *
96
     * @return \OwenIt\Auditing\Drivers\Database
97
     */
98 118
    protected function createDatabaseDriver(): Database
99
    {
100 118
        return $this->container->make(Database::class);
101
    }
102
103
    /**
104
     * Fire the Auditing event.
105
     *
106
     * @param \OwenIt\Auditing\Contracts\Auditable $model
107
     * @param \OwenIt\Auditing\Contracts\AuditDriver $driver
108
     *
109
     * @return bool
110
     */
111 118
    protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool
112
    {
113
        return $this
114 118
                ->container
115 118
                ->make('events')
116 118
                ->until(new Auditing($model, $driver)) !== false;
117
    }
118
}
119