ForgotPasswordManager::canResetPassword()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 3
rs 10
1
<?php
2
3
namespace App\Security\ForgotPassword;
4
5
use App\Converter\UserStringConverter;
6
use App\Entity\ActiveDirectoryUser;
7
use App\Entity\PasswordResetToken;
8
use App\Entity\User;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
12
use Symfony\Component\Mailer\MailerInterface;
13
use Symfony\Component\Mime\Address;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
use Symfony\Contracts\Translation\TranslatorInterface;
16
17
class ForgotPasswordManager {
18
    private LoggerInterface $logger;
19
20
    public function __construct(private PasswordManager $passwordManager, private MailerInterface $mailer, private TranslatorInterface $translator,
21 1
                                private UserStringConverter $userConverter, private UrlGeneratorInterface $urlGenerator, LoggerInterface $logger = null) {
22 1
        $this->logger = $logger ?? new NullLogger();
23 1
    }
24 1
25 1
    public function canResetPassword(User $user, ?string $email): bool {
26 1
        return $user instanceof User && !$user instanceof ActiveDirectoryUser && $email !== null;
27 1
    }
28 1
29
    public function resetPassword(?User $user, ?string $email): void {
30 1
        if($user === null) {
31 1
            return;
32
        }
33
34 1
        if($this->canResetPassword($user, $email) !== true) {
35 1
            return;
36
        }
37
38
        $token = $this->passwordManager->createPasswordToken($user);
39 1
40
        $email = (new TemplatedEmail())
41
            ->to(new Address($email, $this->userConverter->convert($user)))
0 ignored issues
show
Bug introduced by
It seems like $email can also be of type null; however, parameter $address of Symfony\Component\Mime\Address::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            ->to(new Address(/** @scrutinizer ignore-type */ $email, $this->userConverter->convert($user)))
Loading history...
42
            ->subject($this->translator->trans('reset_password.title', [], 'mail'))
43 1
            ->textTemplate('mail/reset_password.txt.twig')
44
            ->htmlTemplate('mail/reset_password.html.twig')
45 1
            ->context([
46 1
                'token' => $token->getToken(),
47 1
                'link' => $this->urlGenerator->generate(
48 1
                    'change_password', [
49
                        'token' => $token->getToken()
50
                    ],
51 1
                    UrlGeneratorInterface::ABSOLUTE_URL
52 1
                ),
53 1
                'expiry_date' => $token->getExpiresAt(),
54 1
                'username' => $user->getUsername()
55 1
            ]);
56
57 1
        $this->mailer->send($email);
58 1
    }
59
60 1
    public function updatePassword(PasswordResetToken $token, string $password): void {
61 1
        $this->passwordManager->setPassword($token, $password);
62
63 1
        $this->logger
64 1
            ->info(sprintf('User "%s" successfully updated his/her password.', $token->getUser()->getUsername()));
65
    }
66
}