Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoginHandler
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
5.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
1<?php
2
3declare(strict_types=1);
4
5namespace App\Modules\User\Application\Command;
6
7use App\Modules\User\Domain\Model\User;
8use App\Modules\User\Domain\Repository\UserRepositoryInterface;
9
10/**
11 * User Login Command Handler.
12 *
13 * Validates user credentials against password hash and returns User entity upon success.
14 *
15 * @package App\Modules\User\Application\Command
16 */
17final readonly class LoginHandler
18{
19    /**
20     * LoginHandler constructor.
21     *
22     * @param UserRepositoryInterface $userRepository User repository contract instance.
23     */
24    public function __construct(
25        private UserRepositoryInterface $userRepository
26    ) {
27    }
28
29    /**
30     * Handles authentication command and returns User entity if credentials match.
31     *
32     * @param LoginCommand $command Login command containing credentials.
33     * @return User|null Mapped User entity on success, null on authentication failure.
34     */
35    public function handle(LoginCommand $command): ?User
36    {
37        $user = $this->userRepository->findByUsernameOrEmail($command->login);
38        if ($user === null || !$user->isActive()) {
39            return null;
40        }
41
42        if (!password_verify($command->password, $user->getPasswordHash())) {
43            return null;
44        }
45
46        return $user;
47    }
48}