Passed
Push — v2 ( 9b853e...868892 )
by Daniel
04:58
created

PasswordManager::requestResetEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Manager\User;
15
16
use DateTime;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Silverback\ApiComponentBundle\Entity\User\AbstractUser;
19
use Silverback\ApiComponentBundle\Exception\InvalidParameterException;
20
use Silverback\ApiComponentBundle\Mailer\UserMailer;
21
use Silverback\ApiComponentBundle\Security\TokenGenerator;
22
use Symfony\Component\Security\Core\Exception\AuthenticationException;
23
use Symfony\Component\Validator\Validator\ValidatorInterface;
24
25
class PasswordManager
26
{
27
    private UserMailer $userMailer;
28
    private EntityManagerInterface $entityManager;
29
    private ValidatorInterface $validator;
30
    private TokenGenerator $tokenGenerator;
31
    private int $tokenTtl;
32
33
    public function __construct(
34
        UserMailer $userMailer,
35
        EntityManagerInterface $entityManager,
36
        ValidatorInterface $validator,
37
        TokenGenerator $tokenGenerator,
38
        int $tokenTtl = 8600
39
    ) {
40
        $this->userMailer = $userMailer;
41
        $this->entityManager = $entityManager;
42
        $this->validator = $validator;
43
        $this->tokenGenerator = $tokenGenerator;
44
        $this->tokenTtl = $tokenTtl;
45
    }
46
47
    public function requestResetEmail(AbstractUser $user): void
48
    {
49
        if ($user->isPasswordRequestLimitReached($this->tokenTtl)) {
50
            return;
51
        }
52
        $username = $user->getUsername();
53
        if (!$username) {
54
            throw new InvalidParameterException(sprintf('The entity %s should have a username set to send a password reset email.', AbstractUser::class));
55
        }
56
        $user->setNewPasswordConfirmationToken($confirmationToken = $this->tokenGenerator->generateToken());
57
        $user->setPasswordRequestedAt(new DateTime());
58
        $this->userMailer->sendPasswordResetEmail($user);
59
        $this->entityManager->flush();
60
    }
61
62
    public function passwordReset(AbstractUser $user, string $newPassword): void
63
    {
64
        $user->setPlainPassword($newPassword);
65
        $user->setNewPasswordConfirmationToken(null);
66
        $user->setPasswordRequestedAt(null);
67
        $errors = $this->validator->validate($user, null, ['password_reset']);
68
        if (\count($errors)) {
69
            throw new AuthenticationException('The password entered is not valid');
70
        }
71
        $this->persistPlainPassword($user);
72
    }
73
74
    public function persistPlainPassword(AbstractUser $user): AbstractUser
75
    {
76
        $this->entityManager->persist($user);
77
        $this->entityManager->flush();
78
        $user->eraseCredentials();
79
80
        return $user;
81
    }
82
}
83