Passed
Push — master ( 7c5be3...871cbe )
by Gabriel
04:33
created

EmailsCleanupRecords::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Nip\MailModule\Console\Commands;
4
5
use Nip\Database\Query\Delete;
6
use Nip\Database\Result;
7
use Nip\MailModule\Models\EmailsTable\EmailsTrait;
8
use Nip\Records\Locator\ModelLocator;
9
use Nip\Records\RecordManager;
10
11
/**
12
 * Class EmailsCleanupRecords
13
 * @package Nip\MailModule\Console\Commands
14
 */
15
class EmailsCleanupRecords
16
{
17 1
    public function handle()
18
    {
19
        /** @var EmailsTrait $emailsManager */
20 1
        $emailsManager = ModelLocator::get('emails');
21
22 1
        $types = $emailsManager::reduceEmailsByType();
23 1
        $records = 0;
24 1
        foreach ($types as $type => $days) {
25 1
            $records += $this->deleteByType($emailsManager, $type, $days);
0 ignored issues
show
Bug introduced by
$emailsManager of type Nip\MailModule\Models\EmailsTable\EmailsTrait is incompatible with the type Nip\Records\RecordManager expected by parameter $emailsManager of Nip\MailModule\Console\C...Records::deleteByType(). ( Ignorable by Annotation )

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

25
            $records += $this->deleteByType(/** @scrutinizer ignore-type */ $emailsManager, $type, $days);
Loading history...
26
        }
27 1
        return $records;
28
    }
29
30
    /**
31
     * @param EmailsTrait|RecordManager $emailsManager
32
     * @param $type
33
     * @param $days
34
     * @return bool|int
35
     */
36 1
    protected function deleteByType(RecordManager $emailsManager, string $type, $days)
37
    {
38 1
        $query = $this->deleteByTypeQuery($emailsManager, $type, $days);
39 1
        $result = $query->execute();
40 1
        if ($result instanceof Result && $result->isValid()) {
41
            return $result->numRows();
42
        }
43 1
        return 0;
44
    }
45
46
    /**
47
     * @param EmailsTrait|RecordManager $emailsManager
48
     * @param $type
49
     * @param $days
50
     * @return Delete
51
     */
52 1
    protected function deleteByTypeQuery($emailsManager, $type, $days)
53
    {
54 1
        $query = $emailsManager->newDeleteQuery();
0 ignored issues
show
Bug introduced by
It seems like newDeleteQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

54
        /** @scrutinizer ignore-call */ 
55
        $query = $emailsManager->newDeleteQuery();
Loading history...
55 1
        if (is_string($type) && strlen($type) > 1) {
56 1
            $query->where('`type` = ?', $type);
0 ignored issues
show
Bug introduced by
$type of type string is incompatible with the type array expected by parameter $values of Nip\Database\Query\AbstractQuery::where(). ( Ignorable by Annotation )

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

56
            $query->where('`type` = ?', /** @scrutinizer ignore-type */ $type);
Loading history...
57
        }
58 1
        $query->where(
59 1
            '`' . $emailsManager::getSentDateField() . '` <= DATE_SUB(CURRENT_DATE(), INTERVAL ' . $days . ' DAY)'
0 ignored issues
show
introduced by
The method getSentDateField() does not exist on Nip\Records\RecordManager. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

59
            '`' . $emailsManager::/** @scrutinizer ignore-call */ getSentDateField() . '` <= DATE_SUB(CURRENT_DATE(), INTERVAL ' . $days . ' DAY)'
Loading history...
60
        );
61
62 1
        $query->limit(500);
63
64 1
        return $query;
65
    }
66
}
67