Completed
Push — master ( e8bd82...cb1f3e )
by Patrick
17:05
created

SqsFifoConnector::connect()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 1
dl 0
loc 21
ccs 15
cts 15
cp 1
crap 4
rs 9.0534
c 0
b 0
f 0
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 266
    public function connect(array $config)
20
    {
21 266
        $config = $this->getDefaultConfiguration($config);
22
23 266
        if (!ends_with($config['queue'], '.fifo')) {
24 38
            throw new InvalidArgumentException('FIFO queue name must end in ".fifo"');
25
        }
26
27 228
        if (!empty($config['key']) && !empty($config['secret'])) {
28 190
            $config['credentials'] = array_only($config, ['key', 'secret']);
29 60
        }
30
31 228
        $group = array_pull($config, 'group', 'default');
32 228
        $deduplicator = array_pull($config, 'deduplicator', 'unique');
33
34 228
        return new SqsFifoQueue(
35 228
            new SqsClient($config),
36 228
            $config['queue'],
37 228
            array_get($config, 'prefix', ''),
38 228
            $group,
39 156
            $deduplicator
40 72
        );
41
    }
42
43
    /**
44
     * Get the default configuration for SQS.
45
     *
46
     * @param  array  $config
47
     *
48
     * @return array
49
     */
50 266
    protected function getDefaultConfiguration(array $config)
51
    {
52
        // Laravel >= 5.1 has the "getDefaultConfiguration" method.
53 266
        if (method_exists(get_parent_class(), 'getDefaultConfiguration')) {
54 161
            return parent::getDefaultConfiguration($config);
55
        }
56
57 105
        return array_merge([
58 105
            'version' => 'latest',
59
            'http' => [
60 42
                'timeout' => 60,
61 42
                'connect_timeout' => 60,
62 42
            ],
63 105
        ], $config);
64
    }
65
}
66