-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-event-dispatcher.php
62 lines (44 loc) · 1014 Bytes
/
10-event-dispatcher.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php declare(strict_types = 1);
class CreateUserHandler
{
public function handle(CreateUserCommand $command) {
$data = [
'name' => $command->name,
'email' => $command->email,
];
$this->userRepository->create($data);
$this->mailer->send($command->email, 'Welcome');
$this->logger->info('User created', $data);
}
}
class CreateUserHandler
{
public function handle(CreateUserCommand $command) {
$data = [
'name' => $command->name,
'email' => $command->email,
];
$this->userRepository->create($data);
$this->eventDispatcher->dispatch(
new UserCreatedEvent($data)
);
}
}
class EventDispatcher
{
public function dispatch($event) {
foreach ($this->subscribers[$event] as $subscriber) {
$subscriber->handle($event);
}
}
}
class LoggerSubscriber
{
public static function getSubscribedEvents()
{
return [UserCreatedEvent::class => 'handle'];
}
public function handle(UserCreatedEvent $event) {
$this->logger->info('User created', $event->data);
}
}