-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.php
42 lines (34 loc) · 1.18 KB
/
Student.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
<?php
declare(strict_types=1);
namespace Gember\ExampleEventSourcingDcb\Domain\Student;
use Gember\EventSourcing\DomainContext\Attribute\DomainEventSubscriber;
use Gember\EventSourcing\DomainContext\Attribute\DomainId;
use Gember\EventSourcing\DomainContext\EventSourcedDomainContext;
use Gember\EventSourcing\DomainContext\EventSourcedDomainContextBehaviorTrait;
/**
* Traditional aggregate root.
*/
final class Student implements EventSourcedDomainContext
{
use EventSourcedDomainContextBehaviorTrait;
/*
* Define to which domain identifiers this context belongs to.
*/
#[DomainId]
private StudentId $studentId;
public static function create(StudentId $studentId): self
{
$student = new self();
$student->apply(new StudentCreatedEvent((string) $studentId));
return $student;
}
/*
* Change internal state by subscribing to relevant domain events for any of the domain identifiers,
* so that this context can apply its business rules.
*/
#[DomainEventSubscriber]
private function onStudentCreatedEvent(StudentCreatedEvent $event): void
{
$this->studentId = new StudentId($event->studentId);
}
}