SqsQueueStoreAdapter::enqueue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Da\Mailer\Queue\Backend\Sqs;
4
5
use Da\Mailer\Exception\InvalidCallException;
6
use Da\Mailer\Queue\Backend\MailJobInterface;
7
use Da\Mailer\Queue\Backend\QueueStoreAdapterInterface;
8
9
class SqsQueueStoreAdapter implements QueueStoreAdapterInterface
10
{
11
    /**
12
     * @var string the name of the queue to store the messages
13
     */
14
    private $queueName;
15
/**
16
     * @var string the URL of the queue to store the messages
17
     */
18
    private $queueUrl;
19
/**
20
     * @var SqsQueueStoreConnection
21
     */
22
    protected $connection;
23
/**
24
     * PdoQueueStoreAdapter constructor.
25
     *
26
     * @param SqsQueueStoreConnection $connection
27
     * @param string $queueName the name of the queue in the SQS where the mail jobs are stored
28
     */
29 5
    public function __construct(SqsQueueStoreConnection $connection, $queueName = 'mail_queue')
30
    {
31 5
        $this->connection = $connection;
32 5
        $this->queueName = $queueName;
33 5
        $this->init();
34 5
    }
35
36
    /**
37
     * @return SqsQueueStoreAdapter
38
     */
39 5
    public function init()
40
    {
41 5
        $this->getConnection()->connect();
42
// create new queue or get existing one
43
        $queue = $this->getConnection()->getInstance()->createQueue([
44 5
            'QueueName' => $this->queueName,
45 5
        ]);
46 5
        $this->queueUrl = $queue['QueueUrl'];
47 5
        return $this;
48
    }
49 5
50
    /**
51
     * @return SqsQueueStoreConnection
52
     */
53
    public function getConnection()
54
    {
55 5
        return $this->connection;
56
    }
57 5
58
    /**
59
     * @param MailJobInterface|SqsMailJob $mailJob
60
     *
61
     * @return bool whether it has been successfully queued or not
62
     */
63
    public function enqueue(MailJobInterface $mailJob)
64
    {
65 3
        $result = $this->getConnection()->getInstance()->sendMessage([
66
            'QueueUrl' => $this->queueUrl,
67 3
            'MessageBody' => $mailJob->getMessage(),
68 3
            'DelaySeconds' => $mailJob->getDelaySeconds(),
0 ignored issues
show
Bug introduced by
The method getDelaySeconds() does not exist on Da\Mailer\Queue\Backend\MailJobInterface. It seems like you code against a sub-type of Da\Mailer\Queue\Backend\MailJobInterface such as Da\Mailer\Queue\Backend\Sqs\SqsMailJob. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
            'DelaySeconds' => $mailJob->/** @scrutinizer ignore-call */ getDelaySeconds(),
Loading history...
69 3
            'Attempt' => $mailJob->getAttempt(),
0 ignored issues
show
Bug introduced by
The method getAttempt() does not exist on Da\Mailer\Queue\Backend\MailJobInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Da\Mailer\Queue\Backend\MailJobInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            'Attempt' => $mailJob->/** @scrutinizer ignore-call */ getAttempt(),
Loading history...
70 3
        ]);
71 3
        $messageId = $result['MessageId'];
72 3
        return $messageId !== null && is_string($messageId);
73 3
    }
74
75 3
    /**
76
     * Returns a MailJob fetched from Amazon SQS.
77
     *
78
     * @return MailJobInterface|SqsMailJob
79
     */
80
    public function dequeue()
81
    {
82
        $result = $this->getConnection()->getInstance()->receiveMessage([
83 3
            'QueueUrl' => $this->queueUrl,
84
        ]);
85 3
        if (empty($result['Messages'])) {
86 3
            return null;
87 3
        }
88
89 3
        $result = array_shift($result['Messages']);
0 ignored issues
show
Bug introduced by
It seems like $result['Messages'] can also be of type null; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
        $result = array_shift(/** @scrutinizer ignore-type */ $result['Messages']);
Loading history...
90 3
        return new SqsMailJob([
91
            'id' => $result['MessageId'],
92
            'receiptHandle' => $result['ReceiptHandle'],
93 3
            'message' => $result['Body'],
94 3
            'attempt' => $result['Attempt'],
95 3
        ]);
96 3
    }
97 3
98 3
    /**
99
     * @param MailJobInterface|SqsMailJob $mailJob
100
     *
101
     * @return bool
102
     */
103
    public function ack(MailJobInterface $mailJob)
104
    {
105
        if ($mailJob->isNewRecord()) {
106 4
            throw new InvalidCallException('SqsMailJob cannot be a new object to be acknowledged');
107
        }
108 4
109 1
        if ($mailJob->getDeleted()) {
0 ignored issues
show
Bug introduced by
The method getDeleted() does not exist on Da\Mailer\Queue\Backend\MailJobInterface. It seems like you code against a sub-type of Da\Mailer\Queue\Backend\MailJobInterface such as Da\Mailer\Queue\Backend\Sqs\SqsMailJob. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        if ($mailJob->/** @scrutinizer ignore-call */ getDeleted()) {
Loading history...
110
            $this->getConnection()->getInstance()->deleteMessage([
111
                'QueueUrl' => $this->queueUrl,
112 3
                'ReceiptHandle' => $mailJob->getReceiptHandle(),
0 ignored issues
show
Bug introduced by
The method getReceiptHandle() does not exist on Da\Mailer\Queue\Backend\MailJobInterface. It seems like you code against a sub-type of Da\Mailer\Queue\Backend\MailJobInterface such as Da\Mailer\Queue\Backend\Sqs\SqsMailJob. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
                'ReceiptHandle' => $mailJob->/** @scrutinizer ignore-call */ getReceiptHandle(),
Loading history...
113 1
            ]);
114 1
            return true;
115 1
        } elseif ($mailJob->getVisibilityTimeout() !== null) {
0 ignored issues
show
Bug introduced by
The method getVisibilityTimeout() does not exist on Da\Mailer\Queue\Backend\MailJobInterface. It seems like you code against a sub-type of Da\Mailer\Queue\Backend\MailJobInterface such as Da\Mailer\Queue\Backend\Sqs\SqsMailJob. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        } elseif ($mailJob->/** @scrutinizer ignore-call */ getVisibilityTimeout() !== null) {
Loading history...
116 1
            $this->getConnection()->getInstance()->changeMessageVisibility([
117
                'QueueUrl' => $this->queueUrl,
118 1
                'ReceiptHandle' => $mailJob->getReceiptHandle(),
119 2
                'VisibilityTimeout' => $mailJob->getVisibilityTimeout(),
120 1
            ]);
121 1
            return true;
122 1
        }
123 1
124 1
        return false;
125
    }
126 1
127
    /**
128
     * {@inheritdoc}
129 1
     */
130
    public function isEmpty(): bool
131
    {
132
        $response = $this->getConnection()->getInstance()->getQueueAttributes([
133
            'QueueUrl' => $this->queueUrl,
134
            'AttributeNames' => ['ApproximateNumberOfMessages'],
135 3
        ]);
136
        return $response['Attributes']['ApproximateNumberOfMessages'] === 0;
137 3
    }
138
}
139