Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QueueInteropTransport & rename vars to avoid confusion #43

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions QueueInteropTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function receive(callable $handler): void

while (!$this->shouldStop) {
try {
if (null === ($message = $consumer->receive($this->options['receiveTimeout'] ?? 30000))) {
if (null === ($interopMessage = $consumer->receive($this->options['receiveTimeout'] ?? 30000))) {
$handler(null);
continue;
}
Expand All @@ -86,43 +86,42 @@ public function receive(callable $handler): void

try {
$handler($this->serializer->decode(array(
'body' => $message->getBody(),
'headers' => $message->getHeaders(),
'properties' => $message->getProperties(),
'body' => $interopMessage->getBody(),
'headers' => $interopMessage->getHeaders(),
'properties' => $interopMessage->getProperties(),
)));

$consumer->acknowledge($message);
$consumer->acknowledge($interopMessage);
} catch (RejectMessageException $e) {
$consumer->reject($message);
$consumer->reject($interopMessage);
} catch (RequeueMessageException $e) {
$consumer->reject($message, true);
$consumer->reject($interopMessage, true);
}
}
}

/**
* {@inheritdoc}
*/
public function send(Envelope $message): Envelope
public function send(Envelope $envelope): Envelope
{
$context = $this->contextManager->context();
$destination = $this->getDestination($message);
$destination = $this->getDestination($envelope);
$topic = $context->createTopic($destination['topic']);

if ($this->debug) {
$this->contextManager->ensureExists($destination);
}

$encodedMessage = $this->serializer->encode($message);
$encodedMessage = $this->serializer->encode($envelope);

$originalMessage = $message;
$message = $context->createMessage(
$interopMessage = $context->createMessage(
$encodedMessage['body'],
$encodedMessage['properties'] ?? array(),
$encodedMessage['headers'] ?? array()
);

$this->setMessageMetadata($message, $originalMessage);
$this->setMessageMetadata($interopMessage, $envelope);

$producer = $context->createProducer();

Expand All @@ -140,17 +139,17 @@ public function send(Envelope $message): Envelope
}

try {
$producer->send($topic, $message);
$producer->send($topic, $interopMessage);
} catch (InteropQueueException $e) {
if ($this->contextManager->recoverException($e, $destination)) {
// The context manager recovered the exception, we re-try.
$this->send($message);
$envelope = $this->send($envelope);
}

throw new SendingMessageFailedException($e->getMessage(), null, $e);
}

return new Envelope($message);
return $envelope;
}

/**
Expand Down Expand Up @@ -191,9 +190,9 @@ public function configureOptions(OptionsResolver $resolver): void
});
}

private function getDestination(?Envelope $message): array
private function getDestination(?Envelope $envelope): array
{
$configuration = $message ? $message->last(TransportConfiguration::class) : null;
$configuration = $envelope ? $envelope->last(TransportConfiguration::class) : null;
$topic = null !== $configuration ? $configuration->getTopic() : null;

return array(
Expand All @@ -204,23 +203,23 @@ private function getDestination(?Envelope $message): array
);
}

private function setMessageMetadata(Message $message, Envelope $originalMessage): void
private function setMessageMetadata(Message $interopMessage, Envelope $envelope): void
{
$configuration = $originalMessage->last(TransportConfiguration::class);
$configuration = $envelope->last(TransportConfiguration::class);

if (null === $configuration) {
return;
}

$metadata = $configuration->getMetadata();
$class = new \ReflectionClass($message);
$class = new \ReflectionClass($interopMessage);

foreach ($metadata as $key => $value) {
$setter = sprintf('set%s', ucfirst($key));
if (!$class->hasMethod($setter)) {
throw new MissingMessageMetadataSetterException($key, $setter, $class->getName());
}
$message->{$setter}($value);
$interopMessage->{$setter}($value);
}
}
}
2 changes: 1 addition & 1 deletion Tests/QueueInteropTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testSendAndEnsuresTheInfrastructureExistsWithDebug()
true
);

$transport->send($envelope);
$this->assertSame($envelope, $transport->send($envelope));
}

public function testSendWithoutDebugWillNotVerifyTheInfrastructureForPerformanceReasons()
Expand Down