-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentRegistrationModel.php
More file actions
42 lines (35 loc) · 1.28 KB
/
StudentRegistrationModel.php
File metadata and controls
42 lines (35 loc) · 1.28 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
<?php
declare(strict_types=1);
namespace Demo\Feature\StudentRegistration\Model;
use Backslash\EventStore\Query\EventClass;
use Backslash\EventStore\Query\Identifier;
use Backslash\EventStore\Query\QueryInterface;
use Backslash\Model\AbstractModel;
use Demo\Feature\StudentRegistration\Event\StudentRegisteredEvent;
use Demo\Feature\StudentRegistration\Exception\InvalidStudentIdException;
use Demo\Feature\StudentRegistration\Exception\StudentIdAlreadyUsedException;
class StudentRegistrationModel extends AbstractModel
{
private bool $registered = false;
public static function buildQuery(string $studentId): QueryInterface
{
return EventClass::is(StudentRegisteredEvent::class)
->and(
Identifier::is('studentId', $studentId),
);
}
public function register(string $studentId, string $name): void
{
if ($this->registered) {
throw new StudentIdAlreadyUsedException();
}
if (!ctype_digit($studentId)) {
throw new InvalidStudentIdException();
}
$this->record(new StudentRegisteredEvent($studentId, mb_substr($name, 0, 50)));
}
protected function applyStudentRegisteredEvent(StudentRegisteredEvent $event): void
{
$this->registered = true;
}
}