Passed
Push — v2 ( 227daa...82a867 )
by Daniel
05:48
created

PasswordManager::pathToAppUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 3
crap 6
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\Security;
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 Symfony\Component\Security\Core\Exception\AuthenticationException;
22
use Symfony\Component\Validator\Validator\ValidatorInterface;
23
24
class PasswordManager
25
{
26
    private UserMailer $userMailer;
27
    private EntityManagerInterface $entityManager;
28
    private ValidatorInterface $validator;
29
    private TokenGenerator $tokenGenerator;
30
    private int $tokenTtl;
31
32
    public function __construct(
33
        UserMailer $userMailer,
34
        EntityManagerInterface $entityManager,
35
        ValidatorInterface $validator,
36
        TokenGenerator $tokenGenerator,
37
        int $tokenTtl = 8600
38
    ) {
39
        $this->userMailer = $userMailer;
40
        $this->entityManager = $entityManager;
41
        $this->validator = $validator;
42
        $this->tokenGenerator = $tokenGenerator;
43
        $this->tokenTtl = $tokenTtl;
44
    }
45
46
    public function requestResetEmail(AbstractUser $user): void
47
    {
48
        if ($user->isPasswordRequestLimitReached($this->tokenTtl)) {
49
            return;
50
        }
51
        $username = $user->getUsername();
52
        if (!$username) {
53
            throw new InvalidParameterException(sprintf('The entity %s should have a username set to send a password reset email.', AbstractUser::class));
54
        }
55
        $user->setNewPasswordConfirmationToken($confirmationToken = $this->tokenGenerator->generateToken());
56
        $user->setPasswordRequestedAt(new DateTime());
57
        $this->userMailer->sendPasswordResetEmail($user);
58
        $this->entityManager->flush();
59
    }
60
61
    public function passwordReset(AbstractUser $user, string $newPassword): void
62
    {
63
        $user->setPlainPassword($newPassword);
64
        $user->setNewPasswordConfirmationToken(null);
65
        $user->setPasswordRequestedAt(null);
66
        $errors = $this->validator->validate($user, null, ['password_reset']);
67
        if (\count($errors)) {
68
            throw new AuthenticationException('The password entered is not valid');
69
        }
70
        $this->persistPlainPassword($user);
71
    }
72
73
    public function persistPlainPassword(AbstractUser $user): AbstractUser
74
    {
75
        $this->entityManager->persist($user);
76
        $this->entityManager->flush();
77
        $user->eraseCredentials();
78
79
        return $user;
80
    }
81
}
82