-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGpsConnectionFactory.php
123 lines (104 loc) · 3.67 KB
/
GpsConnectionFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Enqueue\Gps;
use Enqueue\Dsn\Dsn;
use Google\Cloud\PubSub\PubSubClient;
use Interop\Queue\ConnectionFactory;
use Interop\Queue\Context;
class GpsConnectionFactory implements ConnectionFactory
{
/**
* @var array
*/
private $config;
/**
* @var PubSubClient
*/
private $client;
/**
* @see https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
* @see \Google\Cloud\PubSub\PubSubClient::__construct()
*
* [
* 'projectId' => The project ID from the Google Developer's Console.
* 'keyFilePath' => The full path to your service account credentials.json file retrieved from the Google Developers Console.
* 'retries' => Number of retries for a failed request. **Defaults to** `3`.
* 'scopes' => Scopes to be used for the request.
* 'emulatorHost' => The endpoint used to emulate communication with GooglePubSub.
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true'
* ]
*
* or
*
* gps:
* gps:?projectId=projectName
*
* or instance of Google\Cloud\PubSub\PubSubClient
*
* @param array|string|PubSubClient|null $config
*/
public function __construct($config = 'gps:')
{
if ($config instanceof PubSubClient) {
$this->client = $config;
$this->config = ['lazy' => false] + $this->defaultConfig();
return;
}
if (empty($config)) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
if (array_key_exists('dsn', $config)) {
$config = array_replace_recursive($config, $this->parseDsn($config['dsn']));
unset($config['dsn']);
}
} else {
throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', PubSubClient::class));
}
$this->config = array_replace($this->defaultConfig(), $config);
}
/**
* @return GpsContext
*/
public function createContext(): Context
{
if ($this->config['lazy']) {
return new GpsContext(function () {
return $this->establishConnection();
});
}
return new GpsContext($this->establishConnection());
}
private function parseDsn(string $dsn): array
{
$dsn = Dsn::parseFirst($dsn);
if ('gps' !== $dsn->getSchemeProtocol()) {
throw new \LogicException(sprintf('The given scheme protocol "%s" is not supported. It must be "gps"', $dsn->getSchemeProtocol()));
}
$emulatorHost = $dsn->getString('emulatorHost');
$hasEmulator = $emulatorHost ? true : null;
return array_filter(array_replace($dsn->getQuery(), [
'projectId' => $dsn->getString('projectId'),
'keyFilePath' => $dsn->getString('keyFilePath'),
'retries' => $dsn->getDecimal('retries'),
'scopes' => $dsn->getString('scopes'),
'emulatorHost' => $emulatorHost,
'hasEmulator' => $hasEmulator,
'lazy' => $dsn->getBool('lazy'),
]), function ($value) { return null !== $value; });
}
private function establishConnection(): PubSubClient
{
if (false == $this->client) {
$this->client = new PubSubClient($this->config);
}
return $this->client;
}
private function defaultConfig(): array
{
return [
'lazy' => true,
];
}
}