1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShiftOneLabs\LaravelSqsFifoQueue\Queue\Connectors; |
4
|
|
|
|
5
|
|
|
use Aws\Sqs\SqsClient; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Illuminate\Queue\Connectors\SqsConnector; |
8
|
|
|
use ShiftOneLabs\LaravelSqsFifoQueue\SqsFifoQueue; |
9
|
|
|
|
10
|
|
|
class SqsFifoConnector extends SqsConnector |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Establish a queue connection. |
14
|
|
|
* |
15
|
|
|
* @param array $config |
16
|
|
|
* |
17
|
|
|
* @return \Illuminate\Contracts\Queue\Queue |
18
|
|
|
*/ |
19
|
4 |
|
public function connect(array $config) |
20
|
|
|
{ |
21
|
4 |
|
$config = $this->getDefaultConfiguration($config); |
22
|
|
|
|
23
|
4 |
|
if (!ends_with($config['queue'], '.fifo')) { |
24
|
1 |
|
throw new InvalidArgumentException('FIFO queue name must end in ".fifo"'); |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
if (array_key_exists('key', $config) && array_key_exists('secret', $config)) { |
28
|
3 |
|
$config['credentials'] = array_only($config, ['key', 'secret']); |
29
|
3 |
|
} |
30
|
|
|
|
31
|
3 |
|
$group = array_pull($config, 'group', 'default'); |
32
|
3 |
|
$deduplicator = array_pull($config, 'deduplicator', 'unique'); |
33
|
|
|
|
34
|
3 |
|
return new SqsFifoQueue( |
35
|
3 |
|
new SqsClient($config), |
36
|
3 |
|
$config['queue'], |
37
|
3 |
|
array_get($config, 'prefix', ''), |
38
|
3 |
|
$group, |
39
|
|
|
$deduplicator |
40
|
3 |
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the default configuration for SQS. |
45
|
|
|
* |
46
|
|
|
* @param array $config |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
4 |
|
protected function getDefaultConfiguration(array $config) |
51
|
|
|
{ |
52
|
|
|
// Laravel >= 5.1 has the "getDefaultConfiguration" method. |
53
|
4 |
|
if (method_exists(get_parent_class(), 'getDefaultConfiguration')) { |
54
|
4 |
|
return parent::getDefaultConfiguration($config); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return array_merge([ |
58
|
|
|
'version' => 'latest', |
59
|
|
|
'http' => [ |
60
|
|
|
'timeout' => 60, |
61
|
|
|
'connect_timeout' => 60, |
62
|
|
|
], |
63
|
|
|
], $config); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|