Passed
Pull Request — master (#594)
by Jeff
09:12
created

Database::audit()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 4
eloc 7
c 5
b 0
f 2
nc 3
nop 1
dl 0
loc 14
ccs 4
cts 8
cp 0.5
crap 6
rs 10
1
<?php
2
3
namespace OwenIt\Auditing\Drivers;
4
5
use Illuminate\Support\Facades\Config;
6
use OwenIt\Auditing\Contracts\Audit;
7
use OwenIt\Auditing\Contracts\Auditable;
8
use OwenIt\Auditing\Contracts\AuditDriver;
9
10
class Database implements AuditDriver
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 47
    public function audit(Auditable $model): Audit
16
    {
17 47
        $implementation = Config::get('audit.implementation', \OwenIt\Auditing\Models\Audit::class);
18
19
        // If we want to avoid storing Audits with empty old_values & new_values, return null here.
20 47
        if (! Config::get('audit.empty_values')) {
21
            $attributeGetter = $model->resolveAttributeGetter($model->getAuditEvent());
0 ignored issues
show
Bug introduced by
The method resolveAttributeGetter() does not exist on OwenIt\Auditing\Contracts\Auditable. Since it exists in all sub-types, consider adding an abstract or default implementation to OwenIt\Auditing\Contracts\Auditable. ( Ignorable by Annotation )

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

21
            /** @scrutinizer ignore-call */ 
22
            $attributeGetter = $model->resolveAttributeGetter($model->getAuditEvent());
Loading history...
22
            list($old, $new) = $model->$attributeGetter();
23
            if (empty($old) && empty($new)) {
24
                return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return OwenIt\Auditing\Contracts\Audit.
Loading history...
25
            }
26
        }
27
28 47
        return call_user_func([$implementation, 'create'], $model->toAudit());
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 47
    public function prune(Auditable $model): bool
35
    {
36 47
        if (($threshold = $model->getAuditThreshold()) > 0) {
37 1
            $forRemoval = $model->audits()
38 1
                ->latest()
39 1
                ->get()
40 1
                ->slice($threshold)
41 1
                ->pluck('id');
42
43 1
            if (!$forRemoval->isEmpty()) {
44 1
                return $model->audits()
45 1
                    ->whereIn('id', $forRemoval)
46 1
                    ->delete() > 0;
47
            }
48
        }
49
50 47
        return false;
51
    }
52
}
53