SwooleTaskQueue::later()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Task;
4
5
use Exception;
6
use Illuminate\Contracts\Queue\Queue as QueueContract;
7
use Illuminate\Queue\Queue;
8
use Swoole\Timer;
9
10
/**
11
 * Class SwooleTaskQueue (5.7)
12
 */
13
class SwooleTaskQueue extends Queue implements QueueContract
14
{
15
    /**
16
     * Swoole Connector
17
     *
18
     * @var \Swoole\Http\Server
19
     */
20
    protected $swoole;
21
22
    /**
23
     * Create Async Task instance.
24
     *
25
     * @param \Swoole\Http\Server $swoole
26
     */
27
    public function __construct($swoole)
28
    {
29
        $this->swoole = $swoole;
30
    }
31
32
    /**
33
     * Push a new job onto the queue.
34
     *
35
     * @param  string|object $job
36
     * @param  mixed $data
37
     * @param  string $queue
38
     *
39
     * @return mixed
40
     */
41
    public function push($job, $data = '', $queue = null)
42
    {
43
        return $this->pushRaw($this->createPayload($job, $data), $queue);
44
    }
45
46
    /**
47
     * Push a raw payload onto the queue.
48
     *
49
     * @param  string $payload
50
     * @param  string $queue
51
     * @param  array $options
52
     *
53
     * @return mixed
54
     */
55
    public function pushRaw($payload, $queue = null, array $options = [])
56
    {
57
        return $this->swoole->task($payload, ! is_numeric($queue) ? -1 : (int)$queue);
58
    }
59
60
    /**
61
     * Push a new job onto the queue after a delay.
62
     *
63
     * @param  \DateTimeInterface|\DateInterval|int $delay
64
     * @param  string|object $job
65
     * @param  mixed $data
66
     * @param  string $queue
67
     *
68
     * @return mixed
69
     */
70
    public function later($delay, $job, $data = '', $queue = null)
71
    {
72
        return Timer::after($this->secondsUntil($delay) * 1000, function () use ($job, $data, $queue) {
73
            return $this->push($job, $data, $queue);
74
        });
75
    }
76
77
    /**
78
     * Create a typical, string based queue payload array.
79
     *
80
     * @param  string $job
81
     * @param  string $queue
82
     * @param  mixed $data
83
     *
84
     * @throws \Exception
85
     */
86
    protected function createStringPayload($job, $queue, $data)
87
    {
88
        throw new Exception('Unsupported empty data');
89
    }
90
91
    /**
92
     * Get the size of the queue.
93
     *
94
     * @param  string $queue
95
     *
96
     * @return int
97
     */
98
    public function size($queue = null)
99
    {
100
        return -1;
101
    }
102
103
    /**
104
     * Pop the next job off of the queue.
105
     *
106
     * @param  string $queue
107
     *
108
     * @return \Illuminate\Contracts\Queue\Job|null
109
     */
110
    public function pop($queue = null)
111
    {
112
        return null;
113
    }
114
}
115