1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Laravel Auditing package. |
4
|
|
|
* |
5
|
|
|
* @author Antério Vieira <[email protected]> |
6
|
|
|
* @author Quetzy Garcia <[email protected]> |
7
|
|
|
* @author Raphael França <[email protected]> |
8
|
|
|
* @copyright 2015-2018 |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, |
11
|
|
|
* please view the LICENSE.md file that was distributed |
12
|
|
|
* with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace OwenIt\Auditing; |
16
|
|
|
|
17
|
|
|
use Illuminate\Support\Manager; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
20
|
|
|
use OwenIt\Auditing\Contracts\AuditDriver; |
21
|
|
|
use OwenIt\Auditing\Drivers\Database; |
22
|
|
|
use OwenIt\Auditing\Events\Audited; |
23
|
|
|
use OwenIt\Auditing\Events\Auditing; |
24
|
|
|
use OwenIt\Auditing\Exceptions\AuditingException; |
25
|
|
|
|
26
|
|
|
class Auditor extends Manager implements Contracts\Auditor |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getDefaultDriver() |
32
|
|
|
{ |
33
|
|
|
return 'database'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
protected function createDriver($driver) |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
return parent::createDriver($driver); |
43
|
|
|
} catch (InvalidArgumentException $exception) { |
44
|
|
|
if (class_exists($driver)) { |
45
|
|
|
return $this->app->make($driver); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
throw $exception; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function auditDriver(Auditable $model): AuditDriver |
56
|
|
|
{ |
57
|
|
|
$driver = $this->driver($model->getAuditDriver()); |
58
|
|
|
|
59
|
|
|
if (!$driver instanceof AuditDriver) { |
60
|
|
|
throw new AuditingException('The driver must implement the AuditDriver contract'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $driver; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function execute(Auditable $model) |
70
|
|
|
{ |
71
|
|
|
if (!$model->readyForAuditing()) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$driver = $this->auditDriver($model); |
76
|
|
|
|
77
|
|
|
if (!$this->fireAuditingEvent($model, $driver)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($audit = $driver->audit($model)) { |
82
|
|
|
$driver->prune($model); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->app->make('events')->fire( |
86
|
|
|
new Audited($model, $driver, $audit) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create an instance of the Database audit driver. |
92
|
|
|
* |
93
|
|
|
* @return \OwenIt\Auditing\Drivers\Database |
94
|
|
|
*/ |
95
|
|
|
protected function createDatabaseDriver(): Database |
96
|
|
|
{ |
97
|
|
|
return $this->app->make(Database::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Fire the Auditing event. |
102
|
|
|
* |
103
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
104
|
|
|
* @param \OwenIt\Auditing\Contracts\AuditDriver $driver |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool |
109
|
|
|
{ |
110
|
|
|
return $this->app->make('events')->until( |
111
|
|
|
new Auditing($model, $driver) |
112
|
|
|
) !== false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|