Passed
Push — master ( 38224a...a3a793 )
by Gabriel
02:06
created

ClearOldEmails::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 5
c 1
b 1
f 1
nc 3
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Giuga\LaravelMailLog\Commands;
4
5
use Carbon\Carbon;
6
use Giuga\LaravelMailLog\Models\MailLog;
7
use Illuminate\Console\Command;
8
9
class ClearOldEmails extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'giuga:purge-mail-log';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Remove mails saved before the time defined in the config';
24
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        if (config('mail-log.purge')) {
43
            $beforeDate = Carbon::now()->subDays(config('mail-log.purge_after'));
44
            $toRemove = MailLog::where('created_at', '<', $beforeDate)->get();
45
            foreach ($toRemove as $item) {
46
                $item->delete();
47
            }
48
        }
49
    }
50
}
51