44
55namespace Enqueue \Gps ;
66
7+ use Enqueue \Dsn \Dsn ;
78use Google \Cloud \PubSub \PubSubClient ;
89use Interop \Queue \PsrConnectionFactory ;
910use Interop \Queue \PsrContext ;
@@ -15,6 +16,11 @@ class GpsConnectionFactory implements PsrConnectionFactory
1516 */
1617 private $ config ;
1718
19+ /**
20+ * @var PubSubClient
21+ */
22+ private $ client ;
23+
1824 /**
1925 * @see https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
2026 * @see \Google\Cloud\PubSub\PubSubClient::__construct()
@@ -24,6 +30,7 @@ class GpsConnectionFactory implements PsrConnectionFactory
2430 * 'keyFilePath' => The full path to your service account credentials.json file retrieved from the Google Developers Console.
2531 * 'retries' => Number of retries for a failed request. **Defaults to** `3`.
2632 * 'scopes' => Scopes to be used for the request.
33+ * 'emulatorHost' => The endpoint used to emulate communication with GooglePubSub.
2734 * 'lazy' => 'the connection will be performed as later as possible, if the option set to true'
2835 * ]
2936 *
@@ -32,17 +39,31 @@ class GpsConnectionFactory implements PsrConnectionFactory
3239 * gps:
3340 * gps:?projectId=projectName
3441 *
35- * @param array|string|null $config
42+ * or instance of Google\Cloud\PubSub\PubSubClient
43+ *
44+ * @param array|string|PubSubClient|null $config
3645 */
3746 public function __construct ($ config = 'gps: ' )
3847 {
39- if (empty ($ config ) || 'gps: ' === $ config ) {
48+ if ($ config instanceof PubSubClient) {
49+ $ this ->client = $ config ;
50+ $ this ->config = ['lazy ' => false ] + $ this ->defaultConfig ();
51+
52+ return ;
53+ }
54+
55+ if (empty ($ config )) {
4056 $ config = [];
4157 } elseif (is_string ($ config )) {
4258 $ config = $ this ->parseDsn ($ config );
4359 } elseif (is_array ($ config )) {
60+ if (array_key_exists ('dsn ' , $ config )) {
61+ $ config = array_replace_recursive ($ config , $ this ->parseDsn ($ config ['dsn ' ]));
62+
63+ unset($ config ['dsn ' ]);
64+ }
4465 } else {
45- throw new \LogicException ('The config must be either an array of options, a DSN string or null ' );
66+ throw new \LogicException (sprintf ( 'The config must be either an array of options, a DSN string, null or instance of %s ' , PubSubClient::class) );
4667 }
4768
4869 $ this ->config = array_replace ($ this ->defaultConfig (), $ config );
@@ -64,22 +85,36 @@ public function createContext(): PsrContext
6485
6586 private function parseDsn (string $ dsn ): array
6687 {
67- if (false === strpos ($ dsn , 'gps: ' )) {
68- throw new \LogicException (sprintf ('The given DSN "%s" is not supported. Must start with "gps:". ' , $ dsn ));
69- }
70-
71- $ config = [];
88+ $ dsn = new Dsn ($ dsn );
7289
73- if ($ query = parse_url ($ dsn , PHP_URL_QUERY )) {
74- parse_str ($ query , $ config );
90+ if ('gps ' !== $ dsn ->getSchemeProtocol ()) {
91+ throw new \LogicException (sprintf (
92+ 'The given scheme protocol "%s" is not supported. It must be "gps" ' ,
93+ $ dsn ->getSchemeProtocol ()
94+ ));
7595 }
7696
77- return $ config ;
97+ $ emulatorHost = $ dsn ->getQueryParameter ('emulatorHost ' );
98+ $ hasEmulator = $ emulatorHost ? true : null ;
99+
100+ return array_filter (array_replace ($ dsn ->getQuery (), [
101+ 'projectId ' => $ dsn ->getQueryParameter ('projectId ' ),
102+ 'keyFilePath ' => $ dsn ->getQueryParameter ('keyFilePath ' ),
103+ 'retries ' => $ dsn ->getInt ('retries ' ),
104+ 'scopes ' => $ dsn ->getQueryParameter ('scopes ' ),
105+ 'emulatorHost ' => $ emulatorHost ,
106+ 'hasEmulator ' => $ hasEmulator ,
107+ 'lazy ' => $ dsn ->getBool ('lazy ' ),
108+ ]), function ($ value ) { return null !== $ value ; });
78109 }
79110
80111 private function establishConnection (): PubSubClient
81112 {
82- return new PubSubClient ($ this ->config );
113+ if (false == $ this ->client ) {
114+ $ this ->client = new PubSubClient ($ this ->config );
115+ }
116+
117+ return $ this ->client ;
83118 }
84119
85120 private function defaultConfig (): array
0 commit comments