Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
91.67% |
11 / 12 |
|
91.67% |
11 / 12 |
|
87.50% |
7 / 8 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| DefaultIdentityService | |
91.67% |
11 / 12 |
|
91.67% |
11 / 12 |
|
87.50% |
7 / 8 |
|
75.00% |
3 / 4 |
8.12 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createIdentity | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| changeEmail | |
80.00% |
4 / 5 |
|
80.00% |
4 / 5 |
|
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| complete | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Auth\Domain\Service; |
| 4 | |
| 5 | use App\Auth\Domain\Model\Identity; |
| 6 | use App\Auth\Domain\Port\Driven\EmailAvailabilityChecker; |
| 7 | use App\Auth\Domain\Port\Driven\IdentityAvailabilityChecker; |
| 8 | use App\Auth\Domain\Port\Driven\PasswordHasher; |
| 9 | use App\Shared\Domain\Exception\EntityAlreadyExistsException; |
| 10 | use App\Shared\Domain\Model\EntityId; |
| 11 | |
| 12 | readonly class DefaultIdentityService implements IdentityService |
| 13 | { |
| 14 | public function __construct( |
| 15 | private PasswordHasher $passwordHasher, |
| 16 | private IdentityAvailabilityChecker $identityAvailabilityChecker, |
| 17 | private EmailAvailabilityChecker $emailAvailabilityChecker |
| 18 | ) { |
| 19 | } |
| 20 | |
| 21 | public function createIdentity(string $email, string $username, string $password): ?Identity |
| 22 | { |
| 23 | if (!$this->identityAvailabilityChecker->isIdentityAvailable($email, $username)) { |
| 24 | throw new EntityAlreadyExistsException('Identity already exists'); |
| 25 | } |
| 26 | return Identity::create(EntityId::generate(), $email, $username, $this->passwordHasher->hash($password)); |
| 27 | } |
| 28 | |
| 29 | public function changeEmail(Identity $identity, string $email): Identity |
| 30 | { |
| 31 | if ($identity->getEmail() === $email) { |
| 32 | return $identity; |
| 33 | } |
| 34 | |
| 35 | if (!$this->emailAvailabilityChecker->isEmailAvailable($email)) { |
| 36 | throw new EntityAlreadyExistsException('Email is taken'); |
| 37 | } |
| 38 | |
| 39 | return $identity->changeEmail($email); |
| 40 | } |
| 41 | |
| 42 | public function complete(Identity $identity): Identity |
| 43 | { |
| 44 | if ($identity->isComplete()) { |
| 45 | return $identity; |
| 46 | } |
| 47 | |
| 48 | return $identity->complete(); |
| 49 | } |
| 50 | } |