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

AzineMailgunMailerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 9

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