Completed
Push — master ( 6f429a...4083cc )
by Alexis
06:02
created

TwigSwiftMailer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A sendConfirmationEmailMessage() 0 13 1
B sendMessage() 0 26 3
1
<?php
2
3
namespace AWurth\SilexUser\Mailer;
4
5
use AWurth\SilexUser\Entity\UserInterface;
6
use Swift_Mailer;
7
use Swift_Message;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
use Twig_Environment;
10
11
class TwigSwiftMailer implements MailerInterface
12
{
13
    /**
14
     * @var Swift_Mailer
15
     */
16
    protected $mailer;
17
18
    /**
19
     * @var Twig_Environment
20
     */
21
    protected $twig;
22
23
    /**
24
     * @var UrlGeneratorInterface
25
     */
26
    protected $router;
27
28
    /**
29
     * @var array
30
     */
31
    protected $parameters;
32
33
    public function __construct(Swift_Mailer $mailer, Twig_Environment $twig, UrlGeneratorInterface $router, array $parameters)
34
    {
35
        $this->mailer = $mailer;
36
        $this->twig = $twig;
37
        $this->router = $router;
38
        $this->parameters = $parameters;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function sendConfirmationEmailMessage(UserInterface $user)
45
    {
46
        $url = $this->router->generate('silex_user.registration_confirm', [
47
            'token' => $user->getConfirmationToken()
48
        ], UrlGeneratorInterface::ABSOLUTE_URL);
49
50
        $context = [
51
            'user' => $user,
52
            'confirmationUrl' => $url
53
        ];
54
55
        $this->sendMessage('silex_user/registration/email.twig', $context, $this->parameters['from_email']['confirmation'], $user->getEmail());
56
    }
57
58
    /**
59
     * @param string $templateName
60
     * @param array $context
61
     * @param string|array $fromEmail
62
     * @param string $toEmail
63
     */
64
    protected function sendMessage($templateName, array $context, $fromEmail, $toEmail)
65
    {
66
        $template = $this->twig->load($templateName);
67
        $subject = $template->renderBlock('subject', $context);
68
        $textBody = $template->renderBlock('body_text', $context);
69
70
        $htmlBody = '';
71
        
72
        if ($template->hasBlock('body_html', $context)) {
73
            $htmlBody = $template->renderBlock('body_html', $context);
74
        }
75
76
        $message = new Swift_Message();
77
        $message->setSubject($subject)
78
            ->setFrom($fromEmail)
79
            ->setTo($toEmail);
80
81
        if (!empty($htmlBody)) {
82
            $message->setBody($htmlBody, 'text/html')
83
                ->addPart($textBody, 'text/plain');
84
        } else {
85
            $message->setBody($textBody);
86
        }
87
88
        $this->mailer->send($message);
89
    }
90
}
91