|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\CommandHandler; |
|
4
|
|
|
|
|
5
|
|
|
use App\Command\ResetPasswordConfirmationCommand; |
|
6
|
|
|
use App\Repository\UserRepository; |
|
7
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
8
|
|
|
use App\CommandHandler\Exception\ResetPasswordConfirmationException; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
11
|
|
|
use App\Entity\User; |
|
12
|
|
|
use App\Repository\Exception\UserNotFoundException; |
|
13
|
|
|
use App\Service\TokenStorageService; |
|
14
|
|
|
|
|
15
|
|
|
class ResetPasswordConfirmationCommandHandler implements CommandHandlerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var UserRepository |
|
19
|
|
|
*/ |
|
20
|
|
|
private $repository; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var MessageBusInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $eventBus; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $logger; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var UserPasswordEncoderInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $encoder; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var TokenStorageService |
|
35
|
|
|
*/ |
|
36
|
|
|
private $tokenStorageService; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param MessageBusInterface $eventBus |
|
40
|
|
|
* @param UserRepository $repository |
|
41
|
|
|
* @param LoggerInterface $logger |
|
42
|
|
|
* @param UserPasswordEncoderInterface $encoder |
|
43
|
|
|
* @param TokenStorageService $tokenStorage |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
MessageBusInterface $eventBus, |
|
47
|
|
|
UserRepository $repository, |
|
48
|
|
|
LoggerInterface $logger, |
|
49
|
|
|
UserPasswordEncoderInterface $encoder, |
|
50
|
|
|
TokenStorageService $tokenStorageService |
|
51
|
|
|
) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->eventBus = $eventBus; |
|
54
|
|
|
$this->repository = $repository; |
|
55
|
|
|
$this->logger = $logger; |
|
56
|
|
|
$this->encoder = $encoder; |
|
57
|
|
|
$this->tokenStorageService = $tokenStorageService; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param ResetPasswordConfirmationCommand $command |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __invoke(ResetPasswordConfirmationCommand $command) |
|
64
|
|
|
{ |
|
65
|
|
|
try { |
|
66
|
|
|
$user = $this->repository->findOneBy(['passwordRequestToken' => $command->getToken()]); |
|
67
|
|
|
|
|
68
|
|
|
if (!$user instanceof User) { |
|
69
|
|
|
throw new UserNotFoundException('User not found'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$password = $this->encoder->encodePassword($user, $command->getPassword()); |
|
73
|
|
|
$user->setPassword($password); |
|
74
|
|
|
$user->setPasswordRequestToken(null); |
|
75
|
|
|
$this->repository->save($user); |
|
76
|
|
|
$this->tokenStorageService->storeToken($user); |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
$this->logger->error($e->getMessage()); |
|
79
|
|
|
throw new ResetPasswordConfirmationException('Pasword reset confirmation error: '.$e->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|