Passed
Push — master ( 16f5ef...10c43e )
by Morten
06:45 queued 14s
created

Auditor::createDriver()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 130
    protected function createDriver($driver)
29
    {
30
        try {
31 130
            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 130
    public function auditDriver(Auditable $model): AuditDriver
45
    {
46 130
        $driver = $this->driver($model->getAuditDriver());
47
48 128
        if (!$driver instanceof AuditDriver) {
49 2
            throw new AuditingException('The driver must implement the AuditDriver contract');
50
        }
51
52 126
        return $driver;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 130
    public function execute(Auditable $model)
59
    {
60 130
        if (!$model->readyForAuditing()) {
61 60
            return;
62
        }
63
64 130
        $driver = $this->auditDriver($model);
65
66 126
        if (!$this->fireAuditingEvent($model, $driver)) {
67 2
            return;
68
        }
69
70
        // Check if we want to avoid storing empty values
71 124
        $allowEmpty = Config::get('audit.empty_values');
72 124
        $explicitAllowEmpty = in_array($model->getAuditEvent(), Config::get('audit.allowed_empty_values', []));
73 124
        $explicitDisallowEmpty = in_array($model->getAuditEvent(), Config::get('audit.disallowed_empty_values', []));
74
75 124
        if ($explicitDisallowEmpty || (!$allowEmpty && !$explicitAllowEmpty)) {
76
            if (
77 8
                empty($model->toAudit()['new_values']) &&
78 8
                empty($model->toAudit()['old_values'])
79
            ) {
80 4
                return;
81
            }
82
        }
83
84 124
        $audit = $driver->audit($model);
85 124
        if (!$audit) {
86
            return;
87
        }
88
89 124
        $driver->prune($model);
90
91 124
        $this->container->make('events')->dispatch(
92 124
            new Audited($model, $driver, $audit)
93
        );
94
    }
95
96
    /**
97
     * Create an instance of the Database audit driver.
98
     *
99
     * @return \OwenIt\Auditing\Drivers\Database
100
     */
101 126
    protected function createDatabaseDriver(): Database
102
    {
103 126
        return $this->container->make(Database::class);
104
    }
105
106
    /**
107
     * Fire the Auditing event.
108
     *
109
     * @param \OwenIt\Auditing\Contracts\Auditable $model
110
     * @param \OwenIt\Auditing\Contracts\AuditDriver $driver
111
     *
112
     * @return bool
113
     */
114 126
    protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool
115
    {
116
        return $this
117 126
                ->container
118 126
                ->make('events')
119 126
                ->until(new Auditing($model, $driver)) !== false;
120
    }
121
}
122