Passed
Push — v2 ( 7ee84c...a408c1 )
by Daniel
04:52
created

PasswordManager::passwordResetEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.9
cc 2
nc 2
nop 2
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\HttpFoundation\RequestStack;
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 RequestStack $requestStack;
32
    private int $tokenTtl;
33
34
    public function __construct(
35
        UserMailer $userMailer,
36
        EntityManagerInterface $entityManager,
37
        ValidatorInterface $validator,
38
        TokenGenerator $tokenGenerator,
39
        RequestStack $requestStack,
40
        int $tokenTtl = 8600
41
    ) {
42
        $this->userMailer = $userMailer;
43
        $this->entityManager = $entityManager;
44
        $this->validator = $validator;
45
        $this->tokenGenerator = $tokenGenerator;
46
        $this->requestStack = $requestStack;
47
        $this->tokenTtl = $tokenTtl;
48
    }
49
50
    public function requestResetEmail(AbstractUser $user, string $resetUrl): void
51
    {
52
        if ($user->isPasswordRequestLimitReached($this->tokenTtl)) {
53
            return;
54
        }
55
        $username = $user->getUsername();
56
        if (!$username) {
57
            throw new InvalidParameterException(sprintf('The entity %s should have a username set to send a password reset email.', AbstractUser::class));
58
        }
59
        $user->setNewPasswordConfirmationToken($confirmationToken = $this->tokenGenerator->generateToken());
60
        $user->setPasswordRequestedAt(new DateTime());
61
        $this->userMailer->sendPasswordResetEmail($user, $this->pathToAppUrl($resetUrl, $confirmationToken, $username));
62
        $this->entityManager->flush();
63
    }
64
65
    public function passwordReset(AbstractUser $user, string $newPassword): void
66
    {
67
        $user->setPlainPassword($newPassword);
68
        $user->setNewPasswordConfirmationToken(null);
69
        $user->setPasswordRequestedAt(null);
70
        $errors = $this->validator->validate($user, null, ['password_reset']);
71
        if (\count($errors)) {
72
            throw new AuthenticationException('The password entered is not valid');
73
        }
74
        $this->persistPlainPassword($user);
75
    }
76
77
    public function persistPlainPassword(AbstractUser $user): AbstractUser
78
    {
79
        $this->entityManager->persist($user);
80
        $this->entityManager->flush();
81
        $user->eraseCredentials();
82
83
        return $user;
84
    }
85
86
    private function pathToAppUrl(string $path, string $token, string $email): string
87
    {
88
        $request = $this->requestStack->getCurrentRequest();
89
        $urlParts = $request ? parse_url($request->headers->get('referer')) : ['scheme' => 'https', 'host' => 'no-referrer'];
90
        $path = str_replace(['{{ token }}', '{{ email }}'], [$token, $email], $path);
91
92
        return sprintf(
93
            '%s://%s/%s',
94
            $urlParts['scheme'],
95
            $urlParts['host'],
96
            ltrim($path, '/')
97
        );
98
    }
99
}
100