CurlResponse::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
namespace VideoPublisher\Connection\Curl;
3
4
use VideoPublisher\Connection\ResponseInterface;
5
use VideoPublisher\Exception\KeyNotFoundException;
6
7
/**
8
 * Class CurlResponse.
9
 *
10
 * @author Bart Malestein <[email protected]>
11
 */
12
class CurlResponse implements ResponseInterface
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $content;
19
20
    /**
21
     * @var string
22
     */
23
    private $headers;
24
25
    /**
26
     * @var int
27
     */
28
    private $statusCode;
29
30
    /**
31
     * CurlResponse constructor.
32
     * @param $statusCode
33
     * @param $headers
34
     * @param $content
35
     */
36
    public function __construct($statusCode, $headers, $content)
37
    {
38
        $this->statusCode = $statusCode;
39
        $this->headers = $headers;
40
        $this->content = $content;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getContent()
47
    {
48
        return $this->content;
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getStatusCode()
55
    {
56
        return $this->statusCode;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getJsonResponse()
63
    {
64
        return json_decode($this->content, true);
65
    }
66
67
    /**
68
     * @param $name
69
     * @return bool|string
70
     * @throws KeyNotFoundException
71
     */
72
    public function getHeaderByName($name)
73
    {
74
        $headers = explode("\n", $this->headers);
75
        
76
        foreach ($headers as $header) {
77
            
78
            if (empty($header)) {
79
                
80
                continue;
81
            }
82
            $keyVal = explode(": ", $header);
83
            if ($keyVal[0] === $name) {
84
                
85
                return trim($keyVal[1], " \t\n\r\0\x0B");
86
            }
87
        }
88
        
89
        throw new KeyNotFoundException($name);
90
    }
91
}
92