Repository navigation
Expand file tree
/
Copy pathResetUserPasswordHandler.php
More file actions
52 lines (42 loc) · 1.73 KB
/
Copy pathResetUserPasswordHandler.php
File metadata and controls
52 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace MsgPhp\User\Command\Handler;
use MsgPhp\Domain\DomainMessageBus;
use MsgPhp\Domain\Event\EventSourcingCommandHandlerTrait;
use MsgPhp\Domain\Factory\DomainObjectFactory;
use MsgPhp\User\Command\ChangeUserCredential;
use MsgPhp\User\Command\ResetUserPassword;
use MsgPhp\User\Credential\PasswordProtectedCredential;
use MsgPhp\User\Event\Domain\FinishPasswordRequest;
use MsgPhp\User\Event\UserPasswordRequestFinished;
use MsgPhp\User\Repository\UserRepository;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class ResetUserPasswordHandler
{
use EventSourcingCommandHandlerTrait;
private $factory;
private $bus;
private $repository;
public function __construct(DomainObjectFactory $factory, DomainMessageBus $bus, UserRepository $repository)
{
$this->factory = $factory;
$this->bus = $bus;
$this->repository = $repository;
}
public function __invoke(ResetUserPassword $command): void
{
$user = $this->repository->find($command->userId);
$oldCredential = $user->getCredential();
if ($oldCredential instanceof PasswordProtectedCredential) {
$userId = $command->userId;
$fields = [$oldCredential::getPasswordField() => $command->password] + $command->context;
$this->bus->dispatch($this->factory->create(ChangeUserCredential::class, compact('userId', 'fields')));
}
if ($this->handleEvent($user, $this->factory->create(FinishPasswordRequest::class, compact('oldCredential')))) {
$this->repository->save($user);
$this->bus->dispatch($this->factory->create(UserPasswordRequestFinished::class, compact('user', 'oldCredential')));
}
}
}