Completed
Push — master ( 96ea8b...1276b9 )
by Alexis
08:08
created

TwigSwiftMailer::sendMessage()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 4
1
<?php
2
3
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AWurth\Silex\User\Mailer;
13
14
use AWurth\Silex\User\Model\UserInterface;
15
use Swift_Mailer;
16
use Swift_Message;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
use Twig_Environment;
19
20
class TwigSwiftMailer implements MailerInterface
21
{
22
    /**
23
     * @var Swift_Mailer
24
     */
25
    protected $mailer;
26
27
    /**
28
     * @var Twig_Environment
29
     */
30
    protected $twig;
31
32
    /**
33
     * @var UrlGeneratorInterface
34
     */
35
    protected $router;
36
37
    /**
38
     * @var array
39
     */
40
    protected $parameters;
41
42
    public function __construct(Swift_Mailer $mailer, Twig_Environment $twig, UrlGeneratorInterface $router, array $parameters)
43
    {
44
        $this->mailer = $mailer;
45
        $this->twig = $twig;
46
        $this->router = $router;
47
        $this->parameters = $parameters;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function sendConfirmationEmailMessage(UserInterface $user)
54
    {
55
        $url = $this->router->generate('silex_user.registration_confirm', [
56
            'token' => $user->getConfirmationToken()
57
        ], UrlGeneratorInterface::ABSOLUTE_URL);
58
59
        $context = [
60
            'user' => $user,
61
            'confirmationUrl' => $url
62
        ];
63
64
        $this->sendMessage('silex_user/registration/email.twig', $context, $this->parameters['from_email']['confirmation'], $user->getEmail());
65
    }
66
67
    /**
68
     * @param string $templateName
69
     * @param array $context
70
     * @param string|array $fromEmail
71
     * @param string $toEmail
72
     */
73
    protected function sendMessage($templateName, array $context, $fromEmail, $toEmail)
74
    {
75
        $template = $this->twig->load($templateName);
76
        $subject = $template->renderBlock('subject', $context);
77
        $textBody = $template->renderBlock('body_text', $context);
78
79
        $htmlBody = '';
80
        
81
        if ($template->hasBlock('body_html', $context)) {
82
            $htmlBody = $template->renderBlock('body_html', $context);
83
        }
84
85
        $message = new Swift_Message();
86
        $message->setSubject($subject)
87
            ->setFrom($fromEmail)
88
            ->setTo($toEmail);
89
90
        if (!empty($htmlBody)) {
91
            $message->setBody($htmlBody, 'text/html')
92
                ->addPart($textBody, 'text/plain');
93
        } else {
94
            $message->setBody($textBody);
95
        }
96
97
        $this->mailer->send($message);
98
    }
99
}
100