Responding::payload()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Client responding operator
4
 * User: moyo
5
 * Date: 2018/5/30
6
 * Time: 3:49 PM
7
 */
8
9
namespace Carno\HTTP\Client;
10
11
use Carno\HTTP\Exception\ErrorResponseException;
12
use Carno\HTTP\Standard\Response;
13
14
class Responding
15
{
16
    /**
17
     * @var Response
18
     */
19
    private $response = null;
20
21
    /**
22
     * Responding constructor.
23
     * @param Response $response
24
     */
25
    public function __construct(Response $response)
26
    {
27
        $this->response = $response;
28
    }
29
30
    /**
31
     * @return int
32
     */
33
    public function code() : int
34
    {
35
        return $this->response->getStatusCode();
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function phrase() : string
42
    {
43
        return $this->response->getReasonPhrase();
44
    }
45
46
    /**
47
     * @param string $key
48
     * @return string
49
     */
50
    public function header(string $key) : ?string
51
    {
52
        return $this->response->hasHeader($key) ? $this->response->getHeaderLine($key) : null;
53
    }
54
55
    /**
56
     * raw response content
57
     * @return string
58
     */
59
    public function payload() : string
60
    {
61
        return (string) $this->response->getBody();
62
    }
63
64
    /**
65
     * parsed data with status checking
66
     * @return string
67
     * @throws ErrorResponseException
68
     */
69
    public function data() : string
70
    {
71
        $code = $this->response->getStatusCode();
72
73
        if ($code >= 400 && $code < 600) {
74
            throw new ErrorResponseException($this->response->getReasonPhrase(), $code);
75
        }
76
77
        return $this->payload();
78
    }
79
80
    /**
81
     * @deprecated
82
     * @return string
83
     */
84
    public function __toString() : string
85
    {
86
        return $this->payload();
87
    }
88
}
89