Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateAvatarHandler
90.91% covered (success)
90.91%
10 / 11
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
50.00% covered (danger)
50.00%
1 / 2
3
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
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
90.00% covered (success)
90.00%
9 / 10
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\User\Application\Handler;
4
5use App\Shared\Domain\Exception\EntityNotFoundException;
6use App\Shared\Domain\Model\EntityId;
7use App\Shared\Domain\Port\Driven\EventBus;
8use App\Shared\Domain\Port\Driven\TransactionProvider;
9use App\Shared\Domain\Service\StoredFileService;
10use App\User\Application\Command\UpdateAvatarCommand;
11use App\User\Domain\Model\User;
12use App\User\Domain\Port\Driven\UserRepository;
13
14readonly class UpdateAvatarHandler
15{
16    public function __construct(
17        private readonly TransactionProvider $transactionProvider,
18        private readonly StoredFileService $storedFileService,
19        private readonly UserRepository $userRepository,
20        private readonly EventBus $eventBus
21    ){
22    }
23
24    public function __invoke(UpdateAvatarCommand $command): User
25    {
26        return $this->transactionProvider->transactional(function () use ($command) {
27            $user = $this->userRepository->findByIdentityId(EntityId::fromString($command->identityId));
28
29            if (!$user) {
30                throw new EntityNotFoundException('User not found');
31            }
32
33            $storedFile = $this->storedFileService->replace($user->getAvatar(), $command->tempFile, 'user.avatar');
34
35            $updatedUser = $user->setAvatar($storedFile);
36
37            $this->userRepository->save($updatedUser);
38
39            $this->eventBus->dispatchAll($updatedUser);
40
41            return $updatedUser;
42        });
43    }
44}