Completed
Push — master ( 15189e...f90573 )
by Valentyn
13s
created

SendEmailService::sendEmailConfirmation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Service;
5
6
use App\Users\Entity\User;
7
use App\Users\Service\ConfirmationTokenService;
8
use Symfony\Component\Translation\TranslatorInterface;
9
10
class SendEmailService
11
{
12
    /**
13
     * @var TranslatorInterface
14
     */
15
    private $translator;
16
17
    /**
18
     * @var \Swift_Mailer
19
     */
20
    private $mailer;
21
22
    /**
23
     * @var \Twig_Environment
24
     */
25
    private $twig;
26
27
    /**
28
     * @var ConfirmationTokenService
29
     */
30
    private $confirmationTokenService;
31
32 4
    public function __construct(TranslatorInterface $translator, \Swift_Mailer $mailer, \Twig_Environment $twig, ConfirmationTokenService $confirmationTokenService)
33
    {
34 4
        $this->translator = $translator;
35 4
        $this->mailer = $mailer;
36 4
        $this->twig = $twig;
37 4
        $this->confirmationTokenService = $confirmationTokenService;
38 4
    }
39
40 4
    public function sendEmailConfirmation(User $user)
41
    {
42 4
        $emailConfirmationToken = $this->confirmationTokenService->getEmailConfirmationToken($user)->getToken();
43
44 4
        $body = $this->twig->render(
45 4
            'emails/confirmEmail.html.twig',
46 4
            ['token' => $emailConfirmationToken]
47
        );
48
49 4
        $subject = $this->translator->trans('user_registration_email_subject', [], 'users');
50
51 4
        $this->sendEmail($user->email, $subject, $body);
52 4
    }
53
54 4
    private function sendEmail($recipientEmail, string $subject, string $body)
55
    {
56 4
        $message = (new \Swift_Message($subject))
57 4
            ->setFrom('[email protected]')
58 4
            ->setTo($recipientEmail)
59 4
            ->setBody($body, 'text/html');
60
61
        // todo need to retry if any failed recipients found ($failedRecipients is array of emails)
62 4
        $this->mailer->send($message, $failedRecipients);
63
    }
64
}