Completed
Push — master ( 67a981...19aadf )
by Robin
04:39 queued 02:52
created

OverviewCommand::onOverviewInformationFollows()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 9
nc 1
nop 1
crap 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
/**
19
 * @author Robin van der Vleuten <[email protected]>
20
 */
21
abstract class OverviewCommand extends Command implements CommandInterface
22
{
23
    /**
24
     * @var int
25
     */
26
    protected $from;
27
28
    /**
29
     * @var int
30
     */
31
    protected $to;
32
33
    /**
34
     * @var array
35
     */
36
    protected $format;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param int   $from   the article number where the range begins
42
     * @param int   $to     the article number where the range ends
43
     * @param array $format the format of the articles in response
44
     */
45 13
    public function __construct($from, $to, array $format)
46
    {
47 13
        $this->from = $from;
48 13
        $this->to = $to;
49 13
        $this->format = array_merge(['number' => false], $format);
50
51 13
        parent::__construct(true);
52 13
    }
53
54 2
    public function onOverviewInformationFollows(MultiLineResponse $response)
55
    {
56
        return array_map(function ($line) {
57 2
            $segments = explode("\t", $line);
58 2
            $field = 0;
59
60 2
            return array_reduce(array_keys($this->format), function ($message, $name) use ($segments, &$field) {
61 2
                $message[$name] = $this->format[$name] ? ltrim(substr($segments[$field], strpos($segments[$field], ':') + 1), " \t") : $segments[$field];
62 2
                ++$field;
63
64 2
                return $message;
65 2
            }, []);
66 2
        }, $response->getLines());
67
    }
68
69 2
    public function onNoNewsGroupCurrentSelected(Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71 2
        throw new RuntimeException('A group must be selected first before getting an overview.');
72
    }
73
74 2
    public function onNoArticlesSelected(Response $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76 2
        throw new RuntimeException(sprintf('No articles selected in the given range %d-%d.', $this->from, $this->to));
77
    }
78
}
79