CleanEmails::handle()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 3
eloc 17
c 5
b 2
f 0
nc 2
nop 0
dl 0
loc 34
rs 9.7
1
<?php
2
3
namespace BeyondCode\Mailbox\Console;
4
5
use BeyondCode\Mailbox\InboundEmail;
6
use Carbon\Carbon;
7
use Illuminate\Console\Command;
8
9
class CleanEmails extends Command
10
{
11
    protected $signature = 'mailbox:clean';
12
13
    protected $description = 'Clean up old incoming email logs.';
14
15
    protected $amountDeleted = 0;
16
17
    public function handle()
18
    {
19
        $this->comment('Cleaning old incoming email logs...');
20
21
        $maxAgeInDays = config('mailbox.store_incoming_emails_for_days');
22
23
        if ($maxAgeInDays === INF) {
24
            $this->error($this->signature.' is disabled because store_incoming_emails_for_days is set to INF.');
25
26
            return 1;
27
        }
28
29
        $cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
0 ignored issues
show
Unused Code introduced by
The call to Carbon\Carbon::subDay() has too many arguments starting with $maxAgeInDays. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $cutOffDate = Carbon::now()->/** @scrutinizer ignore-call */ subDay($maxAgeInDays)->format('Y-m-d H:i:s');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
30
31
        /** @var InboundEmail $modelClass */
32
        $modelClass = config('mailbox.model');
33
34
        // chunk the deletion to avoid memory issues
35
36
        $this->amountDeleted = 0;
37
38
        $modelClass::where('created_at', '<', $cutOffDate)
39
            ->select('id')
40
            ->chunk(100, function ($models) use ($modelClass) {
41
                foreach ($models as $model) {
42
                    $modelInstance = $modelClass::find($model->id);
43
                    $modelInstance->delete();
44
                    $this->amountDeleted++;
45
                }
46
            });
47
48
        $this->info("Deleted {$this->amountDeleted} record(s) from the Mailbox logs.");
49
50
        $this->comment('All done!');
51
    }
52
}
53