Completed
Push — master ( 60fead...da51a9 )
by frey
03:34
created

CMQQueue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
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 3
    public function isPlain()
53
    {
54 3
        return Arr::get($this->plainOptions, 'enable', false);
55
    }
56
57
    /**
58
     * @return string
59
     */
60 1
    public function getPlainJob()
61
    {
62 1
        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 1
    public function size($queue = null)
73
    {
74 1
        $attributes = $this->getQueue($queue)->get_attributes();
75
76 1
        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
     * @throws \ReflectionException
88
     */
89 1
    public function push($job, $data = '', $queue = null)
90 1
    {
91 1
        if ($this->isPlain()) {
92
            return $this->pushRaw($job->getPayload(), $queue);
93
        }
94
95 1
        $reflection = new \ReflectionMethod($this, 'createPayload');
96 1
        if ($reflection->getNumberOfParameters() === 3) { // version >= 5.7
97 1
            $payload = $this->createPayload($job, $queue, $data);
98 1
        } else {
99
            $payload = $this->createPayload($job, $data);
100
        }
101
102 1
        return $this->pushRaw($payload, $queue);
103
    }
104
105
    /**
106
     * Push a raw payload onto the queue.
107
     *
108
     * @param string $payload
109
     * @param string $queue
110
     * @param array $options
111
     *
112
     * @return \Freyo\LaravelQueueCMQ\Queue\Driver\Message|array
113
     * @throws \Freyo\LaravelQueueCMQ\Queue\Driver\CMQServerNetworkException
114
     * @throws \Freyo\LaravelQueueCMQ\Queue\Driver\CMQServerException
115
     */
116 3
    public function pushRaw($payload, $queue = null, array $options = [])
117
    {
118 3
        $message = new Message($payload);
119
120 3
        $driver = $this->parseQueue($queue);
121
122 3
        if ($driver instanceof Topic) {
123
            switch ($this->topicOptions['filter']) {
124
                case self::CMQ_TOPIC_TAG_FILTER_NAME:
125
                    return $driver->publish_message($message->msgBody, explode(',', $queue), null);
126
                case self::CMQ_TOPIC_ROUTING_FILTER_NAME:
127
                    return $driver->publish_message($message->msgBody, [], $queue);
128
                default:
129
                    throw new \InvalidArgumentException(
130
                        'Invalid CMQ topic filter: ' . $this->topicOptions['filter']
131
                    );
132
            }
133
        }
134
135 3
        return $driver->send_message($message, Arr::get($options, 'delay', 0));
136
    }
137
138
    /**
139
     * Push a new job onto the queue after a delay.
140
     *
141
     * @param \DateTimeInterface|\DateInterval|int $delay
142
     * @param string|object $job
143
     * @param mixed $data
144
     * @param string $queue
145
     *
146
     * @return mixed
147
     * @throws \ReflectionException
148
     */
149 1
    public function later($delay, $job, $data = '', $queue = null)
150
    {
151 1
        $delay = method_exists($this, 'getSeconds')
152 1
            ? $this->getSeconds($delay)
153 1
            : $this->secondsUntil($delay);
154
155 1
        if ($this->isPlain()) {
156
            return $this->pushRaw($job->getPayload(), $queue, ['delay' => $delay]);
157
        }
158
159 1
        $reflection = new \ReflectionMethod($this, 'createPayload');
160 1
        if ($reflection->getNumberOfParameters() === 3) { // version >= 5.7
161 1
            $payload = $this->createPayload($job, $queue, $data);
162 1
        } else {
163
            $payload = $this->createPayload($job, $data);
164
        }
165
166 1
        return $this->pushRaw($payload, $queue, ['delay' => $delay]);
167
    }
168
169
    /**
170
     * Pop the next job off of the queue.
171
     *
172
     * @param string $queue
173
     *
174
     * @return \Illuminate\Contracts\Queue\Job|null
175
     */
176 2
    public function pop($queue = null)
177
    {
178
        try {
179 1
            $queue = $this->getQueue($queue);
180 1
            $message = $queue->receive_message($this->queueOptions['polling_wait_seconds']);
181 1
        } catch (CMQServerException $e) {
182
            if ((int)$e->getCode() === self::CMQ_QUEUE_NO_MESSAGE_CODE) { //ignore no message
183
                return null;
184
            }
185
            throw $e;
186
        }
187
188 2
        return new CMQJob($this->container, $this, $message, $queue);
189
    }
190
191
    /**
192
     * Get the queue.
193
     *
194
     * @param string $queue
195
     *
196
     * @return Driver\Queue
197
     */
198 7
    public function getQueue($queue = null)
199
    {
200 7
        return $this->queueAccount->get_queue($queue ?: $this->queueOptions['name']);
201
    }
202
203
    /**
204
     * Get the topic.
205
     *
206
     * @param string $topic
207
     *
208
     * @return Driver\Topic
209
     */
210 1
    public function getTopic($topic = null)
211
    {
212 1
        return $this->topicAccount->get_topic($topic ?: $this->topicOptions['name']);
213
    }
214
215
    /**
216
     * Parse name to topic or queue.
217
     *
218
     * @param string $queue
219
     *
220
     * @return Driver\Queue|Driver\Topic
221
     */
222 4
    public function parseQueue($queue = null)
223
    {
224 4
        if ($this->topicOptions['enable']) {
225
            return $this->getTopic($this->topicOptions['name'] ?: $queue);
226
        }
227
228 4
        return $this->getQueue($queue ?: $this->queueOptions['name']);
229
    }
230
}
231