Completed
Pull Request — master (#7)
by
unknown
02:28
created

AzineMailgunMailerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 23
rs 9.0856
c 3
b 0
f 1
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 Monolog\Logger;
7
use Symfony\Component\Translation\Translator;
8
use Symfony\Component\Translation\TranslatorInterface;
9
use Symfony\Component\Validator\Validator\ValidatorInterface;
10
11
class AzineMailgunMailerService
12
{
13
    private $mailer;
14
    private $validator;
15
    private $logger;
16
    private $twig;
17
    private $translator;
18
    private $mailerUser;
19
    private $ticketId;
20
    private $ticketSubject;
21
    private $ticketMessage;
22
    private $adminUserEmail;
23
    
24
    public function __construct(
25
        \Swift_Mailer $mailer,
26
        ValidatorInterface $validator,
27
        Logger $logger,
28
        \Twig_Environment $twig,
29
        TranslatorInterface $translator,
30
        $mailerUser,
31
        $ticketId,
32
        $ticketSubject,
33
        $ticketMessage,
34
        $adminUserEmail
35
    ) {
36
        $this->mailer = $mailer;
37
        $this->validator = $validator;
38
        $this->logger = $logger;
39
        $this->twig = $twig;
40
        $this->translator = $translator;
41
        $this->mailerUser = $mailerUser;
42
        $this->ticketId = $ticketId;
43
        $this->ticketSubject = $ticketSubject;
44
        $this->ticketMessage = $ticketMessage;
45
        $this->adminUserEmail = $adminUserEmail;
46
    }
47
48
    public function  sendSpamComplaintNotification($eventId)
49
    {
50
        $emailValidityErrors = $this->validateEmail($this->adminUserEmail);
51
        if (is_null($emailValidityErrors)) {            
52
            /** @var \Swift_Message $message */
53
            $message = $this->mailer->createMessage();
54
            $message->setTo($this->adminUserEmail)
55
                    ->setFrom($this->mailerUser)
56
                    ->setSubject($this->translator->trans('notification.spam_complaint_received'))
57
                    ->setBody(
58
                        $this->twig->render('@AzineMailgunWebhooks/Email/notification.html.twig', array('eventId' => $eventId, 'ticketId' => $this->ticketId)),
59
                        'text/html'
60
                    );
61
            
62
            $this->mailer->send($message);
63
        } else {
64
            $this->logger->warning('Tried to send notification about spam complaint but adminUserEmail is invalid: ' . json_encode($emailValidityErrors));
65
        }
66
    }
67
68
    private function validateEmail($email)
69
    {
70
        $errors = $this->validator->validate(
71
            $email, 
72
            array(
73
                new \Symfony\Component\Validator\Constraints\Email(),
74
                new \Symfony\Component\Validator\Constraints\NotBlank()
75
            )
76
        );
77
78
        if (count($errors) > 0) {
79
            return $errors;
80
        } else {
81
            return null;
82
        }
83
    }
84
}