Completed
Push — master ( ab5c84...b6c757 )
by Nelson J
03:47
created

Response::bodyRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Njasm\Soundcloud\Request;
4
5
/**
6
 * SoundCloud API wrapper in PHP
7
 *
8
 * @author      Nelson J Morais <[email protected]>
9
 * @copyright   2014 Nelson J Morais <[email protected]>
10
 * @license     http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link        http://github.com/njasm/soundcloud
12
 * @package     Njasm\Soundcloud
13
 */
14
15
class Response implements ResponseInterface
16
{
17
    private $httpVersion;
18
    private $httpCode;
19
    private $httpCodeString;
20
    private $response;
21
    private $info;
22
    private $errno;
23
    private $errorString;
24
    private $headers = array();
25
    private $body;
26
    
27 19
    public function __construct($response, array $info, $errno, $errorString)
28
    {
29 19
        $this->response = $response;
30 19
        $this->info = $info;
31 19
        $this->errno = $errno;
32 19
        $this->errorString = $errorString;
33
        
34 19
        list($headers, $body) = $this->buildHeaderParts($response);
35
36 19
        $this->buildHeadersArray($headers);        
37 19
        $this->body = $body;
38 19
    }
39
    
40 19
    private function buildHeaderParts($response)
41
    {
42 19
        $parts = explode("\r\n\r\nHTTP/", $response);
43 19
        $parts = (count($parts) > 1 ? 'HTTP/' : '') . array_pop($parts);
44
        
45 19
        return explode("\r\n\r\n", $parts, 2);
46
    }
47
    
48 19
    private function buildHeadersArray($headers)
49
    {
50 19
        $headers = explode("\n", $headers);
51 19
        $httHead = array_shift($headers);
52 19
        list($this->httpVersion, $this->httpCode, $this->httpCodeString) = explode(" ", $httHead, 3);
53
        
54 19
        foreach ($headers as $header) {  
55 19
            list($key, $value) = explode(": ", $header, 2);
56 19
            $this->headers[trim($key)] = trim($value);
57
        }
58 19
    }
59
    
60 1
    public function getHeaders()
61
    {
62 1
        return $this->headers;
63
    }
64
    
65 9
    public function hasHeader($header)
66
    {
67 9
        return array_key_exists($header, $this->headers);
68
    }
69
    
70 8
    public function getHeader($header)
71
    {
72 8
        return $this->hasHeader($header) ? $this->headers[$header] : null;
0 ignored issues
show
Documentation introduced by
$header is of type string, but the function expects a object<Njasm\Soundcloud\Request\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
    }
74
    
75 9
    public function bodyRaw()
76
    {
77 9
        return $this->body;
78
    }
79
    
80 5
    public function bodyObject()
81
    {
82 5
        $contentType = $this->getHeader('Content-Type');
83 5
        if (stripos($contentType, 'application/json') !== false) {
84 4
            return json_decode($this->body);
85
        }
86
87 1
        throw new \OutOfBoundsException("Last Request Content-Type isn't application/json.");
88
    }
89
    
90 2
    public function bodyArray()
91
    {
92 2
        $body = $this->bodyRaw();
93
        
94 2
        return json_decode($body, true);
95
    }
96
    
97 1
    public function getHttpVersion()
98
    {
99 1
        return $this->httpVersion;
100
    }
101
    
102 1
    public function getHttpCode()
103
    {
104 1
        return $this->httpCode;
105
    }
106
    
107 1
    public function getInfo()
108
    {
109 1
        return $this->info;
110
    }
111
    
112
    public function getErrorNo()
113
    {
114
        return $this->errno;
115
    }
116
    
117
    public function getErrorString()
118
    {
119
        return $this->errorString;
120
    }
121
    
122
    public function getRaw()
123
    {
124
        return $this->response;
125
    }
126
    
127
    public function getHttpCodeString()
128
    {
129
        return $this->httpCodeString;
130
    }
131
}
132