1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rvdv\Nntp\Command; |
4
|
|
|
|
5
|
|
|
use Rvdv\Nntp\Exception\RuntimeException; |
6
|
|
|
use Rvdv\Nntp\Response\MultiLineResponse; |
7
|
|
|
use Rvdv\Nntp\Response\Response; |
8
|
|
|
|
9
|
|
|
class ListCommand extends Command |
10
|
|
|
{ |
11
|
|
|
const KEYWORD_ACTIVE = 'ACTIVE'; |
12
|
|
|
const KEYWORD_ACTIVE_TIMES = 'ACTIVE.TIMES'; |
13
|
|
|
const KEYWORD_DISTRIB_PATS = 'DISTRIB.PATS'; |
14
|
|
|
const KEYWORD_HEADERS = 'HEADERS'; |
15
|
|
|
const KEYWORD_NEWSGROUPS = 'NEWSGROUPS'; |
16
|
|
|
const KEYWORD_OVERVIEW_FMT = 'OVERVIEW.FMT'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $keyword; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $arguments; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $keyword |
30
|
|
|
* @param string $arguments |
31
|
|
|
*/ |
32
|
|
|
public function __construct($keyword = null, $arguments = null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct([], true); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function getExpectedResponseCodes() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
Response::INFORMATION_FOLLOWS => 'onListFollows', |
44
|
|
|
Response::INVALID_KEYWORD => 'onInvalidKeyword', |
45
|
|
|
Response::PROGRAM_ERROR => 'onError', |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function execute() |
53
|
|
|
{ |
54
|
|
|
return sprintf('LIST %s %s', $this->keyword, $this->arguments); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Called when the list is received from the server. |
59
|
|
|
* |
60
|
|
|
* @param MultiLineResponse $response |
61
|
|
|
*/ |
62
|
|
|
public function onListFollows(MultiLineResponse $response) |
63
|
|
|
{ |
64
|
|
|
$lines = $response->getLines(); |
65
|
|
|
$totalLines = count($lines); |
66
|
|
|
|
67
|
|
|
for ($i = 0; $i < $totalLines; ++$i) { |
68
|
|
|
list($name, $high, $low, $status) = explode(' ', $lines[$i]); |
69
|
|
|
|
70
|
|
|
$this->result[$i] = [ |
71
|
|
|
'name' => $name, |
72
|
|
|
'high' => $high, |
73
|
|
|
'low' => $low, |
74
|
|
|
'status' => $status, |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function onInvalidKeyword(Response $response) |
80
|
|
|
{ |
81
|
|
|
throw new RuntimeException('Invalid keyword, or unexpected argument for keyword.', $response->getStatusCode()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function onError(Response $response) |
85
|
|
|
{ |
86
|
|
|
throw new RuntimeException('Error retrieving group list', $response->getStatusCode()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|