Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateUserHandler
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
4
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%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Identity\Application\Command;
6
7use App\Identity\Application\AuthenticatedUserFinder;
8use App\Identity\Application\Query\CurrentUserView;
9use App\Identity\Domain\Exceptions\EmailAlreadyInUse;
10use App\Identity\Domain\UserRepository;
11use App\Identity\Domain\ValueObject\BirthDate;
12use App\Identity\Domain\ValueObject\Email;
13use App\Identity\Domain\ValueObject\UserId;
14use App\Shared\Domain\ValueObject\Currency;
15use App\Shared\Infrastructure\Storage\SignedImageUrl;
16
17final readonly class UpdateUserHandler
18{
19    public function __construct(
20        private AuthenticatedUserFinder $finder,
21        private UserRepository $users,
22        private SignedImageUrl $imageUrls,
23    ) {
24    }
25
26    public function handle(UpdateUserCommand $command): CurrentUserView
27    {
28        $user = $this->finder->getOrFail(new UserId($command->userId));
29        $email = new Email($command->email);
30
31        if (!$user->email()->equals($email) && $this->users->existsByEmail($email)) {
32            throw EmailAlreadyInUse::of($email);
33        }
34
35        $user->update(
36            $email,
37            new Currency($command->preferredCurrency),
38            $command->name,
39            $command->surnames,
40            BirthDate::fromNullable($command->birthDate),
41        );
42
43        $this->users->save($user);
44
45        return CurrentUserView::fromUser($user, $this->imageUrls->for($user->avatarName()));
46    }
47}