Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AuthenticateUserHandler
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
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
8declare(strict_types=1);
9
10namespace AppDemo\User\Application\Command;
11
12use AppDemo\User\Domain\Email;
13use AppDemo\User\Domain\Event\UserAuthenticatedEvent;
14use AppDemo\User\Domain\User;
15use AppDemo\User\Domain\UserRepository;
16use InvalidArgumentException;
17use Override;
18use Phexium\Application\Command\AbstractCommandHandler;
19use Phexium\Application\Command\CommandHandlerInterface;
20use Phexium\Application\Command\CommandInterface;
21use Phexium\Domain\TypeGuard;
22use Phexium\Plugin\Clock\Port\ClockInterface;
23use Phexium\Plugin\EventBus\Port\EventBusInterface;
24use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
25use Phexium\Plugin\Logger\Port\LoggerInterface;
26use Phexium\Plugin\PasswordHasher\Port\PasswordHasherInterface;
27
28final 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}