Response   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
B createFromString() 0 23 5
A __construct() 0 5 1
A getMessage() 0 4 1
A getStatusCode() 0 4 1
A __toString() 0 4 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\Response;
13
14
use Rvdv\Nntp\Exception\InvalidArgumentException;
15
use Rvdv\Nntp\Exception\RuntimeException;
16
17
/**
18
 * Response.
19
 *
20
 * @author Robin van der Vleuten <[email protected]>
21
 */
22
class Response implements ResponseInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    public static $codes = [
28
        'HelpTextFollows' => 100, // RFC 3977
29
30
        'PostingAllowed' => 200, // RFC 3977
31
        'PostingProhibited' => 201, // RFC 3977
32
33
        'ConnectionClosing' => 205, // rfc 3977
34
        'GroupSelected' => 211, // rfc 3977
35
        'InformationFollows' => 215, // rfc 2980
36
        'ArticleFollows' => 220, //rfc 3977
37
        'HeadFollows' => 221, //rfc 3977 Section 6.2.2
38
        'BodyFollows' => 222, //rfc 3977
39
        'OverviewInformationFollows' => 224, // rfc 2980
40
        'ArticleReceived' => 240, //rfc 3977
41
        'AuthenticationAccepted' => 281, // rfc 4643
42
        'XfeatureEnabled' => 290,
43
44
        'SendArticle' => 340, // rfc 3977
45
        'PasswordRequired' => 381, // rfc 4643
46
47
        'NoSuchGroup' => 411, // rfc 3977
48
        'NoNewsgroupCurrentSelected' => 412, // rfc 2980
49
        'NoArticlesSelected' => 420, // rfc 2980
50
        'NoSuchArticleNumber' => 423, // rfc 3977
51
        'NoSuchArticleId' => 430, // rfc 3977
52
        'PostingNotPermitted' => 440, // rfc 3977
53
        'PostingFailed' => 441, // rfc 3977
54
        'AuthenticationRejected' => 481, // rfc 4643
55
        'AuthenticationOutOfSequence' => 482, // rfc 4643
56
57
        'CommandUnknown' => 500, // rfc 3977
58
        'InvalidKeyword' => 501, // rfc 3977
59
        'CommandUnavailable' => 502, // rfc 4643
60
        'ProgramError' => 503, // rfc 2980
61
    ];
62
63
    /**
64
     * @var string
65
     */
66
    private $message;
67
68
    /**
69
     * @var int
70
     */
71
    private $statusCode;
72
73
    /**
74
     * @param string $response
75
     */
76 9
    public static function createFromString($response)
77
    {
78 9
        if (false === strpos($response, "\r\n")) {
79 1
            throw new InvalidArgumentException(
80
                'Invalid response given: response string should be terminated by &#92;r&#92;n'
81 1
            );
82
        }
83
84 8
        $response = trim($response);
85 8
        if (!preg_match('/^(\d{3}) (.+)$/s', $response, $matches)) {
86 1
            throw new InvalidArgumentException(
87 1
                sprintf('Invalid response given: "%s"', $response)
88 1
            );
89
        }
90
91 7
        if ($matches[1] < 100 || $matches[1] >= 600) {
92 1
            throw new RuntimeException(
93 1
                sprintf('Invalid status code: %d', $matches[1])
94 1
            );
95
        }
96
97 6
        return new self((int) $matches[1], $matches[2]);
98
    }
99
100
    /**
101
     * Constructor.
102
     *
103
     * @param int    $statusCode
104
     * @param string $message
105
     */
106 6
    public function __construct($statusCode, $message)
107
    {
108 6
        $this->statusCode = $statusCode;
109 6
        $this->message = $message;
110 6
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 3
    public function getMessage()
116
    {
117 3
        return $this->message;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 4
    public function getStatusCode()
124
    {
125 4
        return $this->statusCode;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 2
    public function __toString()
132
    {
133 2
        return sprintf('%s [%d]', $this->message, $this->statusCode);
134
    }
135
}
136