GetJob::setArguments()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 21
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 4
nop 1
crap 56
1
<?php
2
namespace Disque\Command;
3
4
use Disque\Command\Argument\StringChecker;
5
use Disque\Command\Argument\InvalidOptionException;
6
use Disque\Command\Response\JobsWithQueueResponse;
7
use Disque\Command\Response\JobsWithCountersResponse;
8
9
class GetJob extends BaseCommand implements CommandInterface
10
{
11
    use StringChecker;
12
13
    /**
14
     * Tells which class handles the response
15
     *
16
     * @var int
17
     */
18
    protected $responseHandler = JobsWithQueueResponse::class;
19
20
    /**
21
     * Available command options
22
     *
23
     * @var array
24
     */
25
    protected $options = [
26
        'nohang' => false,
27
        'count' => null,
28
        'timeout' => null,
29
        'withcounters' => false
30
    ];
31
32
    /**
33
     * Available command arguments, and their mapping to options
34
     *
35
     * @var array
36
     */
37
    protected $availableArguments = [
38
        'nohang' => 'NOHANG',
39
        'timeout' => 'TIMEOUT',
40
        'count' => 'COUNT',
41
        'withcounters' => 'WITHCOUNTERS'
42
    ];
43
44
    /**
45
     * Get command
46
     *
47
     * @return string Command
48
     */
49
    public function getCommand()
50
    {
51
        return 'GETJOB';
52
    }
53
54
    /**
55
     * Tells if this command blocks while waiting for a response, to avoid
56
     * being affected by connection timeouts.
57
     *
58
     * @return bool If true, this command blocks
59
     */
60
    public function isBlocking()
61
    {
62
        $arguments = $this->getArguments();
63
        $options = end($arguments);
64
        if (is_array($options) && !empty($options['nohang'])) {
65
            return false;
66
        }
67
        return true;
68
    }
69
70
    /**
71
     * Set arguments for the command
72
     *
73
     * The $arguments must contain at least one, possibly more queue names
74
     * to read from. If the last value in the $arguments array is an array,
75
     * it can contain further optional arguments.
76
     * @see $availableArguments
77
     *
78
     * @param array $arguments Arguments
79
     * @throws InvalidOptionException
80
     */
81
    public function setArguments(array $arguments)
82
    {
83
        $options = [];
84
        $last = end($arguments);
85
        if (is_array($last)) {
86
            $options = $last;
87
            $arguments = array_slice($arguments, 0, -1);
88
89
            if (
90
                (isset($options['count']) && !is_int($options['count'])) ||
91
                (isset($options['timeout']) && !is_int($options['timeout']))
92
            ) {
93
                throw new InvalidOptionException($this, $last);
94
            }
95
96
            if (!empty($options['withcounters'])) {
97
                // The response will contain NACKs and additional-deliveries
98
                $this->responseHandler = JobsWithCountersResponse::class;
99
            }
100
101
            $options = $this->toArguments($options);
102
        }
103
104
        $this->checkStringArguments($arguments);
105
        $this->arguments = array_merge($options, ['FROM'], array_values($arguments));
106
    }
107
}
108