2021-04-18 22:24:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DigiComp\FlowSymfonyBridge\Messenger\Transport;
|
|
|
|
|
|
|
|
use Neos\Flow\Annotations as Flow;
|
|
|
|
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
|
|
|
|
use Symfony\Component\Messenger\Transport\TransportInterface;
|
|
|
|
|
2023-02-18 22:53:05 +01:00
|
|
|
#[Flow\Scope('singleton')]
|
2021-04-18 22:24:02 +02:00
|
|
|
class TransportsContainer implements ContainerInterface
|
|
|
|
{
|
2023-02-18 22:53:05 +01:00
|
|
|
#[Flow\InjectConfiguration]
|
2021-04-18 22:24:02 +02:00
|
|
|
protected array $configuration;
|
|
|
|
|
2023-02-18 22:53:05 +01:00
|
|
|
#[Flow\Inject(lazy: false)]
|
|
|
|
protected ObjectManagerInterface $objectManager;
|
2021-04-18 22:24:02 +02:00
|
|
|
|
2023-02-18 22:53:05 +01:00
|
|
|
#[Flow\Inject(name: 'DigiComp.FlowSymfonyBridge.Messenger:TransportFactory', lazy: false)]
|
|
|
|
protected TransportFactoryInterface $transportFactory;
|
2021-04-18 22:24:02 +02:00
|
|
|
|
2023-02-18 22:53:05 +01:00
|
|
|
#[Flow\Inject(lazy: false)]
|
|
|
|
protected FailureTransportContainer $failureTransports;
|
2023-01-05 15:33:20 +01:00
|
|
|
|
2021-04-18 22:24:02 +02:00
|
|
|
/**
|
|
|
|
* @var TransportInterface[]
|
|
|
|
*/
|
|
|
|
protected array $transports;
|
|
|
|
|
|
|
|
public function get(string $id)
|
|
|
|
{
|
|
|
|
if (! isset($this->configuration['transports'][$id])) {
|
|
|
|
throw new \InvalidArgumentException('Unknown transport name: ' . $id);
|
|
|
|
}
|
|
|
|
if (! isset($this->transports[$id])) {
|
2022-09-18 13:17:53 +02:00
|
|
|
$transportDefinition = \array_merge([
|
2021-04-18 22:24:02 +02:00
|
|
|
'dsn' => '',
|
|
|
|
'options' => [],
|
|
|
|
'serializer' => $this->configuration['defaultSerializerName'],
|
2022-09-18 13:17:53 +02:00
|
|
|
// TODO: Probably this has to be setup elsewhere, as the transport does not care by itself
|
|
|
|
'retry_strategy' => [ // TODO: Make the default configurable
|
2021-04-18 22:24:02 +02:00
|
|
|
'max_retries' => 3,
|
2022-09-18 13:17:53 +02:00
|
|
|
// milliseconds delay
|
2021-04-18 22:24:02 +02:00
|
|
|
'delay' => 1000,
|
2022-09-18 13:17:53 +02:00
|
|
|
// causes the delay to be higher before each retry
|
|
|
|
// e.g. 1 second delay, 2 seconds, 4 seconds
|
2021-04-18 22:24:02 +02:00
|
|
|
'multiplier' => 2,
|
|
|
|
'max_delay' => 0,
|
2022-09-18 13:17:53 +02:00
|
|
|
// override all of this with a service that
|
|
|
|
// implements Symfony\Component\Messenger\Retry\RetryStrategyInterface
|
2021-04-18 22:24:02 +02:00
|
|
|
'service' => null
|
2022-09-18 13:17:53 +02:00
|
|
|
],
|
2021-04-18 22:24:02 +02:00
|
|
|
], $this->configuration['transports'][$id]);
|
|
|
|
$this->transports[$id] = $this->transportFactory->createTransport(
|
|
|
|
$transportDefinition['dsn'],
|
|
|
|
$transportDefinition['options'],
|
|
|
|
$this->objectManager->get($transportDefinition['serializer'])
|
|
|
|
);
|
2023-01-05 15:33:20 +01:00
|
|
|
if (isset($transportDefinition['failureTransport'])) {
|
|
|
|
$this->failureTransports->set($id, $this->get($transportDefinition['failureTransport']));
|
|
|
|
} elseif (isset($this->configuration['failureTransport'])) {
|
|
|
|
$this->failureTransports->set($id, $this->get($this->configuration['failureTransport']));
|
|
|
|
}
|
2021-04-18 22:24:02 +02:00
|
|
|
}
|
|
|
|
return $this->transports[$id];
|
|
|
|
}
|
|
|
|
|
2023-02-18 22:53:05 +01:00
|
|
|
public function has(string $id): bool
|
2021-04-18 22:24:02 +02:00
|
|
|
{
|
|
|
|
return isset($this->configuration['transports'][$id]);
|
|
|
|
}
|
|
|
|
}
|