Completed
Branch master (9dff9d)
by Albert
05:30
created

SwooleTaskQueue::pop()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Task;
4
5
use Exception;
6
use Swoole\Timer;
7
use Illuminate\Queue\Queue;
8
use Illuminate\Contracts\Queue\Queue as QueueContract;
9
10
class SwooleTaskQueue extends Queue implements QueueContract
11
{
12
    /**
13
     * Swoole Connector
14
     *
15
     * @var \Swoole\Http\Server
16
     */
17
    protected $swoole;
18
19
    /**
20
     * Create Async Task instance.
21
     *
22
     * @param \Swoole\Http\Server  $swoole
23
     */
24
    public function __construct($swoole)
25
    {
26
        $this->swoole = $swoole;
27
    }
28
29
    /**
30
     * Push a new job onto the queue.
31
     *
32
     * @param  string|object  $job
33
     * @param  mixed   $data
34
     * @param  string  $queue
35
     * @return mixed
36
     */
37
    public function push($job, $data = '', $queue = null)
38
    {
39
        return $this->pushRaw($this->createPayload($job, $data), $queue);
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

39
        return $this->pushRaw($this->createPayload(/** @scrutinizer ignore-type */ $job, $data), $queue);
Loading history...
40
    }
41
42
    /**
43
     * Push a raw payload onto the queue.
44
     *
45
     * @param  string  $payload
46
     * @param  string  $queue
47
     * @param  array   $options
48
     * @return mixed
49
     */
50
    public function pushRaw($payload, $queue = null, array $options = [])
51
    {
52
        return $this->swoole->task($payload, ! is_numeric($queue) ? 1 : (int) $queue);
53
    }
54
55
    /**
56
     * Push a new job onto the queue after a delay.
57
     *
58
     * @param  \DateTimeInterface|\DateInterval|int  $delay
59
     * @param  string|object  $job
60
     * @param  mixed   $data
61
     * @param  string  $queue
62
     * @return mixed
63
     */
64
    public function later($delay, $job, $data = '', $queue = null)
65
    {
66
        return Timer::after($this->secondsUntil($delay) * 1000, function () use ($job, $data, $queue) {
67
            return $this->push($job, $data, $queue);
68
        });
69
    }
70
71
    /**
72
     * Create a typical, string based queue payload array.
73
     *
74
     * @param  string  $job
75
     * @param  mixed  $data
76
     *
77
     * @throws Expcetion
78
     */
79
    protected function createStringPayload($job, $data)
80
    {
81
        throw new Exception('Unsupported empty data');
82
    }
83
84
    /**
85
     * Get the size of the queue.
86
     *
87
     * @param  string  $queue
88
     * @return int
89
     */
90
    public function size($queue = null)
91
    {
92
        return -1;
93
    }
94
95
    /**
96
     * Pop the next job off of the queue.
97
     *
98
     * @param  string  $queue
99
     * @return \Illuminate\Contracts\Queue\Job|null
100
     */
101
    public function pop($queue = null)
102
    {
103
        return null;
104
    }
105
}
106