1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Darkilliant\MqProcessBundle\Message\Provider; |
6
|
|
|
|
7
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
8
|
|
|
use Darkilliant\MqProcessBundle\Message\Message; |
9
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
10
|
|
|
|
11
|
|
|
class AmqpMessageProvider implements MessageProviderInterface |
12
|
|
|
{ |
13
|
|
|
/** @var AMQPChannel */ |
14
|
|
|
private $channel; |
15
|
|
|
/** @var string */ |
16
|
|
|
private $queue; |
17
|
|
|
/** @var array */ |
18
|
|
|
private $messages = []; |
19
|
|
|
/** @var string */ |
20
|
|
|
private $consumerTag; |
21
|
|
|
|
22
|
9 |
|
public function __construct(AMQPChannel $channel, string $queue, string $exchange, bool $persistant, int $batchCount) |
|
|
|
|
23
|
|
|
{ |
24
|
9 |
|
$this->channel = $channel; |
25
|
9 |
|
$this->queue = $queue; |
26
|
|
|
|
27
|
9 |
|
$this->channel->queue_declare($this->queue, false, $persistant, false, false); |
28
|
9 |
|
$this->channel->queue_bind($queue, $exchange, $this->queue); |
29
|
9 |
|
$this->channel->basic_qos(null, 5000, false); |
30
|
9 |
|
} |
31
|
|
|
|
32
|
1 |
|
public function consume($ackRequired = true) |
33
|
|
|
{ |
34
|
1 |
|
$this->channel->basic_consume( |
35
|
1 |
|
$this->queue, |
36
|
1 |
|
$this->getConsumerTag(), |
37
|
|
|
// when true, the server will not send messages to the connection that published them |
38
|
1 |
|
false, |
39
|
|
|
// when true, disable acknowledgements (more speed) |
40
|
1 |
|
!$ackRequired, |
41
|
|
|
// when true, queues may only be accessed by the current connection |
42
|
1 |
|
false, |
43
|
|
|
// when true, the server will not respond to the method. |
44
|
1 |
|
false, |
45
|
1 |
|
[$this, 'process'] //callback |
46
|
|
|
); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
public function stopConsume() |
50
|
|
|
{ |
51
|
1 |
|
$this->channel->basic_cancel($this->queue); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
2 |
|
public function process($envelope) |
55
|
|
|
{ |
56
|
2 |
|
if (null === $envelope) { |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->messages[] = $message = $this->buildMessage($envelope); |
|
|
|
|
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
public function messageOk(Message $message) |
64
|
|
|
{ |
65
|
1 |
|
$this->channel->basic_ack($message->getId()); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function messageKo(Message $message, bool $requeue) |
69
|
|
|
{ |
70
|
1 |
|
$this->channel->basic_nack($message->getId(), false, $requeue); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function waitChannel($blocking = true) |
74
|
|
|
{ |
75
|
1 |
|
$this->channel->wait(null, !$blocking); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
3 |
|
public function fetchMessage() |
79
|
|
|
{ |
80
|
3 |
|
return array_shift($this->messages); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @codeCoverageIgnore |
85
|
|
|
*/ |
86
|
|
|
protected function getConsumerTag(): string |
87
|
|
|
{ |
88
|
|
|
if (null === $this->consumerTag) { |
89
|
|
|
$this->consumerTag = sprintf('mqprocess_%s_%s_%s', $this->queue, time(), uniqid()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->consumerTag; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
private function buildMessage(AMQPMessage $envelope): Message |
96
|
|
|
{ |
97
|
1 |
|
$properties = []; |
98
|
|
|
$propertyKeys = [ |
99
|
1 |
|
'content_type', 'delivery_mode', 'content_encoding', 'type', 'timestamp', 'priority', 'expiration', |
100
|
|
|
'app_id', 'message_id', 'reply_to', 'correlation_id', 'user_id', 'cluster_id', 'channel', 'consumer_tag', |
101
|
|
|
'delivery_tag', 'redelivered', 'exchange', 'routing_key', |
102
|
|
|
]; |
103
|
|
|
|
104
|
1 |
|
foreach ($propertyKeys as $key) { |
105
|
1 |
|
if ($envelope->has($key)) { |
106
|
1 |
|
$properties[$key] = $envelope->get($key); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$properties['headers'] = []; |
111
|
1 |
|
if ($envelope->has('application_headers')) { |
112
|
1 |
|
foreach ($envelope->get('application_headers') as $key => $value) { |
113
|
1 |
|
$properties['headers'][$key] = $value[1]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return new Message($envelope->body, $properties, $envelope->get('delivery_tag')); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.