Test Failed
Pull Request — master (#674)
by Orkhan
05:19
created

Database::prune()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.7968

Importance

Changes 5
Bugs 0 Features 2
Metric Value
cc 3
eloc 11
c 5
b 0
f 2
nc 3
nop 1
dl 0
loc 17
ccs 3
cts 12
cp 0.25
crap 6.7968
rs 9.9
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 12
    public function audit(Auditable $model): Audit
16
    {
17 12
        $implementation = Config::get('audit.implementation', \OwenIt\Auditing\Models\Audit::class);
18
19 12
        return call_user_func([$implementation, 'create'], $model->toAudit());
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 12
    public function prune(Auditable $model): bool
26
    {
27 12
        if (($threshold = $model->getAuditThreshold()) > 0) {
28
            $forRemoval = $model->audits()
29
                ->latest()
30
                ->get()
31
                ->slice($threshold)
32
                ->pluck('id');
33
34
            if (!$forRemoval->isEmpty()) {
35
                return $model->audits()
36
                    ->whereIn('id', $forRemoval)
37
                    ->delete() > 0;
38
            }
39
        }
40
41 12
        return false;
42
    }
43
}
44