Completed
Pull Request — master (#30)
by
unknown
05:39
created

AzineMailgunMailerService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 174
ccs 0
cts 57
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
B sendSpamComplaintNotification() 0 49 6
A sendBlacklistNotification() 0 23 3
1
<?php
2
3
4
namespace Azine\MailgunWebhooksBundle\Services;
5
6
use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\HetrixtoolsServiceResponse;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
use Symfony\Component\Translation\TranslatorInterface;
9
use Azine\MailgunWebhooksBundle\Entity\EmailTrafficStatistics;
10
11
class AzineMailgunMailerService
12
{
13
    /**
14
     * @var \Swift_Mailer
15
     */
16
    private $mailer;
17
18
    /**
19
     * @var \Twig_Environment
20
     */
21
    private $twig;
22
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28
    /**
29
     * @var string
30
     */
31
    private $fromEmail;
32
33
    /**
34
     * @var string
35
     */
36
    private $ticketId;
37
38
    /**
39
     * @var string
40
     */
41
    private $ticketSubject;
42
43
    /**
44
     * @var string
45
     */
46
    private $ticketMessage;
47
48
    /**
49
     * @var string
50
     */
51
    private $spamAlertsRecipientEmail;
52
53
    /**
54
     * @var ManagerRegistry
55
     */
56
    private $managerRegistry;
57
58
    /**
59
     * @var int
60
     */
61
    private $sendNotificationsInterval;
62
63
    /**
64
     * AzineMailgunMailerService constructor.
65
     *
66
     * @param \Swift_Mailer $mailer
67
     * @param \Twig_Environment $twig
68
     * @param TranslatorInterface $translator
69
     * @param string $fromEmail
70
     * @param string $ticketId
71
     * @param string $ticketSubject
72
     * @param string $ticketMessage
73
     * @param string $spamAlertsRecipientEmail
74
     * @param ManagerRegistry $managerRegistry
75
     * @param int $sendNotificationsInterval
76
     */
77
    public function __construct(
78
        \Swift_Mailer $mailer,
79
        \Twig_Environment $twig,
80
        TranslatorInterface $translator,
81
        $fromEmail,
82
        $ticketId,
83
        $ticketSubject,
84
        $ticketMessage,
85
        $spamAlertsRecipientEmail,
86
        ManagerRegistry $managerRegistry,
87
        $sendNotificationsInterval
88
    ) {
89
        $this->mailer = $mailer;
90
        $this->twig = $twig;
91
        $this->translator = $translator;
92
        $this->fromEmail = $fromEmail;
93
        $this->ticketId = $ticketId;
94
        $this->ticketSubject = $ticketSubject;
95
        $this->ticketMessage = $ticketMessage;
96
        $this->spamAlertsRecipientEmail = $spamAlertsRecipientEmail;
97
        $this->managerRegistry = $managerRegistry;
98
        $this->sendNotificationsInterval = $sendNotificationsInterval;
99
    }
100
101
    /**
102
     * @param string $eventId
103
     * @throws \Exception
104
     * @return int $messagesSent
105
     */
106
    public function  sendSpamComplaintNotification($eventId)
107
    {
108
        $messagesSent = 0;
109
        $failedRecipients = [];
110
111
        /** @var \Swift_Message $message */
112
        $message = $this->mailer->createMessage();
113
        $message->setTo($this->spamAlertsRecipientEmail)
114
            ->setFrom($this->fromEmail)
115
            ->setSubject($this->translator->trans('notification.spam_complaint_received'))
116
            ->setBody(
117
                $this->twig->render('@AzineMailgunWebhooks/Email/notification.html.twig', array('eventId' => $eventId, 'ticketId' => $this->ticketId)),
118
                'text/html'
119
            );
120
121
        $lastSpamReport = $this->managerRegistry->getManager()->getRepository(EmailTrafficStatistics::class)
122
            ->getLastByAction(EmailTrafficStatistics::SPAM_ALERT_SENT);
123
124
        if($lastSpamReport instanceof EmailTrafficStatistics) {
125
126
            $time = new \DateTime();
127
            $timeDiff = $time->diff($lastSpamReport->getCreated());
128
129
            if($timeDiff->i > $this->sendNotificationsInterval) {
130
131
                $messagesSent = $this->mailer->send($message, $failedRecipients);
132
            }
133
134
        }else{
135
136
            $messagesSent = $this->mailer->send($message, $failedRecipients);
137
        }
138
139
        if($messagesSent > 0) {
140
141
            $spamAlert = new EmailTrafficStatistics();
142
            $spamAlert->setAction(EmailTrafficStatistics::SPAM_ALERT_SENT);
143
            $manager = $this->managerRegistry->getManager();
144
            $manager->persist($spamAlert);
145
            $manager->flush($spamAlert);
146
            $manager->clear();
147
        }
148
149
        if($messagesSent == 0 && !empty($failedRecipients)){
150
151
            throw new \Exception('Tried to send notification about spam complaint but no messages were sent');
152
        }
153
        return $messagesSent;
154
    }
155
156
    /**
157
     * @param HetrixtoolsServiceResponse $response
158
     * @throws \Exception
159
     * @return int $messagesSent
160
     */
161
    public function sendBlacklistNotification(HetrixtoolsServiceResponse $response)
162
    {
163
        $messagesSent = 0;
0 ignored issues
show
Unused Code introduced by
$messagesSent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
164
        $failedRecipients = [];
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)),
173
                'text/html'
174
            );
175
176
        $messagesSent = $this->mailer->send($message, $failedRecipients);
177
178
        if($messagesSent == 0 && !empty($failedRecipients)){
179
180
            throw new \Exception('Tried to send notification about ip is blacklisted but no messages were sent');
181
        }
182
        return $messagesSent;
183
    }
184
}