diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 13f565a..eb885e5 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -21,7 +21,13 @@ public function getConfigTreeBuilder(): TreeBuilder ->defaultValue('cache.app') ->info('A cache for storing access tokens.') ->end() - ->end(); + ->enumNode('forced_transport') + ->values(['grpc', 'rest']) + ->defaultNull() + ->info('A forced transport for all messenger transports.') + ->end() + ->end() + ; return $treeBuilder; } diff --git a/DependencyInjection/PetitPressGpsMessengerExtension.php b/DependencyInjection/PetitPressGpsMessengerExtension.php index 89e31a7..db9beb8 100644 --- a/DependencyInjection/PetitPressGpsMessengerExtension.php +++ b/DependencyInjection/PetitPressGpsMessengerExtension.php @@ -24,9 +24,12 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); + $gpsTransportFactoryDefinition = $container->getDefinition(GpsTransportFactory::class); if ($config['auth_cache']) { - $gpsTransportFactoryDefinition = $container->getDefinition(GpsTransportFactory::class); $gpsTransportFactoryDefinition->replaceArgument(1, new Reference($config['auth_cache'])); } + if (isset($config['forced_transport'])) { + $gpsTransportFactoryDefinition->replaceArgument(2, $config['forced_transport']); + } } } diff --git a/Resources/config/services.php b/Resources/config/services.php index ed46c73..8e5ae41 100644 --- a/Resources/config/services.php +++ b/Resources/config/services.php @@ -13,6 +13,7 @@ ->set(GpsTransportFactory::class) ->args([ new ReferenceConfigurator(GpsConfigurationResolverInterface::class), + null, null ]) ->tag('messenger.transport_factory') diff --git a/Tests/Transport/GpsTransportFactoryTest.php b/Tests/Transport/GpsTransportFactoryTest.php index ca706ee..7605971 100644 --- a/Tests/Transport/GpsTransportFactoryTest.php +++ b/Tests/Transport/GpsTransportFactoryTest.php @@ -17,7 +17,8 @@ protected function setUp(): void { $this->subject = new GpsTransportFactory( $this->createMock(GpsConfigurationResolverInterface::class), - $this->createMock(CacheItemPoolInterface::class) + $this->createMock(CacheItemPoolInterface::class), + null ); } diff --git a/Transport/GpsTransportFactory.php b/Transport/GpsTransportFactory.php index a342e73..8e6fb45 100644 --- a/Transport/GpsTransportFactory.php +++ b/Transport/GpsTransportFactory.php @@ -18,11 +18,16 @@ final class GpsTransportFactory implements TransportFactoryInterface { private GpsConfigurationResolverInterface $gpsConfigurationResolver; private ?CacheItemPoolInterface $cache; + private ?string $forcedTransport; - public function __construct(GpsConfigurationResolverInterface $gpsConfigurationResolver, ?CacheItemPoolInterface $cache) - { + public function __construct( + GpsConfigurationResolverInterface $gpsConfigurationResolver, + ?CacheItemPoolInterface $cache, + ?string $forcedTransport + ) { $this->gpsConfigurationResolver = $gpsConfigurationResolver; $this->cache = $cache; + $this->forcedTransport = $forcedTransport; } /** @@ -36,6 +41,9 @@ public function createTransport(string $dsn, array $options, SerializerInterface if ($this->cache instanceof CacheItemPoolInterface) { $clientConfig['authCache'] ??= $this->cache; } + if (isset($this->forcedTransport)) { + $clientConfig['transport'] = $this->forcedTransport; + } return new GpsTransport( new PubSubClient($clientConfig),