-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandHandler.php
289 lines (266 loc) · 14.6 KB
/
CommandHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
declare(strict_types=1);
namespace Wwwision\DCBExample;
use RuntimeException;
use Webmozart\Assert\Assert;
use Wwwision\DCBEventStore\EventStore;
use Wwwision\DCBEventStore\Types\AppendCondition;
use Wwwision\DCBEventStore\Types\ExpectedHighestSequenceNumber;
use Wwwision\DCBEventStore\Types\StreamQuery\StreamQuery;
use Wwwision\DCBExample\Command\Command;
use Wwwision\DCBExample\Command\CreateCourse;
use Wwwision\DCBExample\Command\RegisterStudent;
use Wwwision\DCBExample\Command\RenameCourse;
use Wwwision\DCBExample\Command\SubscribeStudentToCourse;
use Wwwision\DCBExample\Command\UnsubscribeStudentFromCourse;
use Wwwision\DCBExample\Command\UpdateCourseCapacity;
use Wwwision\DCBExample\DecisionModel\DecisionModel;
use Wwwision\DCBExample\Event\CourseCapacityChanged;
use Wwwision\DCBExample\Event\CourseCreated;
use Wwwision\DCBExample\Event\CourseRenamed;
use Wwwision\DCBExample\Event\StudentRegistered;
use Wwwision\DCBExample\Event\StudentSubscribedToCourse;
use Wwwision\DCBExample\Event\StudentUnsubscribedFromCourse;
use Wwwision\DCBExample\Exception\ConstraintException;
use Wwwision\DCBExample\Projection\ClosureProjection;
use Wwwision\DCBExample\Projection\CompositeProjection;
use Wwwision\DCBExample\Projection\Projection;
use Wwwision\DCBExample\Projection\TaggedProjection;
use Wwwision\DCBExample\Types\CourseCapacity;
use Wwwision\DCBExample\Types\CourseId;
use Wwwision\DCBExample\Types\CourseIds;
use Wwwision\DCBExample\Types\CourseTitle;
use Wwwision\DCBExample\Types\StudentId;
use function PHPStan\dumpType;
use function sprintf;
/**
* Main authority of this package, responsible to handle incoming {@see Command}s (@see self::handle()}
*/
final readonly class CommandHandler
{
private EventSerializer $eventSerializer;
public function __construct(
private EventStore $eventStore,
) {
$this->eventSerializer = new EventSerializer();
}
public function handle(Command $command): void
{
match ($command::class) {
CreateCourse::class => $this->handleCreateCourse($command),
RenameCourse::class => $this->handleRenameCourse($command),
RegisterStudent::class => $this->handleRegisterStudent($command),
SubscribeStudentToCourse::class => $this->handleSubscribeStudentToCourse($command),
UnsubscribeStudentFromCourse::class => $this->handleUnsubscribeStudentFromCourse($command),
UpdateCourseCapacity::class => $this->handleUpdateCourseCapacity($command),
default => throw new RuntimeException(sprintf('Unsupported command %s', $command::class), 1684579212),
};
}
private function handleCreateCourse(CreateCourse $command): void
{
$decisionModel = $this->buildDecisionModel(
courseExists: self::courseExists($command->courseId),
);
if ($decisionModel->state->courseExists) {
throw new ConstraintException(sprintf('Failed to create course with id "%s" because a course with that id already exists', $command->courseId->value), 1684593925);
}
$domainEvent = new CourseCreated($command->courseId, $command->initialCapacity, $command->courseTitle);
$this->eventStore->append($this->eventSerializer->convertDomainEvent($domainEvent), $decisionModel->appendCondition);
}
private function handleRenameCourse(RenameCourse $command): void
{
$decisionModel = $this->buildDecisionModel(
courseExists: self::courseExists($command->courseId),
courseTitle: self::courseTitle($command->courseId),
);
if (!$decisionModel->state->courseExists) {
throw new ConstraintException(sprintf('Failed to rename course with id "%s" because a course with that id does not exist', $command->courseId->value), 1684509782);
}
if ($decisionModel->state->courseTitle->equals($command->newCourseTitle)) {
throw new ConstraintException(sprintf('Failed to rename course with id "%s" to "%s" because this is already the title of this course', $command->courseId->value, $command->newCourseTitle->value), 1684509837);
}
$domainEvent = new CourseRenamed($command->courseId, $command->newCourseTitle);
$this->eventStore->append($this->eventSerializer->convertDomainEvent($domainEvent), $decisionModel->appendCondition);
}
private function handleRegisterStudent(RegisterStudent $command): void
{
$decisionModel = $this->buildDecisionModel(
studentRegistered: self::studentRegistered($command->studentId),
);
if ($decisionModel->state->studentRegistered) {
throw new ConstraintException(sprintf('Failed to register student with id "%s" because a student with that id already exists', $command->studentId->value), 1684579300);
}
$domainEvent = new StudentRegistered($command->studentId);
$this->eventStore->append($this->eventSerializer->convertDomainEvent($domainEvent), $decisionModel->appendCondition);
}
private function handleSubscribeStudentToCourse(SubscribeStudentToCourse $command): void
{
$decisionModel = $this->buildDecisionModel(
courseExists: self::courseExists($command->courseId),
studentRegistered: self::studentRegistered($command->studentId),
courseCapacity: self::courseCapacity($command->courseId),
numberOfCourseSubscriptions: self::numberOfCourseSubscriptions($command->courseId),
studentSubscriptions: self::studentSubscriptions($command->studentId),
);
if (!$decisionModel->state->courseExists) {
throw new ConstraintException(sprintf('Failed to subscribe student with id "%s" to course with id "%s" because a course with that id does not exist', $command->studentId->value, $command->courseId->value), 1685266122);
}
if (!$decisionModel->state->studentRegistered) {
throw new ConstraintException(sprintf('Failed to subscribe student with id "%s" to course with id "%s" because a student with that id does not exist', $command->studentId->value, $command->courseId->value), 1686914105);
}
if ($decisionModel->state->courseCapacity->value === $decisionModel->state->numberOfCourseSubscriptions) {
throw new ConstraintException(sprintf('Failed to subscribe student with id "%s" to course with id "%s" because the course\'s capacity of %d is reached', $command->studentId->value, $command->courseId->value, $decisionModel->state->courseCapacity->value), 1684603201);
}
if ($decisionModel->state->studentSubscriptions->contains($command->courseId)) {
throw new ConstraintException(sprintf('Failed to subscribe student with id "%s" to course with id "%s" because that student is already subscribed to this course', $command->studentId->value, $command->courseId->value), 1684510963);
}
$maximumSubscriptionsPerStudent = 10;
if ($decisionModel->state->studentSubscriptions->count() === $maximumSubscriptionsPerStudent) {
throw new ConstraintException(sprintf('Failed to subscribe student with id "%s" to course with id "%s" because that student is already subscribed the maximum of %d courses', $command->studentId->value, $command->courseId->value, $maximumSubscriptionsPerStudent), 1684605232);
}
$domainEvent = new StudentSubscribedToCourse($command->courseId, $command->studentId);
$this->eventStore->append($this->eventSerializer->convertDomainEvent($domainEvent), $decisionModel->appendCondition);
}
private function handleUnsubscribeStudentFromCourse(UnsubscribeStudentFromCourse $command): void
{
$decisionModel = $this->buildDecisionModel(
courseExists: self::courseExists($command->courseId),
studentRegistered: self::studentRegistered($command->studentId),
studentSubscriptions: self::studentSubscriptions($command->studentId),
);
if (!$decisionModel->state->courseExists) {
throw new ConstraintException(sprintf('Failed to unsubscribe student with id "%s" from course with id "%s" because a course with that id does not exist', $command->studentId->value, $command->courseId->value), 1684579448);
}
if (!$decisionModel->state->studentRegistered) {
throw new ConstraintException(sprintf('Failed to unsubscribe student with id "%s" from course with id "%s" because a student with that id does not exist', $command->studentId->value, $command->courseId->value), 1684579463);
}
if (!$decisionModel->state->studentSubscriptions->contains($command->courseId)) {
throw new ConstraintException(sprintf('Failed to unsubscribe student with id "%s" from course with id "%s" because that student is not subscribed to this course', $command->studentId->value, $command->courseId->value), 1684579464);
}
$domainEvent = new StudentUnsubscribedFromCourse($command->studentId, $command->courseId);
$this->eventStore->append($this->eventSerializer->convertDomainEvent($domainEvent), $decisionModel->appendCondition);
}
private function handleUpdateCourseCapacity(UpdateCourseCapacity $command): void
{
$decisionModel = $this->buildDecisionModel(
courseExists: self::courseExists($command->courseId),
courseCapacity: self::courseCapacity($command->courseId),
numberOfCourseSubscriptions: self::numberOfCourseSubscriptions($command->courseId),
);
if (!$decisionModel->state->courseExists) {
throw new ConstraintException(sprintf('Failed to change capacity of course with id "%s" to %d because a course with that id does not exist', $command->courseId->value, $command->newCapacity->value), 1684604283);
}
if ($decisionModel->state->courseCapacity->equals($command->newCapacity)) {
throw new ConstraintException(sprintf('Failed to change capacity of course with id "%s" to %d because that is already the courses capacity', $command->courseId->value, $command->newCapacity->value), 1686819073);
}
if ($decisionModel->state->numberOfCourseSubscriptions > $command->newCapacity->value) {
throw new ConstraintException(sprintf('Failed to change capacity of course with id "%s" to %d because it already has %d active subscriptions', $command->courseId->value, $command->newCapacity->value, $decisionModel->state->numberOfCourseSubscriptions), 1684604361);
}
$domainEvent = new CourseCapacityChanged($command->courseId, $command->newCapacity);
$this->eventStore->append($this->eventSerializer->convertDomainEvent($domainEvent), $decisionModel->appendCondition);
}
// -----------------------------
/**
* @return Projection<bool>
*/
private static function courseExists(CourseId $courseId): Projection
{
return TaggedProjection::create(
$courseId->toTag(),
ClosureProjection::create(
initialState: false,
onlyLastEvent: true,
)
->when(CourseCreated::class, fn() => true)
);
}
/**
* @return Projection<CourseCapacity>
*/
private static function courseCapacity(CourseId $courseId): Projection
{
return TaggedProjection::create(
$courseId->toTag(),
ClosureProjection::create(
initialState: CourseCapacity::fromInteger(0),
)
->when(CourseCreated::class, fn($_, CourseCreated $event) => $event->initialCapacity)
->when(CourseCapacityChanged::class, fn($_, CourseCapacityChanged $event) => $event->newCapacity)
);
}
/**
* @return Projection<int>
*/
private static function numberOfCourseSubscriptions(CourseId $courseId): Projection
{
return TaggedProjection::create(
$courseId->toTag(),
ClosureProjection::create(
initialState: 0,
)
->when(StudentSubscribedToCourse::class, fn(int $state) => $state + 1)
->when(StudentUnsubscribedFromCourse::class, fn(int $state) => $state - 1)
);
}
/**
* @return Projection<CourseTitle>
*/
private static function courseTitle(CourseId $courseId): Projection
{
return TaggedProjection::create(
$courseId->toTag(),
ClosureProjection::create(
initialState: CourseTitle::fromString(''),
)
->when(CourseCreated::class, static fn ($_, CourseCreated $event) => $event->courseTitle)
->when(CourseRenamed::class, static fn ($_, CourseRenamed $event) => $event->newCourseTitle)
);
}
/**
* @return Projection<bool>
*/
private static function studentRegistered(StudentId $studentId): Projection
{
return TaggedProjection::create(
$studentId->toTag(),
ClosureProjection::create(
initialState: false,
onlyLastEvent: true,
)
->when(StudentRegistered::class, fn() => true)
);
}
/**
* @return Projection<CourseIds>
*/
private static function studentSubscriptions(StudentId $studentId): Projection
{
return TaggedProjection::create(
$studentId->toTag(),
ClosureProjection::create(
initialState: CourseIds::none(),
)
->when(StudentSubscribedToCourse::class, static fn (CourseIds $state, StudentSubscribedToCourse $event) => $state->with($event->courseId))
->when(StudentUnsubscribedFromCourse::class, static fn (CourseIds $state, StudentUnsubscribedFromCourse $event) => $state->without($event->courseId))
);
}
// ------------------------------------
/**
* @phpstan-ignore-next-line
*/
private function buildDecisionModel(Projection ...$projections): DecisionModel
{
$query = StreamQuery::wildcard();
Assert::isMap($projections);
$compositeProjection = CompositeProjection::create($projections);
$query = $query->withCriteria($compositeProjection->getCriteria());
$expectedHighestSequenceNumber = ExpectedHighestSequenceNumber::none();
$state = $compositeProjection->initialState();
foreach ($this->eventStore->read($query) as $eventEnvelope) {
$domainEvent = $this->eventSerializer->convertEvent($eventEnvelope->event);
$state = $compositeProjection->apply($state, $domainEvent, $eventEnvelope);
$expectedHighestSequenceNumber = ExpectedHighestSequenceNumber::fromSequenceNumber($eventEnvelope->sequenceNumber);
}
return new DecisionModel($state, new AppendCondition($query, $expectedHighestSequenceNumber));
}
}