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

SymfonyMailerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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