Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AuthenticateUserHandler | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
26 / 26 |
|
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 Phexium\Application\Command\CommandHandlerInterface; |
| 18 | use Phexium\Plugin\Clock\Port\ClockInterface; |
| 19 | use Phexium\Plugin\EventBus\Port\EventBusInterface; |
| 20 | use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface; |
| 21 | use Phexium\Plugin\Logger\Port\LoggerInterface; |
| 22 | use Phexium\Plugin\PasswordHasher\Port\PasswordHasherInterface; |
| 23 | |
| 24 | final readonly class AuthenticateUserHandler implements CommandHandlerInterface |
| 25 | { |
| 26 | public function __construct( |
| 27 | private UserRepository $userRepository, |
| 28 | private PasswordHasherInterface $passwordHasher, |
| 29 | private EventBusInterface $eventBus, |
| 30 | private ClockInterface $clock, |
| 31 | private IdGeneratorInterface $idGenerator, |
| 32 | private LoggerInterface $logger, |
| 33 | ) {} |
| 34 | |
| 35 | public function __invoke(AuthenticateUserCommand $command): void |
| 36 | { |
| 37 | $this->logger->info('AuthenticateUserHandler: Starting authentication', [ |
| 38 | 'email' => $command->email, |
| 39 | ]); |
| 40 | |
| 41 | try { |
| 42 | $email = Email::fromString($command->email); |
| 43 | |
| 44 | $user = $this->userRepository->findByEmail($email); |
| 45 | |
| 46 | if (!$user instanceof User) { |
| 47 | throw new InvalidArgumentException('Invalid credentials'); |
| 48 | } |
| 49 | |
| 50 | if (!$this->passwordHasher->verify($command->password, $user->getHashedPassword()->getValue())) { |
| 51 | throw new InvalidArgumentException('Invalid credentials'); |
| 52 | } |
| 53 | |
| 54 | $this->logger->info('AuthenticateUserHandler: User authenticated successfully', [ |
| 55 | 'userId' => $user->getId()->getValue(), |
| 56 | 'email' => $user->getEmail()->getValue(), |
| 57 | ]); |
| 58 | |
| 59 | // Dispatch event for successful authentication |
| 60 | $this->eventBus->dispatch( |
| 61 | new UserAuthenticatedEvent( |
| 62 | $this->idGenerator->generate(), |
| 63 | $this->clock->now(), |
| 64 | $user |
| 65 | ) |
| 66 | ); |
| 67 | } catch (InvalidArgumentException $invalidArgumentException) { |
| 68 | $this->logger->warning('AuthenticateUserHandler: Authentication failed', [ |
| 69 | 'email' => $command->email, |
| 70 | 'error' => $invalidArgumentException->getMessage(), |
| 71 | ]); |
| 72 | throw $invalidArgumentException; |
| 73 | } |
| 74 | } |
| 75 | } |