Completed
Pull Request — master (#33)
by
unknown
01:25
created

HeadCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
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
 * HeadCommand.
20
 *
21
 * @author elabz
22
 */
23
class HeadCommand extends Command implements CommandInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $article;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param string $article
34
     */
35
    public function __construct($article)
36
    {
37
        $this->article = $article;
38
39
        parent::__construct(true);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function __invoke()
46
    {
47
        return sprintf('HEAD %s', $this->article);
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function onHeadFollows(MultiLineResponse $response)
54
    {
55
        return array_reduce($response->getLines(), function ($headers, $line) {
56
            preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $matches);
57
            if (!empty($matches)) {
58
                $headers[$matches[1]] = trim($matches[2]);
59
            }
60
61
            return $headers;
62
        });
63
    }
64
65
    public function onNoNewsGroupCurrentSelected(Response $response)
66
    {
67
        throw new RuntimeException('A group must be selected first before getting an article header.', $response->getStatusCode());
68
    }
69
70
    public function onNoSuchArticleNumber(Response $response)
71
    {
72
        throw new RuntimeException('No article with that number.', $response->getStatusCode());
73
    }
74
75
    public function onNoSuchArticleId(Response $response)
76
    {
77
        throw new RuntimeException('No article with that message-id.', $response->getStatusCode());
78
    }
79
}
80