1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Clients; |
4
|
|
|
|
5
|
|
|
use Puzzle\AMQP\Client; |
6
|
|
|
use Puzzle\AMQP\WritableMessage; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
use Puzzle\AMQP\Clients\Processors\MessageProcessorAware; |
9
|
|
|
|
10
|
|
|
class InMemory implements Client |
11
|
|
|
{ |
12
|
|
|
use |
13
|
|
|
\Psr\Log\LoggerAwareTrait, |
14
|
|
|
MessageProcessorAware; |
15
|
|
|
|
16
|
|
|
private |
17
|
|
|
$sentMessages; |
18
|
|
|
|
19
|
24 |
|
public function __construct() |
20
|
|
|
{ |
21
|
24 |
|
$this->sentMessages = []; |
22
|
24 |
|
$this->logger = new NullLogger(); |
23
|
24 |
|
} |
24
|
|
|
|
25
|
19 |
|
public function publish(string $exchangeName, WritableMessage $message): bool |
26
|
|
|
{ |
27
|
19 |
|
$this->updateMessageAttributes($message); |
28
|
19 |
|
$this->saveMessage($exchangeName, $message); |
29
|
|
|
|
30
|
19 |
|
return true; |
31
|
|
|
} |
32
|
|
|
|
33
|
19 |
|
private function updateMessageAttributes(WritableMessage $message): void |
34
|
|
|
{ |
35
|
19 |
|
$message->setAttribute('app_id', 'memory'); |
36
|
19 |
|
$message->addHeader('routing_key', $message->getRoutingKey()); |
37
|
|
|
|
38
|
19 |
|
$this->onPublish($message); |
39
|
19 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function getQueue(string $queueName): \AMQPQueue |
42
|
|
|
{ |
43
|
1 |
|
throw new \RuntimeException('This AMQP Client must be used only for sending purpose'); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function getExchange(?string $exchangeName = null, string $type = AMQP_EX_TYPE_TOPIC): \AMQPExchange |
47
|
|
|
{ |
48
|
1 |
|
throw new \RuntimeException('This AMQP Client must be used only for sending purpose'); |
49
|
|
|
} |
50
|
|
|
|
51
|
19 |
|
private function saveMessage($exchangeName, WritableMessage $message) |
52
|
|
|
{ |
53
|
19 |
|
$this->sentMessages[] = [ |
54
|
19 |
|
'exchange' => $exchangeName, |
55
|
19 |
|
'message' => $message |
56
|
|
|
]; |
57
|
19 |
|
} |
58
|
|
|
|
59
|
12 |
|
public function getSentMessages(): array |
60
|
|
|
{ |
61
|
12 |
|
return $this->sentMessages; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function dropSentMessages(): self |
65
|
|
|
{ |
66
|
1 |
|
$this->sentMessages = []; |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|