From 3bf9a8406a30c791bc3f76fa6aab1818b4dcdb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ronald=20M=C3=A1rf=C3=B6ldi?= Date: Fri, 11 Feb 2022 09:32:15 +0100 Subject: [PATCH] Allow to define custom config for Google\Cloud\PubSub\PubSubClient (#11) Co-authored-by: Ronald Marfoldi --- README.md | 6 ++++-- Transport/GpsConfiguration.php | 17 ++++++++++++++--- Transport/GpsConfigurationInterface.php | 9 ++++++++- Transport/GpsConfigurationResolver.php | 5 ++++- Transport/GpsTransportFactory.php | 8 +++++--- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c8b211c..08555c5 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ framework: gps_transport: dsn: 'gps://default' options: + client_config: # optional (default: []) + apiEndpoint: 'https://europe-west3-pubsub.googleapis.com' max_messages_pull: 10 # optional (default: 10) topic: # optional (default name: messages) name: 'messages' @@ -64,10 +66,10 @@ framework: messenger: transports: gps_transport: - dsn: 'gps://default/messages?max_messages_pull=10' + dsn: 'gps://default/messages?client_config[apiEndpoint]=https://europe-west3-pubsub.googleapis.com&max_messages_pull=10' ``` ### Step 4: Use available stamps if needed * `OrderingKeyStamp`: use for keeping messages of the same context in order. - For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using_ordering_keys). \ No newline at end of file + For more information, read an [official documentation](https://cloud.google.com/pubsub/docs/publisher#using_ordering_keys). diff --git a/Transport/GpsConfiguration.php b/Transport/GpsConfiguration.php index f08a41a..fe4886f 100644 --- a/Transport/GpsConfiguration.php +++ b/Transport/GpsConfiguration.php @@ -12,12 +12,18 @@ final class GpsConfiguration implements GpsConfigurationInterface private string $queueName; private string $subscriptionName; private int $maxMessagesPull; + private array $clientConfig; - public function __construct(string $queueName, string $subscriptionName, int $maxMessagesPull) - { + public function __construct( + string $queueName, + string $subscriptionName, + int $maxMessagesPull, + array $clientConfig + ) { $this->queueName = $queueName; $this->subscriptionName = $subscriptionName; $this->maxMessagesPull = $maxMessagesPull; + $this->clientConfig = $clientConfig; } public function getQueueName(): string @@ -34,4 +40,9 @@ public function getMaxMessagesPull(): int { return $this->maxMessagesPull; } -} \ No newline at end of file + + public function getClientConfig(): array + { + return $this->clientConfig; + } +} diff --git a/Transport/GpsConfigurationInterface.php b/Transport/GpsConfigurationInterface.php index 1e7627e..4aa3d94 100644 --- a/Transport/GpsConfigurationInterface.php +++ b/Transport/GpsConfigurationInterface.php @@ -4,6 +4,8 @@ namespace PetitPress\GpsMessengerBundle\Transport; +use Google\Cloud\PubSub\PubSubClient; + /** * @author Ronald Marfoldi */ @@ -14,4 +16,9 @@ public function getQueueName(): string; public function getSubscriptionName(): string; public function getMaxMessagesPull(): int; -} \ No newline at end of file + + /** + * @see PubSubClient constructor options + */ + public function getClientConfig(): array; +} diff --git a/Transport/GpsConfigurationResolver.php b/Transport/GpsConfigurationResolver.php index a9f54f4..07344cf 100644 --- a/Transport/GpsConfigurationResolver.php +++ b/Transport/GpsConfigurationResolver.php @@ -22,6 +22,7 @@ public function resolve(string $dsn, array $options): GpsConfigurationInterface $optionsResolver = new OptionsResolver(); $optionsResolver + ->setDefault('client_config', []) ->setDefault('max_messages_pull', self::DEFAULT_MAX_MESSAGES_PULL) ->setDefault('topic', static function (OptionsResolver $topicResolver): void { $topicResolver @@ -39,6 +40,7 @@ public function resolve(string $dsn, array $options): GpsConfigurationInterface return ((int) filter_var($value, FILTER_SANITIZE_NUMBER_INT)) ?: null; }) ->setAllowedTypes('max_messages_pull', ['int', 'string']) + ->setAllowedTypes('client_config', 'array') ; $dnsOptions = []; @@ -60,6 +62,7 @@ public function resolve(string $dsn, array $options): GpsConfigurationInterface $resolvedOptions['topic']['name'], $resolvedOptions['queue']['name'], $resolvedOptions['max_messages_pull'], + $resolvedOptions['client_config'], ); } -} \ No newline at end of file +} diff --git a/Transport/GpsTransportFactory.php b/Transport/GpsTransportFactory.php index a54b95a..7e5cb36 100644 --- a/Transport/GpsTransportFactory.php +++ b/Transport/GpsTransportFactory.php @@ -26,9 +26,11 @@ public function __construct(GpsConfigurationResolverInterface $gpsConfigurationR */ public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface { + $options = $this->gpsConfigurationResolver->resolve($dsn, $options); + return new GpsTransport( - new PubSubClient(), - $this->gpsConfigurationResolver->resolve($dsn, $options), + new PubSubClient($options->getClientConfig()), + $options, $serializer ); } @@ -40,4 +42,4 @@ public function supports(string $dsn, array $options): bool { return str_starts_with($dsn, 'gps://'); } -} \ No newline at end of file +}