1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pheanstalk\Command; |
6
|
|
|
|
7
|
|
|
use Pheanstalk\Contract\ResponseInterface; |
8
|
|
|
use Pheanstalk\Contract\ResponseParserInterface; |
9
|
|
|
use Pheanstalk\Exception; |
10
|
|
|
use Pheanstalk\Response\ArrayResponse; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The 'peek', 'peek-ready', 'peek-delayed' and 'peek-buried' commands. |
14
|
|
|
* |
15
|
|
|
* The peek commands let the client inspect a job in the system. There are four |
16
|
|
|
* variations. All but the first (peek) operate only on the currently used tube. |
17
|
|
|
*/ |
18
|
|
|
class PeekCommand extends AbstractCommand implements ResponseParserInterface |
19
|
|
|
{ |
20
|
|
|
public const TYPE_ID = 'id'; |
21
|
|
|
public const TYPE_READY = 'ready'; |
22
|
|
|
public const TYPE_DELAYED = 'delayed'; |
23
|
|
|
public const TYPE_BURIED = 'buried'; |
24
|
|
|
|
25
|
|
|
private const SUBCOMMANDS = [ |
26
|
|
|
self::TYPE_READY, |
27
|
|
|
self::TYPE_DELAYED, |
28
|
|
|
self::TYPE_BURIED, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $subcommand; |
35
|
|
|
|
36
|
28 |
|
public function __construct(string $peekSubject) |
37
|
|
|
{ |
38
|
28 |
|
if (in_array($peekSubject, self::SUBCOMMANDS)) { |
39
|
27 |
|
$this->subcommand = $peekSubject; |
40
|
|
|
} else { |
41
|
1 |
|
throw new Exception\CommandException(sprintf( |
42
|
1 |
|
'Invalid peek subject: %s', |
43
|
|
|
$peekSubject |
44
|
|
|
)); |
45
|
|
|
} |
46
|
27 |
|
} |
47
|
|
|
|
48
|
26 |
|
public function getCommandLine(): string |
49
|
|
|
{ |
50
|
26 |
|
return sprintf('peek-%s', $this->subcommand); |
51
|
|
|
} |
52
|
|
|
|
53
|
24 |
|
public function parseResponse(string $responseLine, ?string $responseData): ArrayResponse |
54
|
|
|
{ |
55
|
24 |
|
if ($responseLine == ResponseInterface::RESPONSE_NOT_FOUND) { |
56
|
23 |
|
return $this->createResponse(ResponseInterface::RESPONSE_NOT_FOUND); |
57
|
|
|
} |
58
|
|
|
|
59
|
14 |
|
if (preg_match('#^FOUND (\d+) \d+$#', $responseLine, $matches)) { |
60
|
13 |
|
return $this->createResponse( |
61
|
13 |
|
ResponseInterface::RESPONSE_FOUND, |
62
|
|
|
[ |
63
|
13 |
|
'id' => (int) $matches[1], |
64
|
13 |
|
'jobdata' => $responseData, |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
throw new Exception\ServerException("Unexpected response"); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|