1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ©[2016] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace SugarAPI\SDK\Request\Abstracts; |
7
|
|
|
|
8
|
|
|
use SugarAPI\SDK\Request\Interfaces\RequestInterface; |
9
|
|
|
|
10
|
|
|
abstract class AbstractRequest implements RequestInterface { |
11
|
|
|
|
12
|
|
|
const STATUS_INIT = 'initialized'; |
13
|
|
|
const STATUS_SENT = 'sent'; |
14
|
|
|
const STATUS_CLOSED = 'closed'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The HTTP Request Type |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected static $_TYPE = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Default Curl Options |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected static $_DEFAULT_OPTIONS = array( |
27
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0, |
28
|
|
|
CURLOPT_HEADER => TRUE, |
29
|
|
|
CURLOPT_SSL_VERIFYPEER => FALSE, |
30
|
|
|
CURLOPT_RETURNTRANSFER => TRUE, |
31
|
|
|
CURLOPT_FOLLOWLOCATION => FALSE, |
32
|
|
|
CURLOPT_USERAGENT => 'SugarAPI-SDK-PHP' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The default HTTP Headers to be added to Curl Request |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected static $_DEFAULT_HEADERS = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The Curl Resource used to actually send data to Sugar API |
43
|
|
|
* @var - Curl Resource |
44
|
|
|
*/ |
45
|
|
|
protected $CurlResponse; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The raw response from curl_exec |
49
|
|
|
* @var - Curl Response |
50
|
|
|
*/ |
51
|
|
|
protected $CurlRequest; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* List of Headers for Request |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $headers = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The body of the request or payload. JSON Encoded |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $body = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The URL the Request is sent to |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $url = ''; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var null |
73
|
|
|
*/ |
74
|
|
|
protected $status = NULL; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The Request Type |
78
|
|
|
* @var |
79
|
|
|
*/ |
80
|
|
|
protected $type; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The options configured on the Curl Resource object |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $options = array(); |
87
|
|
|
|
88
|
1 |
|
public function __construct($url = NULL){ |
89
|
1 |
|
$this->start(); |
90
|
1 |
|
if (!empty($url)){ |
91
|
1 |
|
$this->setURL($url); |
92
|
1 |
|
} |
93
|
1 |
|
$this->setType(static::$_TYPE); |
94
|
1 |
|
$this->setHeaders(static::$_DEFAULT_HEADERS); |
95
|
1 |
|
foreach (static::$_DEFAULT_OPTIONS as $option => $value){ |
96
|
1 |
|
$this->setOption($option, $value); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Always make sure to destroy Curl Resource |
103
|
|
|
*/ |
104
|
1 |
|
public function __destruct() { |
105
|
1 |
|
if ($this->status!==self::STATUS_CLOSED){ |
106
|
1 |
|
curl_close($this->CurlRequest); |
107
|
1 |
|
} |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
1 |
|
public function setURL($url){ |
114
|
1 |
|
$this->url = $url; |
115
|
1 |
|
$this->setOption(CURLOPT_URL, $this->url); |
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
1 |
|
public function getURL(){ |
123
|
1 |
|
return $this->url; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
1 |
|
public function addHeader($name, $value){ |
130
|
1 |
|
$token = $name.": ".$value; |
131
|
1 |
|
$this->headers[] = $token; |
132
|
1 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritdoc |
137
|
|
|
*/ |
138
|
1 |
|
public function setHeaders(array $array = array()){ |
139
|
1 |
|
if (count($array)>0){ |
140
|
1 |
|
foreach ($array as $key => $value){ |
141
|
1 |
|
if (is_numeric($key)){ |
142
|
1 |
|
$this->headers[] = $value; |
143
|
1 |
|
}else { |
144
|
1 |
|
$this->addHeader($key, $value); |
145
|
|
|
} |
146
|
1 |
|
} |
147
|
1 |
|
} |
148
|
1 |
|
$this->setOption(CURLOPT_HTTPHEADER, $this->headers); |
149
|
1 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritdoc |
154
|
|
|
*/ |
155
|
1 |
|
public function getHeaders(){ |
156
|
1 |
|
return $this->headers; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritdoc |
161
|
|
|
*/ |
162
|
1 |
|
public function setBody($body){ |
163
|
1 |
|
$this->body = $body; |
164
|
1 |
|
$this->setOption(CURLOPT_POSTFIELDS, $this->body); |
165
|
1 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritdoc |
170
|
|
|
*/ |
171
|
1 |
|
public function getBody(){ |
172
|
1 |
|
return $this->body; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritdoc |
177
|
|
|
*/ |
178
|
2 |
|
public function getCurlObject(){ |
179
|
2 |
|
return $this->CurlRequest; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @inheritdoc |
184
|
|
|
*/ |
185
|
1 |
|
public function setOption($option, $value){ |
186
|
1 |
|
curl_setopt($this->CurlRequest, $option, $value); |
187
|
1 |
|
$this->options[$option] = $value; |
188
|
1 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritdoc |
193
|
|
|
*/ |
194
|
1 |
|
public function getOptions() { |
195
|
1 |
|
return $this->options; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @inheritdoc |
200
|
|
|
*/ |
201
|
1 |
|
public function send(){ |
202
|
1 |
|
$this->setHeaders(); |
203
|
1 |
|
$this->CurlResponse = curl_exec($this->CurlRequest); |
204
|
1 |
|
$this->status = self::STATUS_SENT; |
|
|
|
|
205
|
1 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritdoc |
210
|
|
|
*/ |
211
|
1 |
|
public function getCurlResponse(){ |
212
|
1 |
|
return $this->CurlResponse; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritdoc |
217
|
|
|
*/ |
218
|
1 |
|
public function setType($type){ |
219
|
1 |
|
$this->type = strtoupper($type); |
220
|
1 |
|
$this->configureType(); |
221
|
1 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Configure the Curl Options based on Request Type |
226
|
|
|
*/ |
227
|
1 |
|
protected function configureType(){ |
228
|
1 |
|
switch ($this->type) { |
229
|
1 |
|
case 'POST': |
230
|
1 |
|
$this->setOption(CURLOPT_POST, TRUE); |
231
|
1 |
|
break; |
232
|
1 |
|
case 'DELETE': |
233
|
1 |
|
case 'PUT': |
234
|
1 |
|
$this->setOption(CURLOPT_CUSTOMREQUEST, $this->type); |
235
|
1 |
|
break; |
236
|
1 |
|
} |
237
|
1 |
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritdoc |
241
|
|
|
*/ |
242
|
1 |
|
public function getType(){ |
243
|
1 |
|
return $this->type; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @inheritdoc |
248
|
|
|
*/ |
249
|
1 |
|
public function reset(){ |
250
|
1 |
|
if (gettype($this->CurlRequest)=='resource'){ |
251
|
1 |
|
$this->close(); |
252
|
1 |
|
} |
253
|
1 |
|
$this->start(); |
254
|
1 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritdoc |
259
|
|
|
*/ |
260
|
2 |
|
public function start(){ |
261
|
2 |
|
$this->CurlRequest = curl_init(); |
262
|
2 |
|
$this->status = self::STATUS_INIT; |
|
|
|
|
263
|
2 |
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @inheritdoc |
268
|
|
|
*/ |
269
|
1 |
|
public function close(){ |
270
|
1 |
|
curl_close($this->CurlRequest); |
271
|
1 |
|
unset($this->CurlRequest); |
272
|
1 |
|
$this->status = self::STATUS_CLOSED; |
|
|
|
|
273
|
1 |
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @inheritdoc |
278
|
|
|
*/ |
279
|
2 |
|
public function getCurlStatus(){ |
280
|
2 |
|
return $this->status; |
281
|
|
|
} |
282
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..