ListCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 0
cbo 3
dl 0
loc 77
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 4 1
A __construct() 0 7 1
A onInvalidKeyword() 0 4 1
A onError() 0 4 1
A onInformationFollows() 0 20 2
1
<?php
2
3
/*
4
 * This file is part of the NNTP library.
5
 *
6
 * (c) Robin van der Vleuten <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rvdv\Nntp\Command;
13
14
use Rvdv\Nntp\Exception\RuntimeException;
15
use Rvdv\Nntp\Response\MultiLineResponse;
16
use Rvdv\Nntp\Response\Response;
17
18
class ListCommand extends Command
19
{
20
    const KEYWORD_ACTIVE = 'ACTIVE';
21
    const KEYWORD_ACTIVE_TIMES = 'ACTIVE.TIMES';
22
    const KEYWORD_DISTRIB_PATS = 'DISTRIB.PATS';
23
    const KEYWORD_HEADERS = 'HEADERS';
24
    const KEYWORD_NEWSGROUPS = 'NEWSGROUPS';
25
    const KEYWORD_OVERVIEW_FMT = 'OVERVIEW.FMT';
26
27
    /**
28
     * @var string
29
     */
30
    protected $keyword;
31
32
    /**
33
     * @var string
34
     */
35
    protected $arguments;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param string $keyword
41
     * @param string $arguments
42
     */
43 7
    public function __construct($keyword = null, $arguments = null)
44
    {
45 7
        $this->keyword = $keyword;
46 7
        $this->arguments = $arguments;
47
48 7
        parent::__construct(true);
49 7
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function __invoke()
55
    {
56 1
        return trim(sprintf('LIST %s %s', $this->keyword, $this->arguments));
57
    }
58
59
    /**
60
     * @param MultiLineResponse $response
61
     *
62
     * @return array
63
     */
64 1
    public function onInformationFollows(MultiLineResponse $response)
65
    {
66 1
        $lines = $response->getLines();
67 1
        $totalLines = count($lines);
68
69 1
        $result = [];
70
71 1
        for ($i = 0; $i < $totalLines; ++$i) {
72 1
            list($name, $high, $low, $status) = explode(' ', $lines[$i]);
73
74 1
            $result[$i] = [
75 1
                'name' => $name,
76 1
                'high' => $high,
77 1
                'low' => $low,
78 1
                'status' => $status,
79
            ];
80 1
        }
81
82 1
        return $result;
83
    }
84
85 1
    public function onInvalidKeyword(Response $response)
86
    {
87 1
        throw new RuntimeException('Invalid keyword, or unexpected argument for keyword.', $response->getStatusCode());
88
    }
89
90 1
    public function onError(Response $response)
91
    {
92 1
        throw new RuntimeException('Error retrieving group list', $response->getStatusCode());
93
    }
94
}
95