1
|
|
|
<?php |
2
|
|
|
namespace Phloppy\Client; |
3
|
|
|
|
4
|
|
|
use Phloppy\Exception\CommandException; |
5
|
|
|
use Phloppy\Job; |
6
|
|
|
use Phloppy\RespUtils; |
7
|
|
|
use Phloppy\Stream\StreamException; |
8
|
|
|
use Phloppy\Stream\StreamInterface; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\ImmutableEventDispatcher; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract Disque Client. |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractClient { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var StreamInterface |
22
|
|
|
*/ |
23
|
|
|
protected $stream; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $log; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var EventDispatcherInterface |
32
|
39 |
|
*/ |
33
|
|
|
private $dispatcher; |
34
|
39 |
|
|
35
|
39 |
|
|
36
|
39 |
|
/** |
37
|
|
|
* @param StreamInterface $stream |
38
|
39 |
|
* @param LoggerInterface|null $log Logger instance. |
39
|
39 |
|
*/ |
40
|
39 |
|
public function __construct( |
41
|
|
|
StreamInterface $stream, |
42
|
|
|
LoggerInterface $log = null, |
43
|
|
|
EventDispatcherInterface $dispatcher = null |
44
|
|
|
) { |
45
|
|
|
if (!$log) { |
46
|
|
|
$log = new NullLogger(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!$dispatcher) { |
50
|
|
|
$dispatcher = new EventDispatcher(); |
51
|
|
|
} |
52
|
30 |
|
|
53
|
|
|
$this->log = $log; |
|
|
|
|
54
|
30 |
|
$this->stream = $stream; |
|
|
|
|
55
|
30 |
|
$this->dispatcher = $dispatcher; |
56
|
25 |
|
} |
57
|
25 |
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return EventDispatcherInterface |
61
|
|
|
*/ |
62
|
|
|
public function getDispatcher() |
63
|
|
|
{ |
64
|
|
|
return new $this->dispatcher; |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
|
68
|
8 |
|
/** |
69
|
8 |
|
* Send request and retrieve response to the connected disque node. |
70
|
7 |
|
* |
71
|
7 |
|
* @param array $args |
72
|
7 |
|
* |
73
|
8 |
|
* @return array|int|null|string |
74
|
|
|
* |
75
|
8 |
|
* @throws CommandException |
76
|
|
|
* @throws StreamException |
77
|
|
|
*/ |
78
|
|
|
protected function send(array $args = []) |
79
|
|
|
{ |
80
|
|
|
$this->log->debug('send()ing command', $args); |
81
|
|
|
$response = RespUtils::deserialize($this->stream->write(RespUtils::serialize($args))); |
82
|
|
|
$this->log->debug('response', [$response]); |
83
|
|
|
|
84
|
|
|
return $response; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Map Disque's job responses to Job objects. |
90
|
|
|
* |
91
|
|
|
* @param array $list Job response array from the disque server. |
92
|
|
|
* |
93
|
|
|
* @return Job[] |
94
|
|
|
*/ |
95
|
|
|
protected function mapJobs(array $list) |
96
|
|
|
{ |
97
|
|
|
return array_map( |
98
|
|
|
function ($element) { |
99
|
|
|
return Job::create([ |
100
|
|
|
'queue' => $element[0], |
101
|
|
|
'id' => $element[1], |
102
|
|
|
'body' => isset($element[2]) ? $element[2] : '', |
103
|
|
|
]); |
104
|
|
|
}, |
105
|
|
|
$list |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.