Completed
Push — master ( 0e9f14...6ec4df )
by Valentyn
04:01
created

sendPasswordRecoveryConfirmation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

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.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
use Symfony\Component\Translation\TranslatorInterface;
11
12
class SendEmailService
13
{
14
    /**
15
     * @var TranslatorInterface
16
     */
17
    private $translator;
18
19
    /**
20
     * @var \Swift_Mailer
21
     */
22
    private $mailer;
23
24
    /**
25
     * @var \Twig_Environment
26
     */
27
    private $twig;
28
29
    /**
30
     * @var ConfirmationTokenService
31
     */
32
    private $confirmationTokenService;
33
34
    /**
35
     * @var LoggerInterface|NullLogger
36
     */
37
    private $logger;
38
39
    /**
40
     * From email
41
     * @var string
42
     */
43
    private $supportEmail;
44
45 5
    public function __construct(TranslatorInterface $translator, \Swift_Mailer $mailer, \Twig_Environment $twig, ConfirmationTokenService $confirmationTokenService, LoggerInterface $logger)
46
    {
47 5
        $this->translator = $translator;
48 5
        $this->mailer = $mailer;
49 5
        $this->twig = $twig;
50 5
        $this->confirmationTokenService = $confirmationTokenService;
51 5
        $this->supportEmail = \getenv('MAILER_SUPPORT_EMAIL') ?: '[email protected]';
52 5
        $this->logger = $logger ?? new NullLogger();
53 5
    }
54
55 2
    public function sendEmailConfirmation(User $user)
56
    {
57 2
        $emailConfirmationToken = $this->confirmationTokenService->getEmailConfirmationToken($user)->getToken();
58
59 2
        $body = $this->twig->render(
60 2
            'emails/confirmEmail.html.twig',
61 2
            ['token' => $emailConfirmationToken]
62
        );
63
64 2
        $subject = $this->translator->trans('user_registration_email_subject', [
65 2
            '{appName}' => \getenv('APP_NAME'),
66 2
        ], 'users');
67
68 2
        $this->sendEmail($user->getEmail(), $subject, $body);
69 2
    }
70
71 1
    public function sendPasswordRecoveryConfirmation(User $user)
72
    {
73 1
        $passwordRecoveryToken = $this->confirmationTokenService->getPasswordRecoveryToken($user)->getToken();
74
75 1
        $body = $this->twig->render(
76 1
            'emails/passwordRecovery.html.twig',
77 1
            ['token' => $passwordRecoveryToken]
78
        );
79
80 1
        $subject = $this->translator->trans('user_password_recovery_email_subject', [], 'users');
81
82 1
        $this->sendEmail($user->getEmail(), $subject, $body);
83 1
    }
84
85 3
    private function sendEmail($recipientEmail, string $subject, string $body)
86
    {
87 3
        $this->logger->info("[MAILER] Trying to send email to {$recipientEmail} with next params:", [
88 3
            'subject' => $subject,
89 3
            'body' => $body,
90
        ]);
91 3
        $message = (new \Swift_Message($subject))
92 3
            ->setFrom($this->supportEmail)
93 3
            ->setTo($recipientEmail)
94 3
            ->setBody($body, 'text/html');
95
96
        // todo need to retry if any failed recipients found ($failedRecipients is array of emails)
97 3
        $this->mailer->send($message, $failedRecipients);
98
99 3
        if (count($failedRecipients)) {
100
            $this->logger->warning('[MAILER] Some of mails not sent, list of all recipients: ', $failedRecipients);
0 ignored issues
show
Bug introduced by
It seems like $failedRecipients can also be of type null; however, Psr\Log\LoggerInterface::warning() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $failedRecipients can also be of type null; however, Psr\Log\AbstractLogger::warning() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
        }
102 3
    }
103
}
104