Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
75.00% covered (warning)
75.00%
3 / 4
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangeEmailHandler
90.91% covered (success)
90.91%
10 / 11
75.00% covered (warning)
75.00%
3 / 4
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
3.33
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
66.67% covered (warning)
66.67%
2 / 3
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3namespace App\Auth\Application\Handler;
4
5use App\Auth\Application\Command\ChangeEmailCommand;
6use App\Auth\Domain\Model\Identity;
7use App\Auth\Domain\Port\Driven\IdentityRepository;
8use App\Auth\Domain\Service\IdentityService;
9use App\Shared\Domain\Exception\EntityNotFoundException;
10use App\Shared\Domain\Model\EntityId;
11use App\Shared\Domain\Port\Driven\EventBus;
12use App\Shared\Domain\Port\Driven\TransactionProvider;
13
14readonly class ChangeEmailHandler
15{
16    public function __construct(
17        private IdentityService $identityService,
18        private IdentityRepository  $identityRepository,
19        private TransactionProvider $transactionProvider,
20        private EventBus $eventBus
21
22    ) {
23    }
24
25    public function __invoke(ChangeEmailCommand $command): ?Identity
26    {
27        $identityId = EntityId::fromString($command->identityId);
28        $identity = $this->identityRepository->findById($identityId);
29        if (null === $identity) {
30            throw new EntityNotFoundException("Identity with identifier could not be found");
31        }
32
33        return $this->transactionProvider->transactional(function () use ($identity, $command) {
34            $identity = $this->identityService->changeEmail($identity, $command->email);
35            $this->identityRepository->save($identity);
36            $this->eventBus->dispatchAll($identity);
37            return $identity;
38        });
39    }
40}