Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RegisterUserHandler
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
3
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%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Identity\Application\Command;
6
7use App\Identity\Application\Service\PasswordHasher;
8use App\Identity\Application\Service\PasswordPolicy;
9use App\Identity\Domain\Exceptions\EmailAlreadyInUse;
10use App\Identity\Domain\User;
11use App\Identity\Domain\UserRepository;
12use App\Identity\Domain\ValueObject\BirthDate;
13use App\Identity\Domain\ValueObject\Email;
14use App\Identity\Domain\ValueObject\UserId;
15use App\Shared\Domain\ValueObject\Currency;
16
17final readonly class RegisterUserHandler
18{
19    public function __construct(
20        private UserRepository $users,
21        private PasswordHasher $passwordHasher,
22        private PasswordPolicy $passwordPolicy,
23    ) {
24    }
25
26    public function handle(RegisterUserCommand $command): UserId
27    {
28        $email = new Email($command->email);
29
30        if ($this->users->existsByEmail($email)) {
31            throw EmailAlreadyInUse::of($email);
32        }
33
34        $this->passwordPolicy->assert($command->plainPassword);
35
36        $user = User::register(
37            UserId::generate(),
38            $email,
39            $this->passwordHasher->hash($command->plainPassword),
40            new Currency($command->preferredCurrency),
41            $command->name,
42            $command->surnames,
43            BirthDate::fromNullable($command->birthDate),
44        );
45
46        $this->users->save($user);
47
48        return $user->id();
49    }
50}