Repository navigation
Expand file tree
/
Copy pathSqsConnectionFactory.php
More file actions
99 lines (85 loc) · 2.75 KB
/
Copy pathSqsConnectionFactory.php
File metadata and controls
99 lines (85 loc) · 2.75 KB
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
<?php
namespace Enqueue\Sqs;
use Aws\Sqs\SqsClient;
use Enqueue\Psr\PsrConnectionFactory;
class SqsConnectionFactory implements PsrConnectionFactory
{
/**
* @var array
*/
private $config;
/**
* @var SqsClient
*/
private $client;
/**
* $config = [
* 'key' => null - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'secret' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'token' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
* 'region' => null, - (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.
* 'retries' => 3, - (int, default=int(3)) Configures the maximum number of allowed retries for a client (pass 0 to disable retries).
* 'version' => '2012-11-05', - (string, required) The version of the webservice to utilize
* 'lazy' => true, - Enable lazy connection (boolean)
* ].
*
* @param $config
*/
public function __construct(array $config = [])
{
$this->config = array_replace([
'key' => null,
'secret' => null,
'token' => null,
'region' => null,
'retries' => 3,
'version' => '2012-11-05',
'lazy' => true,
], $config);
}
/**
* {@inheritdoc}
*
* @return SqsContext
*/
public function createContext()
{
if ($this->config['lazy']) {
return new SqsContext(function () {
return $this->establishConnection();
});
}
return new SqsContext($this->establishConnection());
}
/**
* {@inheritdoc}
*/
public function close()
{
}
/**
* @return SqsClient
*/
private function establishConnection()
{
if ($this->client) {
return $this->client;
}
$config = [
'version' => $this->config['version'],
'retries' => $this->config['retries'],
'region' => $this->config['region'],
];
if ($this->config['key'] && $this->config['secret']) {
$config['credentials'] = [
'key' => $this->config['key'],
'secret' => $this->config['secret'],
];
if ($this->config['token']) {
$config['credentials']['token'] = $this->config['token'];
}
}
$this->client = new SqsClient($config);
return $this->client;
}
}