Skip to content

Commit 7356390

Browse files
author
Jeroen de Graaf
committed
Extract all invariant assert to private assert* methods
For readability.
1 parent 2404d2b commit 7356390

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

src/Domain/Course/ChangeCourseCapacity.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,24 @@ public function changeCapacity(int $capacity): void
4242
/*
4343
* Protect invariants (business rules).
4444
*/
45-
if (!isset($this->courseId)) {
46-
throw CourseNotFoundException::create();
47-
}
45+
$this->assertCourseExists();
4846

4947
/*
5048
* Apply events when all business rules are met.
5149
*/
5250
$this->apply(new CourseCapacityChangedEvent((string) $this->courseId, $capacity));
5351
}
5452

53+
/**
54+
* @throws CourseNotFoundException
55+
*/
56+
private function assertCourseExists(): void
57+
{
58+
if (!isset($this->courseId)) {
59+
throw CourseNotFoundException::create();
60+
}
61+
}
62+
5563
/*
5664
* Change internal state by subscribing to relevant domain events for any of the domain tags,
5765
* so that this use case can apply its business rules.

src/Domain/StudentToCourseSubscription/SubscribeStudentToCourse.php

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,59 @@ public function subscribe(): void
5757
/*
5858
* Protect invariants (business rules).
5959
*/
60+
$this->assertCourseExists();
61+
$this->assertStudentExists();
62+
$this->assertCourseHasSpotsAvailable();
63+
$this->assertStudentCanSubscribeToMoreCourses();
64+
65+
/*
66+
* Apply events when all business rules are met.
67+
*/
68+
$this->apply(new StudentSubscribedToCourseEvent((string) $this->courseId, (string) $this->studentId));
69+
70+
if ($this->totalCountSubscriptionsForCourse >= $this->courseCapacity) {
71+
$this->apply(new CourseFullyBookedEvent((string) $this->courseId));
72+
}
73+
}
74+
75+
/**
76+
* @throws CourseNotFoundException
77+
*/
78+
private function assertCourseExists(): void
79+
{
6080
if (!isset($this->courseId)) {
6181
throw CourseNotFoundException::create();
6282
}
83+
}
6384

85+
/**
86+
* @throws StudentNotFoundException
87+
*/
88+
private function assertStudentExists(): void
89+
{
6490
if (!isset($this->studentId)) {
6591
throw StudentNotFoundException::create();
6692
}
93+
}
6794

95+
/**
96+
* @throws CourseCannotAcceptMoreStudentsException
97+
*/
98+
private function assertCourseHasSpotsAvailable(): void
99+
{
68100
if ($this->totalCountSubscriptionsForCourse >= $this->courseCapacity) {
69101
throw CourseCannotAcceptMoreStudentsException::create();
70102
}
103+
}
71104

105+
/**
106+
* @throws StudentCannotSubscribeToMoreCoursesException
107+
*/
108+
private function assertStudentCanSubscribeToMoreCourses(): void
109+
{
72110
if ($this->totalCountSubscriptionsForStudent >= 10) {
73111
throw StudentCannotSubscribeToMoreCoursesException::create();
74112
}
75-
76-
/*
77-
* Apply events when all business rules are met.
78-
*/
79-
$this->apply(new StudentSubscribedToCourseEvent((string) $this->courseId, (string) $this->studentId));
80-
81-
if ($this->totalCountSubscriptionsForCourse >= $this->courseCapacity) {
82-
$this->apply(new CourseFullyBookedEvent((string) $this->courseId));
83-
}
84113
}
85114

86115
/*

src/Domain/StudentToCourseSubscription/UnsubscribeStudentFromCourse.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,33 @@ public function unsubscribe(): void
5151
/*
5252
* Protect invariants (business rules).
5353
*/
54+
$this->assertCourseExists();
55+
$this->assertStudentExists();
56+
57+
/*
58+
* Apply events when all business rules are met.
59+
*/
60+
$this->apply(new StudentUnsubscribedFromCourseEvent((string) $this->courseId, (string) $this->studentId));
61+
}
62+
63+
/**
64+
* @throws CourseNotFoundException
65+
*/
66+
private function assertCourseExists(): void
67+
{
5468
if (!isset($this->courseId)) {
5569
throw CourseNotFoundException::create();
5670
}
71+
}
5772

73+
/**
74+
* @throws StudentNotFoundException
75+
*/
76+
private function assertStudentExists(): void
77+
{
5878
if (!isset($this->studentId)) {
5979
throw StudentNotFoundException::create();
6080
}
61-
62-
/*
63-
* Apply events when all business rules are met.
64-
*/
65-
$this->apply(new StudentUnsubscribedFromCourseEvent((string) $this->courseId, (string) $this->studentId));
6681
}
6782

6883
/*

0 commit comments

Comments
 (0)