Completed
Pull Request — master (#26)
by Dominik
02:16 queued 50s
created

AzineMailgunMailerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 21
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
4
namespace Azine\MailgunWebhooksBundle\Services;
5
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Symfony\Component\Translation\TranslatorInterface;
8
use Azine\MailgunWebhooksBundle\Entity\EmailTrafficStatistics;
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,
0 ignored issues
show
Documentation introduced by
There is no parameter named $managerRegistry,. Did you maybe mean $managerRegistry?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
74
     * @param int $sendNotificationsInterval
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
     * @throws \Exception
103
     * @return int $messagesSent
104
     */
105
    public function  sendSpamComplaintNotification($eventId)
106
    {
107
        $messagesSent = 0;
108
        $failedRecipients = [];
109
110
        /** @var \Swift_Message $message */
111
        $message = $this->mailer->createMessage();
112
        $message->setTo($this->spamAlertsRecipientEmail)
113
            ->setFrom($this->fromEmail)
114
            ->setSubject($this->translator->trans('notification.spam_complaint_received'))
115
            ->setBody(
116
                $this->twig->render('@AzineMailgunWebhooks/Email/notification.html.twig', array('eventId' => $eventId, 'ticketId' => $this->ticketId)),
117
                'text/html'
118
            );
119
120
        $lastSpamReport = $this->managerRegistry->getManager()->getRepository(EmailTrafficStatistics::class)
121
            ->getLastByAction(EmailTrafficStatistics::SPAM_ALERT_SENT);
122
123
        if($lastSpamReport instanceof EmailTrafficStatistics) {
124
125
            $time = new \DateTime();
126
            $timeDiff = $time->diff($lastSpamReport->getCreated());
127
128
            if($timeDiff->i > $this->sendNotificationsInterval) {
129
130
                $messagesSent = $this->mailer->send($message, $failedRecipients);
131
            }
132
133
        }else{
134
135
            $messagesSent = $this->mailer->send($message, $failedRecipients);
136
        }
137
138
        if($messagesSent > 0) {
139
140
            $spamAlert = new EmailTrafficStatistics();
141
            $spamAlert->setAction(EmailTrafficStatistics::SPAM_ALERT_SENT);
142
            $manager = $this->managerRegistry->getManager();
143
            $manager->persist($spamAlert);
144
            $manager->flush($spamAlert);
145
            $manager->clear();
146
        }
147
148
        if($messagesSent == 0 && !empty($failedRecipients)){
149
150
            throw new \Exception('Tried to send notification about spam complaint but no messages were sent');
151
        }
152
        return $messagesSent;
153
    }
154
}