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
|
|
|
use Disque\Command\Argument\InvalidOptionException; |
8
|
|
|
use Disque\Command\Response\JscanResponse; |
9
|
|
|
|
10
|
|
|
class JScan extends BaseCommand implements CommandInterface |
11
|
|
|
{ |
12
|
|
|
use ArrayChecker; |
13
|
|
|
use OptionChecker; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Tells which class handles the response |
17
|
|
|
* |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $responseHandler = JscanResponse::class; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Available command options |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $options = [ |
28
|
|
|
'busyloop' => false, |
29
|
|
|
'count' => null, |
30
|
|
|
'queue' => null, |
31
|
|
|
'state' => null, |
32
|
|
|
'reply' => null |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Available command arguments, and their mapping to options |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $availableArguments = [ |
41
|
|
|
'busyloop' => 'BUSYLOOP', |
42
|
|
|
'count' => 'COUNT', |
43
|
|
|
'queue' => 'QUEUE', |
44
|
|
|
'state' => 'STATE', |
45
|
|
|
'reply' => 'REPLY' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get command |
50
|
|
|
* |
51
|
|
|
* @return string Command |
52
|
|
|
*/ |
53
|
|
|
public function getCommand() |
54
|
|
|
{ |
55
|
|
|
return 'JSCAN'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set arguments for the command |
60
|
|
|
* |
61
|
|
|
* @param array $arguments Arguments |
62
|
|
|
* @throws InvalidOptionException |
63
|
|
|
*/ |
64
|
|
|
public function setArguments(array $arguments) |
65
|
|
|
{ |
66
|
|
|
$options = []; |
67
|
|
|
if (!empty($arguments)) { |
68
|
|
View Code Duplication |
if ( |
|
|
|
|
69
|
|
|
!$this->checkFixedArray($arguments, count($arguments) > 1 ? 2 : 1) || |
70
|
|
|
!is_numeric($arguments[0]) || |
71
|
|
|
(isset($arguments[1]) && !is_array($arguments[1])) |
72
|
|
|
) { |
73
|
|
|
throw new InvalidCommandArgumentException($this, $arguments); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (isset($arguments[1])) { |
77
|
|
|
$options = $arguments[1]; |
78
|
|
|
$this->checkOptionsInt($options, ['count']); |
79
|
|
|
$this->checkOptionsString($options, ['queue', 'reply']); |
80
|
|
|
$this->checkOptionsArray($options, ['state']); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->arguments = array_merge([ |
85
|
|
|
!empty($arguments) ? (int) $arguments[0] : 0 |
86
|
|
|
], $this->toArguments($options)); |
87
|
|
|
} |
88
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.