-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProfile.php
More file actions
89 lines (74 loc) · 2.28 KB
/
Copy pathProfile.php
File metadata and controls
89 lines (74 loc) · 2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Apply;
use Patchlevel\EventSourcing\Attribute\Id;
use Patchlevel\EventSourcing\Attribute\Snapshot;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\EmailChanged;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\NameChanged;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\Reborn;
#[Aggregate('profile')]
#[Snapshot('default')]
final class Profile extends BasicAggregateRoot
{
#[Id]
private ProfileId $id;
private string $name;
private string|null $email;
public static function create(ProfileId $id, string $name, string|null $email = null): self
{
$self = new self();
$self->recordThat(new ProfileCreated($id, $name, $email));
return $self;
}
public function changeName(string $name): void
{
$this->recordThat(new NameChanged($this->id, $name));
}
public function changeEmail(string $email): void
{
$this->recordThat(new EmailChanged($this->id, $email));
}
public function reborn(): void
{
$this->recordThat(new Reborn(
$this->id,
$this->name,
));
}
#[Apply]
protected function applyProfileCreated(ProfileCreated $event): void
{
$this->id = $event->profileId;
$this->name = $event->name;
$this->email = $event->email;
}
#[Apply]
protected function applyNameChanged(NameChanged $event): void
{
$this->name = $event->name;
}
#[Apply]
protected function applyEmailChanged(EmailChanged $event): void
{
$this->email = $event->email;
}
#[Apply]
protected function applyReborn(Reborn $event): void
{
$this->id = $event->profileId;
$this->name = $event->name;
$this->email = null;
}
public function name(): string
{
return $this->name;
}
public function email(): string|null
{
return $this->email;
}
}