Completed
Push — master ( b01581...4b6f45 )
by Freek
04:00
created

CleanActivitylogCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use Spatie\Activitylog\Models\Activity;
8
9
class CleanActivitylogCommand extends Command
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'activitylog:clean';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Clean up old records from the activity log.';
24
25
    public function handle()
26
    {
27
        $this->comment('Cleaning activity log...');
28
29
        $maxAgeInDays = config('laravel-activitylog.delete_records_older_than_days');
30
31
        $cutOffDate = Carbon::now()->subDays($maxAgeInDays)->format('Y-m-d H:i:s');
32
33
        $amountDeleted = Activity::where('created_at', '<', $cutOffDate)->delete();
34
35
        $this->info("Deleted {$amountDeleted} record(s) from the activity log.");
36
37
        $this->comment('All done!');
38
    }
39
}
40