-
Notifications
You must be signed in to change notification settings - Fork 27
/
RefereeMessageHandler.php
58 lines (49 loc) · 1.74 KB
/
RefereeMessageHandler.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
<?php
declare(strict_types=1);
namespace Gaming\ConnectFour\Port\Adapter\Messaging;
use Gaming\Common\Bus\Bus;
use Gaming\Common\MessageBroker\Context;
use Gaming\Common\MessageBroker\Message;
use Gaming\Common\MessageBroker\MessageHandler;
use Gaming\ConnectFour\Application\Game\Command\AssignChatCommand;
use GamingPlatform\Api\Chat\V1\ChatV1Factory;
use GamingPlatform\Api\Chat\V1\InitiateChat;
final class RefereeMessageHandler implements MessageHandler
{
public function __construct(
private readonly Bus $commandBus
) {
}
public function handle(Message $message, Context $context): void
{
match ($message->name()) {
'Chat.InitiateChatResponse' => $this->handleInitiateChatResponse($message),
'ConnectFour.PlayerJoined' => $this->handlePlayerJoined($message, $context),
default => true
};
}
private function handleInitiateChatResponse(Message $message): void
{
$response = ChatV1Factory::createInitiateChatResponse($message->body());
$this->commandBus->handle(
new AssignChatCommand(
$response->getCorrelationId(),
$response->getChatId()
)
);
}
private function handlePlayerJoined(Message $message, Context $context): void
{
$payload = json_decode($message->body(), true, 512, JSON_THROW_ON_ERROR);
$context->request(
new Message(
'Chat.InitiateChat',
(new InitiateChat())
->setIdempotencyKey('connect-four.' . $payload['gameId'])
->setCorrelationId($payload['gameId'])
->setAuthors([])
->serializeToString()
)
);
}
}