Passed
Pull Request — master (#12)
by frey
02:26
created

CMQQueue::getPlainJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Freyo\LaravelQueueCMQ\Queue;
4
5
use Freyo\LaravelQueueCMQ\Queue\Driver\Account;
6
use Freyo\LaravelQueueCMQ\Queue\Driver\CMQServerException;
7
use Freyo\LaravelQueueCMQ\Queue\Driver\Message;
8
use Freyo\LaravelQueueCMQ\Queue\Driver\Topic;
9
use Freyo\LaravelQueueCMQ\Queue\Jobs\CMQJob;
10
use Illuminate\Contracts\Queue\Queue as QueueContract;
11
use Illuminate\Queue\Queue;
12
use Illuminate\Support\Arr;
13
14
class CMQQueue extends Queue implements QueueContract
15
{
16
    const CMQ_QUEUE_NO_MESSAGE_CODE = 7000;
17
18
    const CMQ_TOPIC_TAG_FILTER_NAME = 'msgtag';
19
    const CMQ_TOPIC_ROUTING_FILTER_NAME = 'routing';
20
21
    /**
22
     * @var array
23
     */
24
    protected $queueOptions;
25
    protected $topicOptions;
26
27
    /**
28
     * @var Account
29
     */
30
    private $queueAccount;
31
    private $topicAccount;
32
33
    /**
34
     * @var array
35
     */
36
    protected $plainOptions;
37
38
    public function __construct(Account $queueAccount, Account $topicAccount, array $config)
39
    {
40
        $this->queueAccount = $queueAccount;
41
        $this->topicAccount = $topicAccount;
42
43
        $this->queueOptions = $config['options']['queue'];
44
        $this->topicOptions = $config['options']['topic'];
45
46
        $this->plainOptions = Arr::get($config, 'plain', []);
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    public function isPlain()
53
    {
54
        return Arr::get($this->plainOptions, 'enable', false);
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getPlainJob()
61
    {
62
        return Arr::get($this->plainOptions, 'job');
63
    }
64
65
    /**
66
     * Get the size of the queue.
67
     *
68
     * @param string $queue
69
     *
70
     * @return int
71
     */
72
    public function size($queue = null)
73
    {
74
        $attributes = $this->getQueue($queue)->get_attributes();
75
76
        return (int) $attributes->activeMsgNum;
77
    }
78
79
    /**
80
     * Push a new job onto the queue.
81
     *
82
     * @param string|object $job
83
     * @param mixed         $data
84
     * @param string        $queue
85
     *
86
     * @return mixed
87
     */
88
    public function push($job, $data = '', $queue = null)
89
    {
90
        $payload = $this->isPlain() ? $job->getPayload() : $this->createPayload($job, $data);
0 ignored issues
show
Bug introduced by
It seems like $job can also be of type object; however, parameter $job of Illuminate\Queue\Queue::createPayload() does only seem to accept string, 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

90
        $payload = $this->isPlain() ? $job->getPayload() : $this->createPayload(/** @scrutinizer ignore-type */ $job, $data);
Loading history...
91
92
        return $this->pushRaw($payload, $queue);
93
    }
94
95
    /**
96
     * Push a raw payload onto the queue.
97
     *
98
     * @param string $payload
99
     * @param string $queue
100
     * @param array  $options
101
     *
102
     * @return mixed
103
     */
104
    public function pushRaw($payload, $queue = null, array $options = [])
105
    {
106
        $message = new Message($payload);
107
108
        $driver = $this->parseQueue($queue);
109
110
        if ($driver instanceof Topic) {
111
            $vTagList = [];
112
            if ($this->topicOptions['filter'] === self::CMQ_TOPIC_TAG_FILTER_NAME) {
113
                $vTagList = explode(',', $queue);
114
            }
115
116
            $routingKey = null;
117
            if ($this->topicOptions['filter'] === self::CMQ_TOPIC_ROUTING_FILTER_NAME) {
118
                $routingKey = $queue;
119
            }
120
121
            return $driver->publish_message($message->msgBody, $vTagList, $routingKey);
122
        }
123
124
        return $driver->send_message($message, array_get($options, 'delay', 0));
125
    }
126
127
    /**
128
     * Push a new job onto the queue after a delay.
129
     *
130
     * @param \DateTimeInterface|\DateInterval|int $delay
131
     * @param string|object                        $job
132
     * @param mixed                                $data
133
     * @param string                               $queue
134
     *
135
     * @return mixed
136
     */
137
    public function later($delay, $job, $data = '', $queue = null)
138
    {
139
        return $this->pushRaw($this->createPayload($job, $data), $queue, ['delay' => $this->secondsUntil($delay)]);
0 ignored issues
show
Bug introduced by
It seems like $job can also be of type object; however, parameter $job of Illuminate\Queue\Queue::createPayload() does only seem to accept string, 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

139
        return $this->pushRaw($this->createPayload(/** @scrutinizer ignore-type */ $job, $data), $queue, ['delay' => $this->secondsUntil($delay)]);
Loading history...
140
    }
141
142
    /**
143
     * Pop the next job off of the queue.
144
     *
145
     * @param string $queue
146
     *
147
     * @return \Illuminate\Contracts\Queue\Job|null
148
     */
149
    public function pop($queue = null)
150
    {
151
        try {
152
            $queue = $this->getQueue($queue);
153
            $message = $queue->receive_message($this->queueOptions['polling_wait_seconds']);
154
        } catch (CMQServerException $e) {
155
            if ($e->getCode() == self::CMQ_QUEUE_NO_MESSAGE_CODE) { //ignore no message
156
                return;
157
            }
158
159
            throw $e;
160
        }
161
162
        return new CMQJob($this->container, $this, $message, $queue);
163
    }
164
165
    /**
166
     * Get the queue.
167
     *
168
     * @param string $queue
169
     *
170
     * @return Driver\Queue
171
     */
172
    public function getQueue($queue = null)
173
    {
174
        return $this->queueAccount->get_queue($queue ?: $this->queueOptions['name']);
175
    }
176
177
    /**
178
     * Get the topic.
179
     *
180
     * @param string $topic
181
     *
182
     * @return Driver\Topic
183
     */
184
    public function getTopic($topic = null)
185
    {
186
        return $this->topicAccount->get_topic($topic ?: $this->topicOptions['name']);
187
    }
188
189
    /**
190
     * Parse name to topic or queue.
191
     *
192
     * @param string $queue
193
     *
194
     * @return Driver\Queue|Driver\Topic
195
     */
196
    public function parseQueue($queue = null)
197
    {
198
        if ($this->topicOptions['enable']) {
199
            $exchangeName = $this->topicOptions['name'] ?: $queue;
200
201
            return $this->getTopic($exchangeName);
202
        }
203
204
        $queueName = $queue ?: $this->queueOptions['name'];
205
206
        return $this->getQueue($queueName);
207
    }
208
}
209