Skip to content

Commit

Permalink
Forced transport (#33)
Browse files Browse the repository at this point in the history
* Allow to define forced transport

* Fix replaceArgument

---------

Co-authored-by: Ronald Marfoldi <[email protected]>
  • Loading branch information
marforon and Ronald Marfoldi authored May 23, 2024
1 parent ff26113 commit c4433e0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion DependencyInjection/PetitPressGpsMessengerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}
1 change: 1 addition & 0 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
->set(GpsTransportFactory::class)
->args([
new ReferenceConfigurator(GpsConfigurationResolverInterface::class),
null,
null
])
->tag('messenger.transport_factory')
Expand Down
3 changes: 2 additions & 1 deletion Tests/Transport/GpsTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
12 changes: 10 additions & 2 deletions Transport/GpsTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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),
Expand Down

0 comments on commit c4433e0

Please sign in to comment.