| 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 |  |  |                                                         '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 | 5 |  |                                                     ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 5 |  |                                     new InputArgument('type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 5 |  |                                                         InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                                                         'Delete Mailgun Events of the given type. It no type is supplied, events of all types are deleted.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 5 |  |                                                     ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |                             )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 5 |  |             ->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 | 5 |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 (null == $type || '' == $type) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |             $output->write('deleting entries of any type.', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 2 |  |             $typeDesc = 'any type'; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 4 |  |         } elseif (array_search($type, array('accepted', 'rejected', 'delivered', 'failed', 'opened', 'clicked', 'unsubscribed', 'complained', 'stored'))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |             $typeDesc = "type '$type'"; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |             throw new InvalidArgumentException("Unknown type: $type"); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 3 |  |         if (null == $ageLimit || '' == $ageLimit) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |             $output->write("using default age-limit of '60 days ago'.", true); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |             $ageLimit = '60 days ago'; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 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 |  |  |  |