2023-05-12 15:06:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Messenger\Bridge\Redis\Transport;
|
|
|
|
|
|
|
|
use Symfony\Component\Messenger\Envelope;
|
|
|
|
use Symfony\Component\Messenger\Exception\LogicException;
|
|
|
|
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
|
2024-05-17 20:26:01 +02:00
|
|
|
use Symfony\Component\Messenger\Exception\TransportException;
|
2023-05-12 15:06:01 +02:00
|
|
|
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
|
|
|
|
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
|
|
|
|
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Alexander Schranz <alexander@sulu.io>
|
|
|
|
* @author Antoine Bluchet <soyuka@gmail.com>
|
|
|
|
*/
|
|
|
|
class RedisReceiver implements ReceiverInterface
|
|
|
|
{
|
|
|
|
private $connection;
|
|
|
|
private $serializer;
|
|
|
|
|
2024-05-17 20:26:01 +02:00
|
|
|
public function __construct(Connection $connection, ?SerializerInterface $serializer = null)
|
2023-05-12 15:06:01 +02:00
|
|
|
{
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->serializer = $serializer ?? new PhpSerializer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function get(): iterable
|
|
|
|
{
|
|
|
|
$message = $this->connection->get();
|
|
|
|
|
|
|
|
if (null === $message) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-17 20:26:01 +02:00
|
|
|
if (null === $message['data']) {
|
|
|
|
try {
|
|
|
|
$this->connection->reject($message['id']);
|
|
|
|
} catch (TransportException $e) {
|
|
|
|
if ($e->getPrevious()) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->get();
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:06:01 +02:00
|
|
|
$redisEnvelope = json_decode($message['data']['message'] ?? '', true);
|
|
|
|
|
|
|
|
if (null === $redisEnvelope) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (\array_key_exists('body', $redisEnvelope) && \array_key_exists('headers', $redisEnvelope)) {
|
|
|
|
$envelope = $this->serializer->decode([
|
|
|
|
'body' => $redisEnvelope['body'],
|
|
|
|
'headers' => $redisEnvelope['headers'],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$envelope = $this->serializer->decode($redisEnvelope);
|
|
|
|
}
|
|
|
|
} catch (MessageDecodingFailedException $exception) {
|
|
|
|
$this->connection->reject($message['id']);
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$envelope->with(new RedisReceivedStamp($message['id']))];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function ack(Envelope $envelope): void
|
|
|
|
{
|
|
|
|
$this->connection->ack($this->findRedisReceivedStamp($envelope)->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function reject(Envelope $envelope): void
|
|
|
|
{
|
|
|
|
$this->connection->reject($this->findRedisReceivedStamp($envelope)->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findRedisReceivedStamp(Envelope $envelope): RedisReceivedStamp
|
|
|
|
{
|
|
|
|
/** @var RedisReceivedStamp|null $redisReceivedStamp */
|
|
|
|
$redisReceivedStamp = $envelope->last(RedisReceivedStamp::class);
|
|
|
|
|
|
|
|
if (null === $redisReceivedStamp) {
|
|
|
|
throw new LogicException('No RedisReceivedStamp found on the Envelope.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $redisReceivedStamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists(\Symfony\Component\Messenger\Transport\RedisExt\RedisReceiver::class, false)) {
|
|
|
|
class_alias(RedisReceiver::class, \Symfony\Component\Messenger\Transport\RedisExt\RedisReceiver::class);
|
|
|
|
}
|