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

Database   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 32
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A audit() 0 5 1
A prune() 0 17 3
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