Completed
Pull Request — master (#2737)
by Jeroen
17:30 queued 02:36
created

SwiftmailerService::sendPasswordResetEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Service\AuthenticationMailer;
4
5
use Kunstmaan\AdminBundle\Entity\UserInterface;
6
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
7
use Symfony\Component\Routing\RouterInterface;
8
use Symfony\Contracts\Translation\TranslatorInterface;
9
use Twig\Environment;
10
11
class SwiftmailerService implements AuthenticationMailerInterface
12
{
13
    /** @var \Swift_Mailer */
14
    private $mailer;
15
    /** @var Environment */
16
    private $twig;
17
    /** @var TranslatorInterface */
18
    private $translator;
19
    /** @var UrlGeneratorInterface */
20
    private $urlGenerator;
21
    /** @var string */
22
    private $senderName;
23
    /** @var string */
24
    private $senderAddress;
25
26
    public function __construct(\Swift_Mailer $mailer, Environment $twig, TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator, string $senderAddress, string $senderName)
27
    {
28
        $this->mailer = $mailer;
29
        $this->twig = $twig;
30
        $this->translator = $translator;
31
        $this->urlGenerator = $urlGenerator;
32
        $this->senderAddress = $senderAddress;
33
        $this->senderName = $senderName;
34
    }
35
36
    public function sendPasswordResetEmail(UserInterface $user, string $locale)
37
    {
38
        $confirmationUrl = $this->urlGenerator->generate('cms_reset_password_confirm', ['token' => $user->getConfirmationToken()], RouterInterface::ABSOLUTE_URL);
39
        $body = $this->twig->render('@KunstmaanAdmin/Resetting/email.txt.twig', [
40
            'user' => $user,
41
            'confirmationUrl' => $confirmationUrl,
42
        ]);
43
44
        $message = (new \Swift_Message())
45
            ->setSubject($this->translator->trans('Password reset email', [], null, $locale))
46
            ->setFrom($this->senderAddress, $this->senderName)
47
            ->setTo($user->getEmail())
48
            ->setBody($body)
49
        ;
50
51
        $this->mailer->send($message);
52
    }
53
}
54