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