| 1 | <?php |
||
| 2 | |||
| 3 | namespace Azine\MailgunWebhooksBundle\Services; |
||
| 4 | |||
| 5 | use Azine\MailgunWebhooksBundle\Entity\EmailTrafficStatistics; |
||
| 6 | use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\HetrixtoolsServiceResponse; |
||
| 7 | use Doctrine\Common\Persistence\ManagerRegistry; |
||
| 8 | use Symfony\Component\Translation\TranslatorInterface; |
||
| 9 | |||
| 10 | class AzineMailgunMailerService |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var \Swift_Mailer |
||
| 14 | */ |
||
| 15 | private $mailer; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var \Twig_Environment |
||
| 19 | */ |
||
| 20 | private $twig; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var TranslatorInterface |
||
| 24 | */ |
||
| 25 | private $translator; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $fromEmail; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $ticketId; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $ticketSubject; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $ticketMessage; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $spamAlertsRecipientEmail; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ManagerRegistry |
||
| 54 | */ |
||
| 55 | private $managerRegistry; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $sendNotificationsInterval; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * AzineMailgunMailerService constructor. |
||
| 64 | * |
||
| 65 | * @param \Swift_Mailer $mailer |
||
| 66 | * @param \Twig_Environment $twig |
||
| 67 | * @param TranslatorInterface $translator |
||
| 68 | * @param string $fromEmail |
||
| 69 | * @param string $ticketId |
||
| 70 | * @param string $ticketSubject |
||
| 71 | * @param string $ticketMessage |
||
| 72 | * @param string $spamAlertsRecipientEmail |
||
| 73 | * @param ManagerRegistry $managerRegistry |
||
| 74 | * @param int $sendNotificationsInterval in Seconds |
||
| 75 | */ |
||
| 76 | public function __construct( |
||
| 77 | \Swift_Mailer $mailer, |
||
| 78 | \Twig_Environment $twig, |
||
| 79 | TranslatorInterface $translator, |
||
| 80 | $fromEmail, |
||
| 81 | $ticketId, |
||
| 82 | $ticketSubject, |
||
| 83 | $ticketMessage, |
||
| 84 | $spamAlertsRecipientEmail, |
||
| 85 | ManagerRegistry $managerRegistry, |
||
| 86 | $sendNotificationsInterval |
||
| 87 | ) { |
||
| 88 | $this->mailer = $mailer; |
||
| 89 | $this->twig = $twig; |
||
| 90 | $this->translator = $translator; |
||
| 91 | $this->fromEmail = $fromEmail; |
||
| 92 | $this->ticketId = $ticketId; |
||
| 93 | $this->ticketSubject = $ticketSubject; |
||
| 94 | $this->ticketMessage = $ticketMessage; |
||
| 95 | $this->spamAlertsRecipientEmail = $spamAlertsRecipientEmail; |
||
| 96 | $this->managerRegistry = $managerRegistry; |
||
| 97 | $this->sendNotificationsInterval = $sendNotificationsInterval; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $eventId |
||
| 102 | * |
||
| 103 | * @throws \Exception |
||
| 104 | * |
||
| 105 | * @return int $messagesSent |
||
| 106 | */ |
||
| 107 | public function sendSpamComplaintNotification($eventId) |
||
| 108 | { |
||
| 109 | $messagesSent = 0; |
||
| 110 | $failedRecipients = array(); |
||
| 111 | |||
| 112 | /** @var \Swift_Message $message */ |
||
| 113 | $message = $this->mailer->createMessage(); |
||
| 114 | $message->setTo($this->spamAlertsRecipientEmail) |
||
| 115 | ->setFrom($this->fromEmail) |
||
| 116 | ->setSubject($this->translator->trans('notification.spam_complaint_received')) |
||
| 117 | ->setBody( |
||
| 118 | $this->twig->render('@AzineMailgunWebhooks/Email/notification.html.twig', array('eventId' => $eventId, 'ticketId' => $this->ticketId)), |
||
| 119 | 'text/html' |
||
| 120 | ); |
||
| 121 | |||
| 122 | $lastSpamReport = $this->managerRegistry->getManager()->getRepository(EmailTrafficStatistics::class) |
||
| 123 | ->getLastByAction(EmailTrafficStatistics::SPAM_ALERT_SENT); |
||
| 124 | |||
| 125 | if ($lastSpamReport instanceof EmailTrafficStatistics) { |
||
| 126 | $time = new \DateTime(); |
||
| 127 | $timeDiff = $time->diff($lastSpamReport->getCreated()); |
||
| 128 | |||
| 129 | if ($timeDiff->s > $this->sendNotificationsInterval) { |
||
| 130 | $messagesSent = $this->mailer->send($message, $failedRecipients); |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | $messagesSent = $this->mailer->send($message, $failedRecipients); |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($messagesSent > 0) { |
||
| 137 | $spamAlert = new EmailTrafficStatistics(); |
||
| 138 | $spamAlert->setAction(EmailTrafficStatistics::SPAM_ALERT_SENT); |
||
| 139 | $manager = $this->managerRegistry->getManager(); |
||
| 140 | $manager->persist($spamAlert); |
||
| 141 | $manager->flush($spamAlert); |
||
| 142 | $manager->clear(); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (0 == $messagesSent && !empty($failedRecipients)) { |
||
| 146 | throw new \Exception('Tried to send notification about spam complaint but no messages were sent'); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $messagesSent; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param HetrixtoolsServiceResponse $response |
||
| 154 | * @param string $ipAddress |
||
| 155 | * @param \DateTime |
||
| 156 | * |
||
| 157 | * @return int |
||
| 158 | * |
||
| 159 | * @throws \Exception |
||
| 160 | */ |
||
| 161 | public function sendBlacklistNotification(HetrixtoolsServiceResponse $response, $ipAddress, \DateTime $sendDateTime) |
||
| 162 | { |
||
| 163 | $sendDateTime = $sendDateTime->format('Y-m-d H:i:s'); |
||
| 164 | $failedRecipients = array(); |
||
| 165 | |||
| 166 | /** @var \Swift_Message $message */ |
||
| 167 | $message = $this->mailer->createMessage(); |
||
| 168 | $message->setTo($this->spamAlertsRecipientEmail) |
||
| 169 | ->setFrom($this->fromEmail) |
||
| 170 | ->setSubject($this->translator->trans('notification.blacklist_received')) |
||
| 171 | ->setBody( |
||
| 172 | $this->twig->render('@AzineMailgunWebhooks/Email/blacklistNotification.html.twig', array('response' => $response, 'ipAddress' => $ipAddress, 'sendDateTime' => $sendDateTime)), |
||
| 173 | 'text/html' |
||
| 174 | ) |
||
| 175 | ->addPart($this->twig->render('@AzineMailgunWebhooks/Email/blacklistNotification.txt.twig', array('response' => $response, 'ipAddress' => $ipAddress, 'sendDateTime' => $sendDateTime), |
||
| 176 | 'text/plain')); |
||
|
0 ignored issues
–
show
|
|||
| 177 | |||
| 178 | $messagesSent = $this->mailer->send($message, $failedRecipients); |
||
| 179 | |||
| 180 | if (0 == $messagesSent && !empty($failedRecipients)) { |
||
| 181 | throw new \Exception('Tried to send notification about ip is blacklisted but no messages were sent'); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $messagesSent; |
||
| 185 | } |
||
| 186 | } |
||
| 187 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.