Completed
Pull Request — master (#36)
by
unknown
07:50
created

PostArticleCommand::onPostingFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Response;
16
17
/**
18
 * PostCommand.
19
 *
20
 * @author thebandit
21
 */
22
class PostArticleCommand extends Command implements CommandInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $groups;
28
29
    /**
30
     * @var string
31
     */
32
    private $subject;
33
34
    /**
35
     * @var string
36
     */
37
    private $body;
38
39
    /**
40
     * @var string
41
     */
42
    private $from;
43
44
    /**
45
     * @var string
46
     */
47
    private $headers;
48
49
    /**
50
     * Constructor.
51
     */
52 8
    public function __construct($groups, $subject, $body, $from, $headers)
53
    {
54 8
        $this->groups = $groups;
55 8
        $this->subject = $subject;
56 8
        $this->body = $body;
57 8
        $this->from = $from;
58 8
        $this->headers = $headers;
59
60 8
        parent::__construct();
61 8
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function __invoke()
67
    {
68
        $article = [
69 1
            'From: '.$this->from,
70 1
            'Newsgroups: '.$this->groups,
71 1
            'Subject: '.$this->subject,
72 1
        ];
73
74 1
        if (null !== $this->headers) {
75
            $article[] = $this->headers;
76
        }
77
78 1
        if (!preg_grep('/^X-poster/', $article)) {
79 1
            $article[] = 'X-poster: php-nntp';
80 1
        }
81
82 1
        $article[] = "\r\n".$this->body;
83
84 1
        return implode("\r\n", $article);
85
    }
86
87 1
    public function onArticleReceived(Response $response)
88
    {
89 1
        return $response;
90
    }
91
92 1
    public function onPostingFailed(Response $response)
93
    {
94 1
        throw new RuntimeException(sprintf('Posting failed: %s', $response->getMessage()), $response->getStatusCode());
95
    }
96
}
97