Completed
Push — master ( e28c79...14b414 )
by Avtandil
02:36
created

SqsFifoQueue::getMessageGroupId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 6
rs 9.6666
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\Queue\SqsFifo;
13
14
use Aws\Sqs\SqsClient;
15
use BadMethodCallException;
16
use Illuminate\Queue\Jobs\SqsJob;
17
use Illuminate\Queue\SqsQueue;
18
use Illuminate\Support\Arr;
19
20
class SqsFifoQueue extends SqsQueue
21
{
22
    /**
23
     * @var array
24
     */
25
    private $options;
26
27
    /**
28
     * Create a new Amazon SQS queue instance.
29
     *
30
     * @param  \Aws\Sqs\SqsClient $sqs
31
     * @param  string $default
32
     * @param  string $prefix
33
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35
    public function __construct(SqsClient $sqs, $default, $prefix = '', array $options = [])
36
    {
37
        $this->sqs = $sqs;
38
        $this->prefix = $prefix;
39
        $this->default = $default;
40
        $this->options = $options;
41
42
        if (Arr::get($this->options, 'polling') === 'long') {
43
            $this->sqs->setQueueAttributes([
44
                'Attributes' => [
45
                    'ReceiveMessageWaitTimeSeconds' => Arr::get($this->options, 'wait_time', 20),
46
                ],
47
                'QueueUrl'   => $this->getQueue($default),
48
            ]);
49
        }
50
    }
51
52
    /**
53
     * Push a raw payload onto the queue.
54
     *
55
     * @see http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/general-recommendations.html
56
     * @see http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queue-recommendations.html
57
     *
58
     * @param  string $payload
59
     * @param  string $queue
60
     * @param  array $options
61
     * @return mixed
62
     */
63
    public function pushRaw($payload, $queue = null, array $options = [])
64
    {
65
        $messageGroupId = $this->getMessageGroupId();
66
        $messageDeduplicationId = $this->getMessageDeduplicationId($payload);
67
68
        $messageId = $this->sqs->sendMessage([
69
            'QueueUrl'               => $this->getQueue($queue),
70
            'MessageBody'            => $payload,
71
            'MessageGroupId'         => $messageGroupId,
72
            'MessageDeduplicationId' => $messageDeduplicationId,
73
        ])->get('MessageId');
74
75
        return $messageId;
76
    }
77
78
    protected function getMessageGroupId(): string
79
    {
80
        $messageGroupId = session()->getId();
81
        if (empty($messageGroupId)) {
82
            $messageGroupId = str_random(40);
83
        }
84
85
        return $messageGroupId;
86
    }
87
88
    protected function getMessageDeduplicationId(string $payload): string
89
    {
90
        return config('app.debug') ? str_random(40) : sha1($payload);
91
    }
92
93
    /**
94
     * FIFO queues don't support per-message delays, only per-queue delays
95
     *
96
     * @see http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html
97
     */
98
    public function later($delay, $job, $data = '', $queue = null)
99
    {
100
        throw new BadMethodCallException('FIFO queues don\'t support per-message delays, only per-queue delays');
101
    }
102
103
    /**
104
     * Pop the next job off of the queue.
105
     *
106
     * @param  string $queue
107
     * @return \Illuminate\Contracts\Queue\Job|null
108
     */
109
    public function pop($queue = null)
110
    {
111
        $response = $this->sqs->receiveMessage([
112
            'QueueUrl'            => $queue = $this->getQueue($queue),
113
            'AttributeNames'      => ['ApproximateReceiveCount'],
114
            'MaxNumberOfMessages' => 1,
115
            'WaitTimeSeconds'     => Arr::get($this->options, 'wait_time', 20),
116
        ]);
117
118
        if (count($response['Messages']) > 0) {
119
            return new SqsJob(
120
                $this->container,
121
                $this->sqs,
122
                $response['Messages'][0],
123
                $this->connectionName,
124
                $queue
125
            );
126
        }
127
    }
128
}
129