Completed
Push — testDev1-rebased ( b23925 )
by Dominik
02:35
created

DeleteOldEntriesCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 6
dl 0
loc 77
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B configure() 0 34 1
B execute() 0 25 6
1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\Command;
4
5
use Azine\MailgunWebhooksBundle\Services\AzineMailgunService;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 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
    public function __construct(AzineMailgunService $mailgunService)
28
    {
29
        $this->mailgunService = $mailgunService;
30
        parent::__construct();
31
    }
32
33
    protected function configure()
34
    {
35
        $this->setName(static::$defaultName)
36
            ->setDescription('Delete old mailgun events from the database')
37
            ->setDefinition(array(
38
                                    new InputArgument('date',
39
                                                        InputArgument::OPTIONAL,
40
                                                        '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
                                    new InputArgument('type',
43
                                                        InputArgument::OPTIONAL,
44
                                                        'Delete Mailgun Events of the given type. It no type is supplied, events of all types are deleted.'
45
                                                    ),
46
                            ))
47
            ->setHelp(<<<EOF
48
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
    }
67
68
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70
        $type = $input->getArgument('type');
71
        $ageLimit = $input->getArgument('date');
72
73
        if (null == $type || '' == $type) {
74
            $output->write('deleting entries of any type.', true);
75
            $typeDesc = 'any type';
76
        } elseif (array_search($type, array('accepted', 'rejected', 'delivered', 'failed', 'opened', 'clicked', 'unsubscribed', 'complained', 'stored'))) {
77
            $typeDesc = "type '$type'";
78
        } else {
79
            throw new InvalidArgumentException("Unknown type: $type");
80
        }
81
82
        if (null == $ageLimit || '' == $ageLimit) {
83
            $output->write("using default age-limit of '60 days ago'.", true);
84
            $ageLimit = '60 days ago';
85
        }
86
87
        $date = new \DateTime($ageLimit);
88
89
        $result = $this->mailgunService->removeEvents($type, $date);
90
91
        $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
    }
93
}
94