-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentProjector.php
More file actions
66 lines (54 loc) · 2.27 KB
/
StudentProjector.php
File metadata and controls
66 lines (54 loc) · 2.27 KB
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
<?php
declare(strict_types=1);
namespace Demo\Feature\StudentView\Projection;
use Backslash\EventBus\EventHandlerInterface;
use Backslash\EventBus\EventHandlerTrait;
use Backslash\ProjectionStore\ProjectionStoreInterface;
use Demo\Feature\CourseDefinition\Event\CourseDefinedEvent;
use Demo\Feature\CourseSubscription\Event\StudentSubscribedToCourseEvent;
use Demo\Feature\CourseSubscription\Event\StudentUnsubscribedFromCourseEvent;
use Demo\Feature\StudentRegistration\Event\StudentRegisteredEvent;
class StudentProjector implements EventHandlerInterface
{
use EventHandlerTrait;
private ProjectionStoreInterface $projections;
public function __construct(ProjectionStoreInterface $projections)
{
$this->projections = $projections;
}
protected function handleCourseDefinedEvent(CourseDefinedEvent $event): void
{
$projection = new CourseInternalProjection($event->courseId, $event->name);
$this->projections->store($projection);
}
protected function handleStudentRegisteredEvent(StudentRegisteredEvent $event): void
{
$projection = new StudentProjection($event->studentId, $event->name);
$this->projections->store($projection);
}
protected function handleStudentSubscribedToCourseEvent(StudentSubscribedToCourseEvent $event): void
{
$student = $this->findStudent($event->studentId);
$course = $this->findCourse($event->courseId);
$student->subscribe($event->courseId, $course->getName());
$this->projections->store($student);
}
protected function handleStudentUnsubscribedFromCourseEvent(StudentUnsubscribedFromCourseEvent $event): void
{
$student = $this->findStudent($event->studentId);
$student->unsubscribe($event->courseId);
$this->projections->store($student);
}
private function findStudent(string $studentId): StudentProjection
{
/** @var StudentProjection $p */
$p = $this->projections->find(StudentProjection::id($studentId), StudentProjection::class);
return $p;
}
private function findCourse(string $courseId): CourseInternalProjection
{
/** @var CourseInternalProjection $p */
$p = $this->projections->find($courseId, CourseInternalProjection::class);
return $p;
}
}