Completed
Push — master ( 73a5aa...d5d862 )
by Ryosuke
03:31
created

Response::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 18
cts 18
cp 1
rs 9.4285
cc 3
eloc 16
nc 3
nop 2
crap 3
1
<?php
2
3
namespace mpyw\Cowitter;
4
5
use mpyw\Cowitter\Helpers\ResponseBodyNormalizer;
6
7
class Response implements ResponseInterface
8
{
9
    protected $version;
10
    protected $statusCode;
11
    protected $reasonPhrase;
12
    protected $headers = [];
13
    protected $handle;
14
    protected $rawContent;
15
    protected $content;
16
17 35
    public function __construct($buffer, $ch)
18 35
    {
19 35
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
20 35
        $header_buffer = substr($buffer, 0, $header_size - 4);
21 35
        $body_buffer = substr($buffer, $header_size);
22 35
        $header_buffer = current(array_slice(explode("\r\n\r\n", $header_buffer), -1));
23 35
        $lines = explode("\r\n", $header_buffer);
24 35
        if (!preg_match('@\AHTTP/([\d.]+)\s+(\d{3})\s+(.+)\z@i', array_shift($lines), $matches)) {
25 1
            throw new \UnexpectedValueException('Invalid response line.');
26
        }
27 34
        $this->version = $matches[1];
28 34
        $this->statusCode = (int)$matches[2];
29 34
        $this->reasonPhrase = $matches[3];
30 34
        foreach ($lines as $line) {
31 34
            list($key, $value) = explode(':', $line, 2) + [1 => ''];
32 34
            $this->headers[strtolower(trim($key))][] = trim($value);
33
        }
34 34
        $this->handle = $ch;
35 34
        $this->rawContent = $body_buffer;
36 34
    }
37
38 1
    public function getVersion()
39 1
    {
40 1
        return $this->version;
41
    }
42
43 24
    public function getStatusCode()
44 24
    {
45 24
        return $this->statusCode;
46
    }
47
48 2
    public function getReasonPhrase()
49 2
    {
50 2
        return $this->reasonPhrase;
51
    }
52
53 1
    public function getHeaders()
54 1
    {
55 1
        return $this->headers;
56
    }
57
58 2
    public function getHeader($name)
59 2
    {
60 2
        $name = strtolower($name);
61 2
        return isset($this->headers[$name]) ? $this->headers[$name] : [];
62
    }
63
64 2
    public function getHeaderLine($name, $delimiter = ', ')
65 2
    {
66 2
        $name = strtolower($name);
67 2
        return implode($delimiter, $this->getHeader($name));
68
    }
69
70 5
    public function getHandle()
71 5
    {
72 5
        return $this->handle;
73
    }
74
75 22
    public function getRawContent()
76 22
    {
77 22
        return $this->rawContent;
78
    }
79
80 23
    public function getContent()
81 23
    {
82 23
        if ($this->content === null) {
83 1
            throw new \UnderflowException('Decoded content has not created yet.');
84
        }
85 22
        return $this->content;
86
    }
87
88 14
    public function hasContent()
89 14
    {
90 14
        return $this->content !== null;
91
    }
92
93 23
    public function withDecodedContent($content)
94 23
    {
95 23
        $clone = clone $this;
96 23
        $clone->content = $content;
97 23
        return $clone;
98
    }
99
}
100