|
1
|
|
|
<?php |
|
2
|
|
|
namespace Disque\Command; |
|
3
|
|
|
|
|
4
|
|
|
use Disque\Command\Argument\ArrayChecker; |
|
5
|
|
|
use Disque\Command\Argument\OptionChecker; |
|
6
|
|
|
use Disque\Command\Argument\InvalidCommandArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class AddJob extends BaseCommand implements CommandInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use ArrayChecker; |
|
11
|
|
|
use OptionChecker; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Available command options |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $options = [ |
|
19
|
|
|
'timeout' => 0, |
|
20
|
|
|
'replicate' => null, |
|
21
|
|
|
'delay' => null, |
|
22
|
|
|
'retry' => null, |
|
23
|
|
|
'ttl' => null, |
|
24
|
|
|
'maxlen' => null, |
|
25
|
|
|
'async' => false |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Available command arguments, and their mapping to options |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $availableArguments = [ |
|
34
|
|
|
'replicate' => 'REPLICATE', |
|
35
|
|
|
'delay' => 'DELAY', |
|
36
|
|
|
'retry' => 'RETRY', |
|
37
|
|
|
'ttl' => 'TTL', |
|
38
|
|
|
'maxlen' => 'MAXLEN', |
|
39
|
|
|
'async' => 'ASYNC' |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get command |
|
44
|
|
|
* |
|
45
|
|
|
* @return string Command |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getCommand() |
|
48
|
|
|
{ |
|
49
|
|
|
return 'ADDJOB'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set arguments for the command |
|
54
|
|
|
* |
|
55
|
|
|
* The first two values in the $arguments array must be the queue name and |
|
56
|
|
|
* the job body. The third value is optional and if present, must be |
|
57
|
|
|
* an array with further arguments. |
|
58
|
|
|
* @see $availableArguments |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $arguments Arguments |
|
61
|
|
|
* @throws InvalidCommandArgumentException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setArguments(array $arguments) |
|
64
|
|
|
{ |
|
65
|
|
|
$count = count($arguments); |
|
66
|
|
|
if (!$this->checkFixedArray($arguments, 2, true) || $count > 3) { |
|
67
|
|
|
throw new InvalidCommandArgumentException($this, $arguments); |
|
68
|
|
|
} elseif (!is_string($arguments[0]) || !is_string($arguments[1])) { |
|
69
|
|
|
throw new InvalidCommandArgumentException($this, $arguments); |
|
70
|
|
|
} elseif ($count === 3 && (!isset($arguments[2]) || !is_array($arguments[2]))) { |
|
71
|
|
|
throw new InvalidCommandArgumentException($this, $arguments); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$options = (!empty($arguments[2]) ? $arguments[2] : []) + ['timeout' => $this->options['timeout']]; |
|
75
|
|
|
$this->checkOptionsInt($options, ['timeout', 'replicate', 'delay', 'retry', 'ttl', 'maxlen']); |
|
76
|
|
|
|
|
77
|
|
|
$this->arguments = array_merge( |
|
78
|
|
|
[$arguments[0], $arguments[1], $options['timeout']], |
|
79
|
|
|
$this->toArguments(array_diff_key($options, ['timeout'=>null])) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
} |