Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AuthenticateUserHandler | |
100.00% |
28 / 28 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\User\Application\Command; |
| 11 | |
| 12 | use AppDemo\User\Domain\Email; |
| 13 | use AppDemo\User\Domain\Event\UserAuthenticatedEvent; |
| 14 | use AppDemo\User\Domain\User; |
| 15 | use AppDemo\User\Domain\UserRepository; |
| 16 | use InvalidArgumentException; |
| 17 | use Override; |
| 18 | use Phexium\Application\Command\AbstractCommandHandler; |
| 19 | use Phexium\Application\Command\CommandHandlerInterface; |
| 20 | use Phexium\Application\Command\CommandInterface; |
| 21 | use Phexium\Domain\TypeGuard; |
| 22 | use Phexium\Plugin\Clock\Port\ClockInterface; |
| 23 | use Phexium\Plugin\EventBus\Port\EventBusInterface; |
| 24 | use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface; |
| 25 | use Phexium\Plugin\Logger\Port\LoggerInterface; |
| 26 | use Phexium\Plugin\PasswordHasher\Port\PasswordHasherInterface; |
| 27 | |
| 28 | final class AuthenticateUserHandler extends AbstractCommandHandler implements CommandHandlerInterface |
| 29 | { |
| 30 | public function __construct( |
| 31 | private readonly UserRepository $userRepository, |
| 32 | private readonly PasswordHasherInterface $passwordHasher, |
| 33 | private readonly EventBusInterface $eventBus, |
| 34 | private readonly ClockInterface $clock, |
| 35 | private readonly IdGeneratorInterface $idGenerator, |
| 36 | private readonly LoggerInterface $logger, |
| 37 | ) {} |
| 38 | |
| 39 | #[Override] |
| 40 | public function handle(CommandInterface $command): void |
| 41 | { |
| 42 | TypeGuard::that($command)->isInstanceOf(AuthenticateUserCommand::class); |
| 43 | |
| 44 | $this->logger->info('AuthenticateUserHandler: Starting authentication', [ |
| 45 | 'email' => $command->email, |
| 46 | ]); |
| 47 | |
| 48 | try { |
| 49 | $email = Email::fromString($command->email); |
| 50 | |
| 51 | $user = $this->userRepository->findByEmail($email); |
| 52 | |
| 53 | if (!$user instanceof User) { |
| 54 | throw new InvalidArgumentException('Invalid credentials'); |
| 55 | } |
| 56 | |
| 57 | if (!$this->passwordHasher->verify($command->password, $user->getHashedPassword()->getValue())) { |
| 58 | throw new InvalidArgumentException('Invalid credentials'); |
| 59 | } |
| 60 | |
| 61 | $this->logger->info('AuthenticateUserHandler: User authenticated successfully', [ |
| 62 | 'userId' => $user->getId()->getValue(), |
| 63 | 'email' => $user->getEmail()->getValue(), |
| 64 | ]); |
| 65 | |
| 66 | // Dispatch event for successful authentication |
| 67 | $this->eventBus->dispatch( |
| 68 | new UserAuthenticatedEvent( |
| 69 | $this->idGenerator->generate(), |
| 70 | $this->clock->now(), |
| 71 | $user |
| 72 | ) |
| 73 | ); |
| 74 | } catch (InvalidArgumentException $invalidArgumentException) { |
| 75 | $this->logger->warning('AuthenticateUserHandler: Authentication failed', [ |
| 76 | 'email' => $command->email, |
| 77 | 'error' => $invalidArgumentException->getMessage(), |
| 78 | ]); |
| 79 | throw $invalidArgumentException; |
| 80 | } |
| 81 | } |
| 82 | } |