1
|
|
|
<?php |
2
|
|
|
namespace Azine\MailgunWebhooksBundle\Command; |
3
|
|
|
|
4
|
|
|
use Azine\MailgunWebhooksBundle\Services\AzineMailgunService; |
5
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Delete old MailgunEvent entries from the database |
13
|
|
|
* |
14
|
|
|
* @author dominik |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class DeleteOldEntriesCommand extends ContainerAwareCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string|null The default command name |
21
|
|
|
*/ |
22
|
|
|
protected static $defaultName = 'mailgun:delete-events'; |
23
|
|
|
|
24
|
|
|
/** @var AzineMailgunService */ |
25
|
|
|
private $mailgunService; |
26
|
|
|
|
27
|
5 |
|
public function __construct(AzineMailgunService $mailgunService) |
28
|
|
|
{ |
29
|
5 |
|
$this->mailgunService = $mailgunService; |
30
|
5 |
|
parent::__construct(); |
31
|
5 |
|
} |
32
|
|
|
|
33
|
5 |
|
protected function configure() |
34
|
|
|
{ |
35
|
5 |
|
$this->setName(static::$defaultName) |
36
|
5 |
|
->setDescription('Delete old mailgun events from the database') |
37
|
5 |
|
->setDefinition(array( |
38
|
5 |
|
new InputArgument( 'date', |
39
|
5 |
|
InputArgument::OPTIONAL, |
40
|
5 |
|
'Delete Mailgun Events that are older than "date" (Default: 60 days ago). The date must be something that strtotime() is able to parse: => e.g. "since yesterday", "until 2 days ago", "> now - 2 hours", ">= 2005-10-15" ' |
41
|
|
|
), |
42
|
5 |
|
new InputArgument( 'type', |
43
|
5 |
|
InputArgument::OPTIONAL, |
44
|
5 |
|
'Delete Mailgun Events of the given type. It no type is supplied, events of all types are deleted.' |
45
|
|
|
), |
46
|
|
|
)) |
47
|
5 |
|
->setHelp(<<<EOF |
48
|
5 |
|
The <info>mailgun:delete-events</info> command deletes old mailgun_event entries from the database. |
49
|
|
|
|
50
|
|
|
Possible types are: |
51
|
|
|
|
52
|
|
|
Event Description |
53
|
|
|
--------------------------------------------------------------------- |
54
|
|
|
accepted Mailgun accepted the request to send/forward the email and the message has been placed in queue. |
55
|
|
|
rejected Mailgun rejected the request to send/forward the email. |
56
|
|
|
delivered Mailgun sent the email and it was accepted by the recipient email server. |
57
|
|
|
failed Mailgun could not deliver the email to the recipient email server. |
58
|
|
|
opened The email recipient opened the email and enabled image viewing. Open tracking must be enabled in the Mailgun control panel, and the CNAME record must be pointing to mailgun.org. |
59
|
|
|
clicked The email recipient clicked on a link in the email. Click tracking must be enabled in the Mailgun control panel, and the CNAME record must be pointing to mailgun.org. |
60
|
|
|
unsubscribed The email recipient clicked on the unsubscribe link. Unsubscribe tracking must be enabled in the Mailgun control panel. |
61
|
|
|
complained The email recipient clicked on the spam complaint button within their email client. Feedback loops enable the notification to be received by Mailgun. |
62
|
|
|
stored Mailgun has stored an incoming message |
63
|
|
|
EOF |
64
|
|
|
) |
65
|
|
|
; |
66
|
5 |
|
} |
67
|
|
|
|
68
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
69
|
|
|
{ |
70
|
4 |
|
$type = $input->getArgument("type"); |
71
|
4 |
|
$ageLimit = $input->getArgument("date"); |
72
|
|
|
|
73
|
4 |
|
if ($type == null || $type == "") { |
74
|
2 |
|
$output->write("deleting entries of any type.", true); |
75
|
2 |
|
$typeDesc = "any type"; |
76
|
2 |
|
} elseif (array_search($type, array("accepted", "rejected", "delivered", "failed", "opened", "clicked", "unsubscribed", "complained", "stored"))) { |
77
|
1 |
|
$typeDesc = "type '$type'"; |
78
|
|
|
} else { |
79
|
1 |
|
throw new InvalidArgumentException("Unknown type: $type"); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
if ($ageLimit == null || $ageLimit == "") { |
83
|
1 |
|
$output->write("using default age-limit of '60 days ago'.", true); |
84
|
1 |
|
$ageLimit = "60 days ago"; |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
$date = new \DateTime($ageLimit); |
88
|
|
|
|
89
|
3 |
|
$result = $this->mailgunService->removeEvents($type, $date); |
90
|
|
|
|
91
|
3 |
|
$output->write("All MailgunEvents (& their CustomVariables & Attachments) older than ".$date->format("Y-m-d H:i:s")." of $typeDesc have been deleted ($result).", true); |
92
|
3 |
|
} |
93
|
|
|
} |
94
|
|
|
|