Test Failed
Pull Request — master (#431)
by
unknown
06:58
created

Database::prune()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 12
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\Drivers;
16
17
use Illuminate\Support\Facades\Config;
18
use OwenIt\Auditing\Contracts\Audit;
19
use OwenIt\Auditing\Contracts\Auditable;
20
use OwenIt\Auditing\Contracts\AuditDriver;
21
22
class Database implements AuditDriver
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function audit(Auditable $model): Audit
28
    {
29
        $implementation = Config::get('audit.implementation', \OwenIt\Auditing\Models\Audit::class);
30
31
        return call_user_func([$implementation, 'create'], $model->toAudit());
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function prune(Auditable $model): bool
38
    {
39
        if (($threshold = $model->getAuditThreshold()) > 0) {
40
            $forRemoval = $model->audits()
41
                ->latest()
42
                ->get()
43
                ->slice($threshold)
44
                ->pluck('id');
45
46
            if (!$forRemoval->isEmpty()) {
47
                return $model->audits()
48
                    ->whereIn('id', $forRemoval)
49
                    ->delete() > 0;
50
            }
51
        }
52
53
        return false;
54
    }
55
}
56