Completed
Push — master ( 83696f...79674b )
by Robin
8s
created

ListCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 9
    public function __construct($keyword = null, $arguments = null)
33
    {
34 9
        $this->keyword = $keyword;
35 9
        $this->arguments = $arguments;
36 9
        parent::__construct([], true);
37 9
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    public function getExpectedResponseCodes()
43
    {
44
        return [
45 2
            Response::INFORMATION_FOLLOWS => 'onListFollows',
46 2
            Response::INVALID_KEYWORD     => 'onInvalidKeyword',
47 2
            Response::PROGRAM_ERROR       => 'onError',
48 2
        ];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function execute()
55
    {
56 1
        return trim(sprintf('LIST %s %s', $this->keyword, $this->arguments));
57
    }
58
59
    /**
60
     * Called when the list is received from the server.
61
     *
62
     * @param MultiLineResponse $response
63
     */
64 1
    public function onListFollows(MultiLineResponse $response)
65
    {
66 1
        $lines = $response->getLines();
67 1
        $totalLines = count($lines);
68
69 1
        for ($i = 0; $i < $totalLines; ++$i) {
70 1
            list($name, $high, $low, $status) = explode(' ', $lines[$i]);
71
72 1
            $this->result[$i] = [
73 1
                'name'   => $name,
74 1
                'high'   => $high,
75 1
                'low'    => $low,
76 1
                'status' => $status,
77
            ];
78 1
        }
79 1
    }
80
81 1
    public function onInvalidKeyword(Response $response)
82
    {
83 1
        throw new RuntimeException('Invalid keyword, or unexpected argument for keyword.', $response->getStatusCode());
84
    }
85
86 1
    public function onError(Response $response)
87
    {
88 1
        throw new RuntimeException('Error retrieving group list', $response->getStatusCode());
89
    }
90
}
91