Skip to content

Commit 2c2e31f

Browse files
author
Jeroen de Graaf
committed
Improve private state property checks
When subscribing/unsubscribing, all events for either a student or a course will be loaded on reconsititution. This also means subscribing events for a student on other courses, and subscribing events for a course for other students. When this has happened before the course and student for the current subscribe/unsubscribe, certain private state properties are not set yet (as the required created events did not happen yet for this domain id combination). This will result in uninitialized errors. Therefore, make sure this cannot happen by setting defaults.
1 parent 207b99d commit 2c2e31f

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

src/Domain/StudentToCourseSubscription/SubscribeStudentToCourse.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ final class SubscribeStudentToCourse implements EventSourcedDomainContext
3939
/*
4040
* Use private properties to guard idempotency and protect invariants.
4141
*/
42-
private bool $isStudentSubscribedToCourse;
4342
private int $courseCapacity;
43+
private bool $isStudentSubscribedToCourse;
4444
private int $totalCountSubscriptionsForCourse;
4545
private int $totalCountSubscriptionsForStudent;
4646

@@ -55,7 +55,7 @@ public function subscribe(): void
5555
/*
5656
* Guard for idempotency.
5757
*/
58-
if ($this->isStudentSubscribedToCourse) {
58+
if ($this->isStudentSubscribedToCourse ?? false) {
5959
return;
6060
}
6161

@@ -83,7 +83,7 @@ public function subscribe(): void
8383
*/
8484
$this->apply(new StudentSubscribedToCourseEvent((string) $this->courseId, (string) $this->studentId));
8585

86-
if ($this->totalCountSubscriptionsForCourse+1 >= $this->courseCapacity) {
86+
if ($this->totalCountSubscriptionsForCourse >= $this->courseCapacity) {
8787
$this->apply(new CourseFullyBookedEvent((string) $this->courseId));
8888
}
8989
}
@@ -105,7 +105,6 @@ private function onStudentCreatedEvent(StudentCreatedEvent $event): void
105105
{
106106
$this->studentId = new StudentId($event->studentId);
107107
$this->totalCountSubscriptionsForStudent = 0;
108-
$this->isStudentSubscribedToCourse = false;
109108
}
110109

111110
#[DomainEventSubscriber]
@@ -117,28 +116,38 @@ private function onCourseCapacityChangedEvent(CourseCapacityChangedEvent $event)
117116
#[DomainEventSubscriber]
118117
private function onStudentSubscribedToCourseEvent(StudentSubscribedToCourseEvent $event): void
119118
{
120-
if (isset($this->studentId)
121-
&& $this->studentId->equals(new StudentId($event->studentId))
122-
&& $this->courseId->equals(new CourseId($event->courseId))
123-
) {
124-
++$this->totalCountSubscriptionsForStudent;
119+
$studentId = $this->studentId ?? null;
120+
$courseId = $this->courseId ?? null;
121+
122+
if ($studentId?->equals(new StudentId($event->studentId)) && $courseId?->equals(new CourseId($event->courseId))) {
125123
$this->isStudentSubscribedToCourse = true;
126124
}
127125

128-
++$this->totalCountSubscriptionsForCourse;
126+
if ($courseId?->equals(new CourseId($event->courseId))) {
127+
++$this->totalCountSubscriptionsForCourse;
128+
}
129+
130+
if ($studentId?->equals(new StudentId($event->studentId))) {
131+
++$this->totalCountSubscriptionsForStudent;
132+
}
129133
}
130134

131135
#[DomainEventSubscriber]
132136
private function onStudentUnsubscribedFromCourseEvent(StudentUnsubscribedFromCourseEvent $event): void
133137
{
134-
if (isset($this->studentId)
135-
&& $this->studentId->equals(new StudentId($event->studentId))
136-
&& $this->courseId->equals(new CourseId($event->courseId))
137-
) {
138-
--$this->totalCountSubscriptionsForStudent;
138+
$studentId = $this->studentId ?? null;
139+
$courseId = $this->courseId ?? null;
140+
141+
if ($studentId?->equals(new StudentId($event->studentId)) && $courseId?->equals(new CourseId($event->courseId))) {
139142
$this->isStudentSubscribedToCourse = false;
140143
}
141144

142-
--$this->totalCountSubscriptionsForCourse;
145+
if ($courseId?->equals(new CourseId($event->courseId))) {
146+
--$this->totalCountSubscriptionsForCourse;
147+
}
148+
149+
if ($studentId?->equals(new StudentId($event->studentId))) {
150+
--$this->totalCountSubscriptionsForStudent;
151+
}
143152
}
144153
}

src/Domain/StudentToCourseSubscription/UnsubscribeStudentFromCourse.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function unsubscribe(): void
4949
/*
5050
* Guard for idempotency.
5151
*/
52-
if (!$this->isStudentSubscribedToCourse) {
52+
if (!($this->isStudentSubscribedToCourse ?? false)) {
5353
return;
5454
}
5555

@@ -84,27 +84,26 @@ private function onCourseCreatedEvent(CourseCreatedEvent $event): void
8484
private function onStudentCreatedEvent(StudentCreatedEvent $event): void
8585
{
8686
$this->studentId = new StudentId($event->studentId);
87-
$this->isStudentSubscribedToCourse = false;
8887
}
8988

9089
#[DomainEventSubscriber]
9190
private function onStudentSubscribedToCourseEvent(StudentSubscribedToCourseEvent $event): void
9291
{
93-
if (isset($this->studentId)
94-
&& $this->studentId->equals(new StudentId($event->studentId))
95-
&& $this->courseId->equals(new CourseId($event->courseId))
96-
) {
92+
$studentId = $this->studentId ?? null;
93+
$courseId = $this->courseId ?? null;
94+
95+
if ($studentId?->equals(new StudentId($event->studentId)) && $courseId?->equals(new CourseId($event->courseId))) {
9796
$this->isStudentSubscribedToCourse = true;
9897
}
9998
}
10099

101100
#[DomainEventSubscriber]
102101
private function onStudentUnsubscribedFromCourseEvent(StudentUnsubscribedFromCourseEvent $event): void
103102
{
104-
if (isset($this->studentId)
105-
&& $this->studentId->equals(new StudentId($event->studentId))
106-
&& $this->courseId->equals(new CourseId($event->courseId))
107-
) {
103+
$studentId = $this->studentId ?? null;
104+
$courseId = $this->courseId ?? null;
105+
106+
if ($studentId?->equals(new StudentId($event->studentId)) && $courseId?->equals(new CourseId($event->courseId))) {
108107
$this->isStudentSubscribedToCourse = false;
109108
}
110109
}

0 commit comments

Comments
 (0)