Completed
Pull Request — master (#23)
by
unknown
08:59
created

ListCommand::getExpectedResponseCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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