forked from marein/php-gaming-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefereeConsumer.php
79 lines (68 loc) · 2.17 KB
/
RefereeConsumer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Gaming\ConnectFour\Port\Adapter\Messaging;
use Gaming\Common\Bus\Bus;
use Gaming\Common\MessageBroker\Model\Consumer\Consumer;
use Gaming\Common\MessageBroker\Model\Consumer\Name;
use Gaming\Common\MessageBroker\Model\Context\Context;
use Gaming\Common\MessageBroker\Model\Message\Message;
use Gaming\Common\MessageBroker\Model\Message\Name as MessageName;
use Gaming\Common\MessageBroker\Model\Subscription\SpecificMessage;
use Gaming\ConnectFour\Application\Game\Command\AssignChatCommand;
final class RefereeConsumer implements Consumer
{
private Bus $commandBus;
public function __construct(Bus $commandBus)
{
$this->commandBus = $commandBus;
}
public function handle(Message $message, Context $context): void
{
$payload = json_decode($message->body(), true, 512, JSON_THROW_ON_ERROR);
match ((string)$message->name()) {
'Chat.InitiateChatResponse' => $this->handleInitiateChatResponse($payload),
'ConnectFour.PlayerJoined' => $this->handlePlayerJoined($payload, $context),
default => true
};
}
public function subscriptions(): array
{
return [
new SpecificMessage('ConnectFour', 'PlayerJoined')
];
}
public function name(): Name
{
return new Name('ConnectFour', 'Referee');
}
/**
* @param array<string, mixed> $payload
*/
private function handleInitiateChatResponse(array $payload): void
{
$this->commandBus->handle(
new AssignChatCommand(
$payload['correlationId'],
$payload['chatId']
)
);
}
/**
* @param array<string, mixed> $payload
*/
private function handlePlayerJoined(array $payload, Context $context): void
{
$context->request(
new Message(
new MessageName('Chat', 'InitiateChat'),
json_encode(
[
'correlationId' => $payload['gameId'],
'authors' => []
],
JSON_THROW_ON_ERROR
)
)
);
}
}