Skip to content

Commit cde3e3c

Browse files
committed
[client] fix tests, update rabbitmq driver.
1 parent 613de56 commit cde3e3c

4 files changed

Lines changed: 85 additions & 646 deletions

File tree

‎pkg/enqueue/Client/Driver/AmqpDriver.php‎

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55
namespace Enqueue\Client\Driver;
66

7+
use Enqueue\AmqpExt\AmqpProducer;
78
use Enqueue\Client\Config;
89
use Enqueue\Client\Message;
9-
use Enqueue\Client\MessagePriority;
1010
use Enqueue\Client\RouteCollection;
1111
use Interop\Amqp\AmqpContext;
1212
use Interop\Amqp\AmqpMessage;
1313
use Interop\Amqp\AmqpQueue;
1414
use Interop\Amqp\AmqpTopic;
1515
use Interop\Amqp\Impl\AmqpBind;
1616
use Interop\Queue\PsrMessage;
17+
use Interop\Queue\PsrProducer;
1718
use Interop\Queue\PsrQueue;
1819
use Interop\Queue\PsrTopic;
1920
use Psr\Log\LoggerInterface;
@@ -31,11 +32,6 @@ class AmqpDriver extends GenericDriver
3132
*/
3233
private $config;
3334

34-
/**
35-
* @var array
36-
*/
37-
private $priorityMap;
38-
3935
/**
4036
* @var RouteCollection
4137
*/
@@ -47,14 +43,6 @@ public function __construct(AmqpContext $context, Config $config, RouteCollectio
4743
$this->config = $config;
4844
$this->routeCollection = $routeCollection;
4945

50-
$this->priorityMap = [
51-
MessagePriority::VERY_LOW => 0,
52-
MessagePriority::LOW => 1,
53-
MessagePriority::NORMAL => 2,
54-
MessagePriority::HIGH => 3,
55-
MessagePriority::VERY_HIGH => 4,
56-
];
57-
5846
parent::__construct($context, $config, $routeCollection);
5947
}
6048

@@ -72,16 +60,17 @@ public function createTransportMessage(Message $clientMessage): PsrMessage
7260
$transportMessage->setExpiration($clientMessage->getExpire() * 1000);
7361
}
7462

63+
$priorityMap = $this->getPriorityMap();
7564
if ($priority = $clientMessage->getPriority()) {
76-
if (false == array_key_exists($priority, $this->getPriorityMap())) {
65+
if (false == array_key_exists($priority, $priorityMap)) {
7766
throw new \InvalidArgumentException(sprintf(
7867
'Cant convert client priority "%s" to transport one. Could be one of "%s"',
7968
$priority,
80-
implode('", "', array_keys($this->getPriorityMap()))
69+
implode('", "', array_keys($priorityMap))
8170
));
8271
}
8372

84-
$transportMessage->setPriority($this->priorityMap[$priority]);
73+
$transportMessage->setPriority($priorityMap[$priority]);
8574
}
8675

8776
return $transportMessage;
@@ -135,17 +124,18 @@ public function createQueue(string $clientQueuName): PsrQueue
135124
}
136125

137126
/**
138-
* @param AmqpTopic $topic
139-
* @param AmqpMessage $transportMessage
127+
* @param AmqpProducer $producer
128+
* @param AmqpTopic $topic
129+
* @param AmqpMessage $transportMessage
140130
*/
141-
protected function doSendToRouter(PsrTopic $topic, PsrMessage $transportMessage): void
131+
protected function doSendToRouter(PsrProducer $producer, PsrTopic $topic, PsrMessage $transportMessage): void
142132
{
143133
// We should not handle priority, expiration, and delay at this stage.
144134
// The router will take care of it while re-sending the message to the final destinations.
145135
$transportMessage->setPriority(null);
146136
$transportMessage->setExpiration(null);
147137

148-
$this->context->createProducer()->send($topic, $transportMessage);
138+
$producer->send($topic, $transportMessage);
149139
}
150140

151141
/**
@@ -160,9 +150,4 @@ protected function createRouterTopic(): PsrTopic
160150

161151
return $topic;
162152
}
163-
164-
protected function getPriorityMap(): array
165-
{
166-
return $this->priorityMap;
167-
}
168153
}

‎pkg/enqueue/Client/Driver/RabbitMqDriver.php‎

Lines changed: 1 addition & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,11 @@
22

33
namespace Enqueue\Client\Driver;
44

5-
use Enqueue\Client\Config;
6-
use Enqueue\Client\Message;
7-
use Enqueue\Client\MessagePriority;
8-
use Enqueue\Client\Meta\QueueMetaRegistry;
9-
use Enqueue\Consumption\Exception\LogicException;
10-
use Interop\Amqp\AmqpContext;
11-
use Interop\Amqp\AmqpMessage;
125
use Interop\Amqp\AmqpQueue;
13-
use Interop\Queue\PsrMessage;
146
use Interop\Queue\PsrQueue;
157

16-
class RabbitMqDriver extends AmqpDriver
8+
final class RabbitMqDriver extends AmqpDriver
179
{
18-
/**
19-
* @var AmqpContext
20-
*/
21-
private $context;
22-
23-
/**
24-
* @var Config
25-
*/
26-
private $config;
27-
28-
/**
29-
* @var QueueMetaRegistry
30-
*/
31-
private $queueMetaRegistry;
32-
33-
/**
34-
* @var array
35-
*/
36-
private $priorityMap;
37-
38-
public function __construct(AmqpContext $context, Config $config, QueueMetaRegistry $queueMetaRegistry)
39-
{
40-
parent::__construct($context, $config, $queueMetaRegistry);
41-
42-
$this->config = $config;
43-
$this->context = $context;
44-
$this->queueMetaRegistry = $queueMetaRegistry;
45-
46-
$this->priorityMap = [
47-
MessagePriority::VERY_LOW => 0,
48-
MessagePriority::LOW => 1,
49-
MessagePriority::NORMAL => 2,
50-
MessagePriority::HIGH => 3,
51-
MessagePriority::VERY_HIGH => 4,
52-
];
53-
}
54-
55-
public function sendToProcessor(Message $message): void
56-
{
57-
if (false == $message->getProperty(Config::PARAMETER_PROCESSOR_NAME)) {
58-
throw new \LogicException('Processor name parameter is required but is not set');
59-
}
60-
61-
if (false == $queueName = $message->getProperty(Config::PARAMETER_PROCESSOR_QUEUE_NAME)) {
62-
throw new \LogicException('Queue name parameter is required but is not set');
63-
}
64-
65-
$transportMessage = $this->createTransportMessage($message);
66-
$destination = $this->createQueue($queueName);
67-
$producer = $this->context->createProducer();
68-
69-
if ($message->getDelay()) {
70-
$producer->setDeliveryDelay($message->getDelay() * 1000);
71-
}
72-
73-
$producer->send($destination, $transportMessage);
74-
}
75-
7610
/**
7711
* @return AmqpQueue
7812
*/
@@ -83,59 +17,4 @@ public function createQueue(string $queueName): PsrQueue
8317

8418
return $queue;
8519
}
86-
87-
/**
88-
* @return AmqpMessage
89-
*/
90-
public function createTransportMessage(Message $message): PsrMessage
91-
{
92-
$transportMessage = parent::createTransportMessage($message);
93-
94-
if ($priority = $message->getPriority()) {
95-
if (false == array_key_exists($priority, $this->priorityMap)) {
96-
throw new \InvalidArgumentException(sprintf(
97-
'Given priority could not be converted to client\'s one. Got: %s',
98-
$priority
99-
));
100-
}
101-
102-
$transportMessage->setPriority($this->priorityMap[$priority]);
103-
}
104-
105-
if ($message->getDelay()) {
106-
if (false == $this->config->getTransportOption('delay_strategy', false)) {
107-
throw new LogicException('The message delaying is not supported. In order to use delay feature install RabbitMQ delay strategy.');
108-
}
109-
110-
$transportMessage->setProperty('enqueue-delay', $message->getDelay() * 1000);
111-
}
112-
113-
return $transportMessage;
114-
}
115-
116-
/**
117-
* @param AmqpMessage $message
118-
*/
119-
public function createClientMessage(PsrMessage $message): Message
120-
{
121-
$clientMessage = parent::createClientMessage($message);
122-
123-
if ($priority = $message->getPriority()) {
124-
if (false === $clientPriority = array_search($priority, $this->priorityMap, true)) {
125-
throw new \LogicException(sprintf('Cant convert transport priority to client: "%s"', $priority));
126-
}
127-
128-
$clientMessage->setPriority($clientPriority);
129-
}
130-
131-
if ($delay = $message->getProperty('enqueue-delay')) {
132-
if (false == is_numeric($delay)) {
133-
throw new \LogicException(sprintf('"enqueue-delay" header is not numeric. "%s"', $delay));
134-
}
135-
136-
$clientMessage->setDelay((int) ((int) $delay) / 1000);
137-
}
138-
139-
return $clientMessage;
140-
}
14120
}

‎pkg/enqueue/Tests/Client/Driver/AmqpDriverTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ class AmqpDriverTest extends TestCase
3030

3131
public function testShouldImplementsDriverInterface()
3232
{
33-
$this->assertClassImplements(DriverInterface::class, GenericDriver::class);
33+
$this->assertClassImplements(DriverInterface::class, AmqpDriver::class);
34+
}
35+
36+
public function testShouldBeSubClassOfGenericDriver()
37+
{
38+
$this->assertClassExtends(GenericDriver::class, AmqpDriver::class);
3439
}
3540

3641
public function testThrowIfPriorityIsNotSupportedOnCreateTransportMessage()

0 commit comments

Comments
 (0)