Completed
Push — master ( f446cd...59ed7d )
by Valentyn
13:46 queued 11:33
created

SendEmailService::sendEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Users\Service;
6
7
use App\Users\Entity\User;
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 5
    public function __construct(TranslatorInterface $translator, \Swift_Mailer $mailer, \Twig_Environment $twig, ConfirmationTokenService $confirmationTokenService)
33
    {
34 5
        $this->translator = $translator;
35 5
        $this->mailer = $mailer;
36 5
        $this->twig = $twig;
37 5
        $this->confirmationTokenService = $confirmationTokenService;
38 5
    }
39
40 2
    public function sendEmailConfirmation(User $user)
41
    {
42 2
        $emailConfirmationToken = $this->confirmationTokenService->getEmailConfirmationToken($user)->getToken();
43
44 2
        $body = $this->twig->render(
45 2
            'emails/confirmEmail.html.twig',
46 2
            ['token' => $emailConfirmationToken]
47
        );
48
49 2
        $subject = $this->translator->trans('user_registration_email_subject', [], 'users');
50
51 2
        $this->sendEmail($user->getEmail(), $subject, $body);
52 2
    }
53
54 1
    public function sendPasswordRecoveryConfirmation(User $user)
55
    {
56 1
        $passwordRecoveryToken = $this->confirmationTokenService->getPasswordRecoveryToken($user)->getToken();
57
58 1
        $body = $this->twig->render(
59 1
            'emails/passwordRecovery.html.twig',
60 1
            ['token' => $passwordRecoveryToken]
61
        );
62
63 1
        $subject = $this->translator->trans('user_password_recovery_email_subject', [], 'users');
64
65 1
        $this->sendEmail($user->getEmail(), $subject, $body);
66 1
    }
67
68 3
    private function sendEmail($recipientEmail, string $subject, string $body)
69
    {
70 3
        $message = (new \Swift_Message($subject))
71 3
            ->setFrom('[email protected]')
72 3
            ->setTo($recipientEmail)
73 3
            ->setBody($body, 'text/html');
74
75
        // todo need to retry if any failed recipients found ($failedRecipients is array of emails)
76 3
        $this->mailer->send($message, $failedRecipients);
77 3
    }
78
}
79