DigiComp.FlowSymfonyBridge..../Classes/Transport/TransportsContainer.php

88 lines
2.9 KiB
PHP
Raw Normal View History

<?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;
/**
* @Flow\Scope("singleton")
*/
class TransportsContainer implements ContainerInterface
{
/**
* @Flow\InjectConfiguration
* @var array
*/
protected array $configuration;
/**
* @Flow\Inject
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @Flow\Inject(name="DigiComp.FlowSymfonyBridge.Messenger:TransportFactory")
* @var TransportFactoryInterface
*/
protected $transportFactory;
/**
* @Flow\Inject
* @var FailureTransportContainer
*/
protected $failureTransports;
/**
* @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([
'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
'max_retries' => 3,
2022-09-18 13:17:53 +02:00
// milliseconds delay
'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
'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
'service' => null
2022-09-18 13:17:53 +02:00
],
], $this->configuration['transports'][$id]);
$this->transports[$id] = $this->transportFactory->createTransport(
$transportDefinition['dsn'],
$transportDefinition['options'],
$this->objectManager->get($transportDefinition['serializer'])
);
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']));
}
}
return $this->transports[$id];
}
public function has(string $id)
{
return isset($this->configuration['transports'][$id]);
}
}