Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
67.86% |
19 / 28 |
|
54.55% |
6 / 11 |
CRAP | |
0.00% |
0 / 1 |
User | |
67.86% |
19 / 28 |
|
54.55% |
6 / 11 |
28.76 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isBlocked | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBlockedReason | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
block | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
unblock | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
setPhoneNumber | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getPhoneNumber | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
activate | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
5.26 | |||
verify | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
getPermissionIdentifiers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | namespace Apie\IntegrationTests\Apie\TypeDemo\Resources; |
3 | |
4 | use Apie\Core\Attributes\Internal; |
5 | use Apie\Core\Entities\EntityInterface; |
6 | use Apie\Core\Identifiers\UuidV4; |
7 | use Apie\Core\Lists\PermissionList; |
8 | use Apie\Core\Permissions\PermissionInterface; |
9 | use Apie\Core\ValueObjects\DatabaseText; |
10 | use Apie\CountryAndPhoneNumber\DutchPhoneNumber; |
11 | use Apie\Fixtures\ValueObjects\EncryptedPassword; |
12 | use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\UserIdentifier; |
13 | use Apie\TextValueObjects\StrongPassword; |
14 | use LogicException; |
15 | |
16 | final class User implements EntityInterface, PermissionInterface |
17 | { |
18 | private ?EncryptedPassword $password = null; |
19 | |
20 | private UuidV4 $activationToken; |
21 | |
22 | private ?DutchPhoneNumber $phoneNumber = null; |
23 | |
24 | private ?DatabaseText $blockedReason = null; |
25 | |
26 | public function __construct( |
27 | private UserIdentifier $id |
28 | ) { |
29 | $this->activationToken = UuidV4::createRandom(); |
30 | } |
31 | |
32 | public function getId(): UserIdentifier |
33 | { |
34 | return $this->id; |
35 | } |
36 | |
37 | public function isBlocked(): bool |
38 | { |
39 | return $this->blockedReason !== null; |
40 | } |
41 | |
42 | public function getBlockedReason(): ?DatabaseText |
43 | { |
44 | return $this->blockedReason; |
45 | } |
46 | |
47 | public function block(DatabaseText $blockedReason): void |
48 | { |
49 | if ($this->blockedReason !== null) { |
50 | throw new LogicException("User is already blocked!"); |
51 | } |
52 | $this->blockedReason = $blockedReason; |
53 | } |
54 | |
55 | public function unblock(): void |
56 | { |
57 | if ($this->blockedReason === null) { |
58 | throw new LogicException("User is not blocked!"); |
59 | } |
60 | $this->blockedReason = null; |
61 | } |
62 | |
63 | public function setPhoneNumber(?DutchPhoneNumber $phoneNumber): self |
64 | { |
65 | $this->phoneNumber = $phoneNumber; |
66 | |
67 | return $this; |
68 | } |
69 | |
70 | public function getPhoneNumber(): ?DutchPhoneNumber |
71 | { |
72 | return $this->phoneNumber; |
73 | } |
74 | |
75 | public function activate(string $activationToken, StrongPassword $newPassword, StrongPassword $repeat): void |
76 | { |
77 | if ($newPassword->toNative() !== $repeat->toNative()) { |
78 | throw new LogicException('Type the same password twice!'); |
79 | } |
80 | if ($activationToken !== $this->activationToken->toNative()) { |
81 | throw new LogicException('Activation token is incorrect'); |
82 | } |
83 | if ($this->isBlocked()) { |
84 | throw new LogicException('User is blocked and can not be activated'); |
85 | } |
86 | $this->password = EncryptedPassword::fromUnencryptedPassword($newPassword); |
87 | } |
88 | |
89 | public function verify(string $password): bool |
90 | { |
91 | if (null === $this->password) { |
92 | throw new LogicException( |
93 | 'User is not activated yet' |
94 | ); |
95 | } |
96 | if ($this->isBlocked()) { |
97 | throw new LogicException('User is blocked'); |
98 | } |
99 | return $this->password->verifyUnencryptedPassword($password); |
100 | } |
101 | |
102 | #[Internal] |
103 | public function getPermissionIdentifiers(): PermissionList |
104 | { |
105 | return new PermissionList([$this->id->toPermission()]); |
106 | } |
107 | } |