CleanActivitylogCommand   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 3
dl 0
loc 31
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 22 1
1
<?php
2
3
namespace Spatie\Activitylog;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Illuminate\Database\Eloquent\Builder;
8
9
class CleanActivitylogCommand extends Command
10
{
11
    protected $signature = 'activitylog:clean
12
                            {log? : (optional) The log name that will be cleaned.}
13
                            {--days= : (optional) Records older than this number of days will be cleaned.}';
14
15
    protected $description = 'Clean up old records from the activity log.';
16
17 7
    public function handle()
18
    {
19 7
        $this->comment('Cleaning activity log...');
20
21 7
        $log = $this->argument('log');
22
23 7
        $maxAgeInDays = $this->option('days') ?? config('activitylog.delete_records_older_than_days');
24
25 7
        $cutOffDate = Carbon::now()->subDays($maxAgeInDays)->format('Y-m-d H:i:s');
26
27 7
        $activity = ActivitylogServiceProvider::getActivityModelInstance();
28
29 7
        $amountDeleted = $activity::where('created_at', '<', $cutOffDate)
30
            ->when($log !== null, function (Builder $query) use ($log) {
31
                $query->inLog($log);
32 7
            })
33 7
            ->delete();
34
35 7
        $this->info("Deleted {$amountDeleted} record(s) from the activity log.");
36
37 7
        $this->comment('All done!');
38 7
    }
39
}
40