Completed
Push — master ( 6ffe9e...612694 )
by Gabriel
03:27
created

EmailsCleanupRecords::deleteByTypeQuery()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 2
nop 3
crap 3
1
<?php
2
3
namespace Nip\MailModule\Console\Commands;
4
5
use Nip\Database\Query\Delete;
6
use Nip\MailModule\Models\EmailsTable\EmailsTrait;
7
use Nip\Records\Locator\ModelLocator;
8
use Nip\Records\RecordManager;
9
10
/**
11
 * Class EmailsCleanupRecords
12
 * @package Nip\MailModule\Console\Commands
13
 */
14
class EmailsCleanupRecords
15
{
16
17
18 1
    public function handle()
19
    {
20
        /** @var EmailsTrait $emailsManager */
21 1
        $emailsManager = ModelLocator::get('emails');
22
23 1
        $types = $emailsManager::reduceEmailsByType();
24 1
        foreach ($types as $type => $days) {
25 1
            $this->deleteByType($emailsManager, $type, $days);
26
        }
27 1
    }
28
29
    /**
30
     * @param EmailsTrait|RecordManager $emailsManager
31
     * @param $type
32
     * @param $days
33
     */
34 1
    protected function deleteByType(RecordManager $emailsManager, string $type, $days)
35
    {
36 1
        $query = $this->deleteByTypeQuery($emailsManager, $type, $days);
37 1
        $query->execute();
38 1
    }
39
40
    /**
41
     * @param EmailsTrait|RecordManager $emailsManager
42
     * @param $type
43
     * @param $days
44
     * @return Delete
45
     */
46 1
    protected function deleteByTypeQuery($emailsManager, $type, $days)
47
    {
48 1
        $query = $emailsManager->newDeleteQuery();
0 ignored issues
show
Bug introduced by
The method newDeleteQuery does only exist in Nip\Records\RecordManager, but not in Nip\MailModule\Models\EmailsTable\EmailsTrait.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49 1
        if (is_string($type) && strlen($type) > 1) {
50 1
            $query->where('`type` = ?', $type);
51
        }
52 1
        $query->where(
53 1
            '`' . $emailsManager::getSentDateField() . '` <= DATE_SUB(CURRENT_DATE(), INTERVAL ' . $days . ' DAY)'
54
        );
55
56 1
        return $query;
57
    }
58
}
59