Passed
Push — master ( 12a81e...e685ae )
by Sebastian
02:29
created

RequestHelper_Response::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the {@link RequestHelper_Response} class.
4
 * @package Application Utils
5
 * @subpackage RequestHelper
6
 * @see RequestHelper_Response
7
 */
8
9
declare(strict_types=1);
10
11
namespace AppUtils;
12
13
/**
14
 * Stores information on the response to a request that was sent.
15
 *
16
 * @package Application Utils
17
 * @subpackage RequestHelper
18
 * @author Sebastian Mordziol <[email protected]>
19
 */
20
class RequestHelper_Response
21
{
22
   /**
23
    * @var RequestHelper
24
    */
25
    protected $request;
26
    
27
   /**
28
    * @var string
29
    */
30
    protected $body = '';
31
    
32
   /**
33
    * @var array
34
    */
35
    protected $info;
36
    
37
   /**
38
    * @var bool
39
    */
40
    protected $isError = false;
41
    
42
   /**
43
    * @var string
44
    */
45
    protected $errorMessage = '';
46
    
47
   /**
48
    * @var integer
49
    */
50
    protected $errorCode = 0;
51
    
52
   /**
53
    * @param RequestHelper $helper
54
    * @param array $info The CURL info array from curl_getinfo().
55
    */
56
    public function __construct(RequestHelper $helper, array $info)
57
    {
58
        $this->request = $helper;
59
        $this->info = $info;
60
    }
61
    
62
   /**
63
    * Retrieves the request instance that initiated the request.
64
    *  
65
    * @return RequestHelper
66
    */
67
    public function getRequest() : RequestHelper
68
    {
69
        return $this->request;
70
    }
71
    
72
    public function setError(int $code, string $message) : RequestHelper_Response
73
    {
74
        $this->errorMessage = $message;
75
        $this->errorCode = $code;
76
        $this->isError = true;
77
        
78
        return $this;
79
    }
80
    
81
    public function setBody(string $body) : RequestHelper_Response
82
    {
83
        $this->body = $body;
84
        return $this;
85
    }
86
    
87
   /**
88
    * Whether an error occurred in the request.
89
    * @return bool
90
    */
91
    public function isError() : bool
92
    {
93
        return $this->isError;
94
    }
95
    
96
   /**
97
    * Retrieves the native error message, if an error occurred.
98
    * @return string
99
    */
100
    public function getErrorMessage() : string
101
    {
102
        return $this->errorMessage;
103
    }
104
    
105
   /**
106
    * Retrieves the native error code, if an error occurred.
107
    * @return int
108
    */
109
    public function getErrorCode() : int
110
    {
111
        return $this->errorCode;
112
    }
113
    
114
    
115
   /**
116
    * Retrieves the full body of the request.
117
    * 
118
    * @return string
119
    */
120
    public function getRequestBody() : string
121
    {
122
        return $this->request->getBody();
123
    }
124
    
125
   /**
126
    * Retrieves the body of the response, if any.
127
    * 
128
    * @return string
129
    */
130
    public function getResponseBody() : string
131
    {
132
        return $this->body;
133
    }
134
    
135
   /**
136
    * The response HTTP code.
137
    * 
138
    * @return int The code, or 0 if none was sent (on error).
139
    */
140
    public function getCode() : int
141
    {
142
        return intval($this->getInfoKey('http_code'));
143
    }
144
    
145
   /**
146
    * Retrieves the actual headers that were sent in the request:
147
    * one header by entry in the indexed array.
148
    * 
149
    * @return array
150
    */
151
    public function getHeaders() : array
152
    {
153
        return ConvertHelper::explodeTrim("\n", $this->getInfoKey('request_header'));
154
    }
155
    
156
    protected function getInfoKey(string $name) : string
157
    {
158
        if(isset($this->info[$name])) {
159
            return (string)$this->info[$name];
160
        }
161
        
162
        return '';
163
    }
164
}
165