ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
PendingRegistration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26
27final readonly class PendingRegistration
28{
29 public function __construct(
31 private ObjectId $usr_id,
32 private PendingRegistrationHash $hash,
33 private \DateTimeImmutable $created_at,
34 private PendingRegistrationStatus $status = PendingRegistrationStatus::PENDING
35 ) {
36 }
37
38 public function id(): PendingRegistrationId
39 {
40 return $this->id;
41 }
42
43 public function userId(): ObjectId
44 {
45 return $this->usr_id;
46 }
47
48 public function hash(): PendingRegistrationHash
49 {
50 return $this->hash;
51 }
52
53 public function createdAt(): \DateTimeImmutable
54 {
55 return $this->created_at;
56 }
57
59 {
60 return $this->status;
61 }
62
63 public function isConfirmed(): bool
64 {
65 return $this->status === PendingRegistrationStatus::CONFIRMED;
66 }
67
68 public function isExpired(): bool
69 {
71 }
72
73 public function isPending(): bool
74 {
75 return $this->status === PendingRegistrationStatus::PENDING;
76 }
77
78 public function withConfirmed(): self
79 {
81 throw new \DomainException('Cannot confirm an expired registration.');
82 }
83
84 if ($this->status === PendingRegistrationStatus::CONFIRMED) {
85 return $this;
86 }
87
88 return new self(
90 $this->usr_id,
91 $this->hash,
92 $this->created_at,
93 PendingRegistrationStatus::CONFIRMED
94 );
95 }
96
97 public function withExpired(): self
98 {
99 if ($this->status === PendingRegistrationStatus::CONFIRMED) {
100 throw new \DomainException('Cannot expire an already confirmed registration.');
101 }
102
104 return $this;
105 }
106
107 return new self(
108 $this->id,
109 $this->usr_id,
110 $this->hash,
111 $this->created_at,
113 );
114 }
115
116 public function hasExpiredAt(\DateTimeImmutable $now, int $validity_in_seconds): bool
117 {
118 $expiration_date = $this->created_at->modify("+{$validity_in_seconds} seconds");
119
120 return $now >= $expiration_date;
121 }
122
126 public function withEvaluatedState(\DateTimeImmutable $now, ?int $validity_in_seconds): self
127 {
128 if (\is_int($validity_in_seconds) && $validity_in_seconds < 1) {
129 throw new \InvalidArgumentException('Invalid validity_in_seconds value.');
130 }
131
132 if ($this->status === PendingRegistrationStatus::CONFIRMED) {
133 return $this;
134 }
135
136 if (\is_int($validity_in_seconds) && $this->hasExpiredAt($now, $validity_in_seconds)) {
137 return $this->withExpired();
138 }
139
140 return $this;
141 }
142}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
withEvaluatedState(\DateTimeImmutable $now, ?int $validity_in_seconds)
__construct(private PendingRegistrationId $id, private ObjectId $usr_id, private PendingRegistrationHash $hash, private \DateTimeImmutable $created_at, private PendingRegistrationStatus $status=PendingRegistrationStatus::PENDING)
hasExpiredAt(\DateTimeImmutable $now, int $validity_in_seconds)