Completed
Push — master ( a4f238...60fead )
by frey
03:46
created

CMQQueue::push()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

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