|
| 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 | +} |
0 commit comments