1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GinoPane\NanoRest; |
4
|
|
|
|
5
|
|
|
define(__NAMESPACE__ . '\ROOT_DIRECTORY', dirname(__FILE__, 2)); |
6
|
|
|
|
7
|
|
|
use GinoPane\NanoRest\{ |
8
|
|
|
Request\RequestContext, |
9
|
|
|
Supplemental\CurlHelper, |
10
|
|
|
Response\ResponseContextAbstract, |
11
|
|
|
Exceptions\TransportException, |
12
|
|
|
Exceptions\ResponseContextException |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class NanoRest |
17
|
|
|
* |
18
|
|
|
* Abstract implementation of transport layer |
19
|
|
|
* |
20
|
|
|
* @author Sergey <Gino Pane> Karavay |
21
|
|
|
*/ |
22
|
|
|
class NanoRest |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* CurlHelper for handling curl-specific logic |
26
|
|
|
* |
27
|
|
|
* @var CurlHelper |
28
|
|
|
*/ |
29
|
|
|
private $curlHelper = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* NanoRest constructor |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->curlHelper = new CurlHelper(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Send previously prepared request |
41
|
|
|
* |
42
|
|
|
* @param RequestContext $requestContext |
43
|
|
|
* |
44
|
|
|
* @throws TransportException |
45
|
|
|
* @throws ResponseContextException |
46
|
|
|
* |
47
|
|
|
* @return ResponseContextAbstract |
48
|
|
|
*/ |
49
|
|
|
public function sendRequest( |
50
|
|
|
RequestContext $requestContext |
51
|
|
|
): ResponseContextAbstract { |
52
|
|
|
$curlHandle = $this->curlHelper->getRequestHandle($requestContext); |
53
|
|
|
|
54
|
|
|
$responseContext = $requestContext->getResponseContextObject(); |
55
|
|
|
|
56
|
|
|
list( |
57
|
|
|
'httpStatus' => $status, |
58
|
|
|
'response' => $content, |
59
|
|
|
'headers' => $headers |
60
|
|
|
) = $this->curlHelper->executeRequestHandle($curlHandle); |
61
|
|
|
|
62
|
|
|
$responseContext->setRequestContext($requestContext) |
63
|
|
|
->setHttpStatusCode((int)$status); |
64
|
|
|
|
65
|
|
|
if ($content) { |
66
|
|
|
$responseContext->setContent($content); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$responseContext->headers()->setHeadersFromString($headers); |
70
|
|
|
|
71
|
|
|
return $responseContext; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|