1 | <?php |
||
2 | |||
3 | namespace Azine\MailgunWebhooksBundle\Command; |
||
4 | |||
5 | use Azine\MailgunWebhooksBundle\Services\AzineMailgunService; |
||
6 | use Symfony\Component\Console\Command\Command; |
||
7 | use Symfony\Component\Console\Input\InputArgument; |
||
8 | use Symfony\Component\Console\Input\InputInterface; |
||
9 | use Symfony\Component\Console\Output\OutputInterface; |
||
10 | use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
||
11 | |||
12 | /** |
||
13 | * Delete old MailgunEvent entries from the database. |
||
14 | * |
||
15 | * @author dominik |
||
16 | */ |
||
17 | class DeleteOldEntriesCommand extends Command |
||
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 | dropped Mailgun has dropped the message due to some error. |
||
64 | EOF |
||
65 | ) |
||
66 | ; |
||
67 | 5 | } |
|
68 | |||
69 | 4 | protected function execute(InputInterface $input, OutputInterface $output) |
|
70 | { |
||
71 | 4 | $type = $input->getArgument('type'); |
|
72 | 4 | $ageLimit = $input->getArgument('date'); |
|
73 | |||
74 | 4 | if (null == $type || '' == $type) { |
|
75 | 2 | $output->write('deleting entries of any type.', true); |
|
76 | 2 | $typeDesc = 'any type'; |
|
77 | 2 | } elseif (array_search($type, array('accepted', 'rejected', 'delivered', 'failed', 'opened', 'clicked', 'unsubscribed', 'complained', 'stored', 'dropped'))) { |
|
78 | 1 | $typeDesc = "type '$type'"; |
|
79 | } else { |
||
80 | 1 | throw new InvalidArgumentException("Unknown type: $type"); |
|
81 | } |
||
82 | |||
83 | 3 | if (null == $ageLimit || '' == $ageLimit) { |
|
84 | 1 | $output->write("using default age-limit of '60 days ago'.", true); |
|
85 | 1 | $ageLimit = '60 days ago'; |
|
86 | } |
||
87 | |||
88 | 3 | $date = new \DateTime($ageLimit); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
89 | |||
90 | 3 | $result = $this->mailgunService->removeEvents($type, $date); |
|
91 | |||
92 | 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); |
|
93 | |||
94 | 3 | return 0; |
|
95 | } |
||
96 | } |
||
97 |