Completed
Push — main ( 083b32...2bbe13 )
by Michael
15s queued 12s
created

FOSUserMailer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Licensed under MIT. See file /LICENSE.
7
 */
8
9
namespace App\Service;
10
11
use FOS\UserBundle\Mailer\MailerInterface;
12
use FOS\UserBundle\Model\UserInterface;
13
use Symfony\Component\Mime\Address;
14
use Symfony\Component\Mime\Email;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
use Twig\Environment;
17
18
class FOSUserMailer implements MailerInterface
19
{
20
    private \Symfony\Component\Mailer\MailerInterface $mailer;
21
22
    private Environment $twig;
23
24
    private UrlGeneratorInterface $router;
25
26
    private string $senderMail;
27
28
    private string $senderName;
29
30
    public function __construct(
31
        \Symfony\Component\Mailer\MailerInterface $mailer,
32
        Environment $twig,
33
        UrlGeneratorInterface $router,
34
        string $senderMail,
35
        string $senderName
36
    ) {
37
        $this->mailer = $mailer;
38
        $this->twig = $twig;
39
        $this->router = $router;
40
        $this->senderMail = $senderMail;
41
        $this->senderName = $senderName;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function sendConfirmationEmailMessage(UserInterface $user)
48
    {
49
        $template = '@FOSUser/Registration/email.txt.twig';
50
        $url = $this->router->generate('fos_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
51
52
        $context = [
53
            'user' => $user,
54
            'confirmationUrl' => $url,
55
        ];
56
57
        $this->sendMessage($template, $context, (string) $user->getEmail());
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function sendResettingEmailMessage(UserInterface $user)
64
    {
65
        $template = '@FOSUser/Resetting/email.txt.twig';
66
        $url = $this->router->generate('fos_user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
67
68
        $context = [
69
            'user' => $user,
70
            'confirmationUrl' => $url,
71
        ];
72
73
        $this->sendMessage($template, $context, (string) $user->getEmail());
74
    }
75
76
    protected function sendMessage(string $templateName, array $context, string $toEmail)
77
    {
78
        $template = $this->twig->load($templateName);
79
        $subject = $template->renderBlock('subject', $context);
80
        $textBody = $template->renderBlock('body_text', $context);
81
82
        $message = new Email();
83
        $message
84
            ->subject($subject)
85
            ->from(new Address($this->senderMail, $this->senderName))
86
            ->to($toEmail)
87
            ->text($textBody);
88
89
        if ($template->hasBlock('body_html', $context)) {
90
            $message->html($template->renderBlock('body_html', $context));
91
        }
92
93
        $this->mailer->send($message);
94
    }
95
}
96