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

AzineMailgunMailerService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 74
rs 10
c 3
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A sendSpamComplaintNotification() 0 19 2
A validateEmail() 0 16 2
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
}