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); |
|
|
|
|
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(); |
|
|
|
|
55
|
1 |
|
if (is_string($type) && strlen($type) > 1) { |
56
|
1 |
|
$query->where('`type` = ?', $type); |
|
|
|
|
57
|
|
|
} |
58
|
1 |
|
$query->where( |
59
|
1 |
|
'`' . $emailsManager::getSentDateField() . '` <= DATE_SUB(CURRENT_DATE(), INTERVAL ' . $days . ' DAY)' |
|
|
|
|
60
|
|
|
); |
61
|
|
|
|
62
|
1 |
|
$query->limit(500); |
63
|
|
|
|
64
|
1 |
|
return $query; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|