Completed
Push — issue/AZ_62_add_command_to_tes... ( be3d81 )
by Dominik
09:46
created

AzineMailgunMailerService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 177
ccs 0
cts 59
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
B sendSpamComplaintNotification() 0 49 6
B sendBlacklistNotification() 0 25 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
     * @param string $ipAddress
159
     * @return int
160
     * @throws \Exception
161
     */
162
    public function sendBlacklistNotification(HetrixtoolsServiceResponse $response, $ipAddress)
163
    {
164
        $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...
165
        $failedRecipients = [];
166
167
        /** @var \Swift_Message $message */
168
        $message = $this->mailer->createMessage();
169
        $message->setTo($this->spamAlertsRecipientEmail)
170
            ->setFrom($this->fromEmail)
171
            ->setSubject($this->translator->trans('notification.blacklist_received'))
172
            ->setBody(
173
                $this->twig->render('@AzineMailgunWebhooks/Email/blacklistNotification.html.twig', array('response' => $response, "ipAddress" => $ipAddress)),
174
                'text/html'
175
            )
176
            ->addPart($this->twig->render('@AzineMailgunWebhooks/Email/blacklistNotification.txt.twig', array('response' => $response, "ipAddress" => $ipAddress),
177
                'text/plain'));
0 ignored issues
show
Unused Code introduced by
The call to Twig_Environment::render() has too many arguments starting with 'text/plain'.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
178
179
        $messagesSent = $this->mailer->send($message, $failedRecipients);
180
181
        if($messagesSent == 0 && !empty($failedRecipients)){
182
183
            throw new \Exception('Tried to send notification about ip is blacklisted but no messages were sent');
184
        }
185
        return $messagesSent;
186
    }
187
}