Skip to content

Commit 2052039

Browse files
authored
Add CancelUserPasswordRequest command (#315)
1 parent 1ce86c3 commit 2052039

5 files changed

Lines changed: 128 additions & 5 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\User\Command;
6+
7+
use MsgPhp\User\UserId;
8+
9+
/**
10+
* @author Roland Franssen <[email protected]>
11+
*/
12+
class CancelUserPasswordRequest
13+
{
14+
/**
15+
* @var UserId
16+
*/
17+
public $userId;
18+
19+
public function __construct(UserId $userId)
20+
{
21+
$this->userId = $userId;
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\User\Command\Handler;
6+
7+
use MsgPhp\Domain\Event\DomainEvent;
8+
use MsgPhp\Domain\Event\EventSourcingCommandHandlerTrait;
9+
use MsgPhp\Domain\Factory\DomainObjectFactory;
10+
use MsgPhp\Domain\Message\DomainMessageBus;
11+
use MsgPhp\Domain\Message\MessageDispatchingTrait;
12+
use MsgPhp\User\Command\CancelUserPasswordRequest;
13+
use MsgPhp\User\Event\Domain\CancelPasswordRequest;
14+
use MsgPhp\User\Event\UserPasswordRequestCanceled;
15+
use MsgPhp\User\Repository\UserRepository;
16+
use MsgPhp\User\User;
17+
18+
/**
19+
* @author Roland Franssen <[email protected]>
20+
*/
21+
final class CancelUserPasswordRequestHandler
22+
{
23+
use EventSourcingCommandHandlerTrait;
24+
use MessageDispatchingTrait;
25+
26+
/**
27+
* @var UserRepository
28+
*/
29+
private $repository;
30+
31+
public function __construct(DomainObjectFactory $factory, DomainMessageBus $bus, UserRepository $repository)
32+
{
33+
$this->factory = $factory;
34+
$this->bus = $bus;
35+
$this->repository = $repository;
36+
}
37+
38+
public function __invoke(CancelUserPasswordRequest $command): void
39+
{
40+
$this->handle($command, function (User $user): void {
41+
$this->repository->save($user);
42+
$this->dispatch(UserPasswordRequestCanceled::class, compact('user'));
43+
});
44+
}
45+
46+
protected function getDomainEvent(CancelUserPasswordRequest $command): DomainEvent
47+
{
48+
return $this->factory->create(CancelPasswordRequest::class);
49+
}
50+
51+
protected function getDomainEventTarget(CancelUserPasswordRequest $command): User
52+
{
53+
return $this->repository->find($command->userId);
54+
}
55+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\User\Event\Domain;
6+
7+
use MsgPhp\Domain\Event\DomainEvent;
8+
9+
/**
10+
* @author Roland Franssen <[email protected]>
11+
*/
12+
class CancelPasswordRequest implements DomainEvent
13+
{
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MsgPhp\User\Event;
6+
7+
use MsgPhp\User\User;
8+
9+
/**
10+
* @author Roland Franssen <[email protected]>
11+
*/
12+
class UserPasswordRequestCanceled
13+
{
14+
/**
15+
* @var User
16+
*/
17+
public $user;
18+
19+
public function __construct(User $user)
20+
{
21+
$this->user = $user;
22+
}
23+
}

Model/ResettablePassword.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MsgPhp\User\Model;
66

7+
use MsgPhp\User\Event\Domain\CancelPasswordRequest;
78
use MsgPhp\User\Event\Domain\RequestPassword;
89

910
/**
@@ -37,20 +38,27 @@ public function requestPassword(string $token = null): void
3738
$this->passwordRequestedAt = new \DateTimeImmutable();
3839
}
3940

40-
public function clearPasswordRequest(): void
41+
public function cancelPasswordRequest(): void
4142
{
4243
$this->passwordResetToken = null;
4344
$this->passwordRequestedAt = null;
4445
}
4546

4647
private function onRequestPasswordEvent(RequestPassword $event): bool
4748
{
48-
if (null === $event->token || $event->token !== $this->passwordResetToken) {
49-
$this->requestPassword($event->token);
49+
$this->requestPassword($event->token);
5050

51-
return true;
51+
return true;
52+
}
53+
54+
private function onCancelPasswordRequestEvent(CancelPasswordRequest $event): bool
55+
{
56+
if (null === $this->passwordRequestedAt) {
57+
return false;
5258
}
5359

54-
return false;
60+
$this->cancelPasswordRequest();
61+
62+
return true;
5563
}
5664
}

0 commit comments

Comments
 (0)