forked from marein/php-gaming-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandConsumer.php
61 lines (51 loc) · 1.64 KB
/
CommandConsumer.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
<?php
declare(strict_types=1);
namespace Gaming\Chat\Infrastructure\Messaging;
use Gaming\Chat\Application\Command\InitiateChatCommand;
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;
final class CommandConsumer 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);
$chatId = $this->commandBus->handle(
new InitiateChatCommand(
$payload['authors']
)
);
assert(is_string($chatId));
$context->reply(
new Message(
new MessageName('Chat', 'InitiateChatResponse'),
json_encode(
[
'chatId' => $chatId,
'correlationId' => $payload['correlationId']
],
JSON_THROW_ON_ERROR
)
)
);
}
public function subscriptions(): array
{
return [
new SpecificMessage('Chat', 'InitiateChat')
];
}
public function name(): Name
{
return new Name('Chat', 'CommandListener');
}
}