|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\LinkedIn\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Happyr\LinkedIn\Exception\LinkedInTransferException; |
|
6
|
|
|
use Http\Client\Exception\TransferException; |
|
7
|
|
|
use Http\Client\HttpClient; |
|
8
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
9
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
10
|
|
|
use Http\Message\MessageFactory; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A class to create HTTP requests and to send them. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class RequestManager implements RequestManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Http\Client\HttpClient |
|
21
|
|
|
*/ |
|
22
|
|
|
private $httpClient; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Http\Message\MessageFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
private $messageFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Psr\Http\Message\RequestInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $lastRequest; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \Psr\Http\Message\ResponseInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $lastResponse; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getLastRequest() { |
|
45
|
|
|
return $this->lastRequest; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getLastResponse() { |
|
52
|
|
|
return $this->lastResponse; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function sendRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') |
|
59
|
|
|
{ |
|
60
|
|
|
$request = $this->getMessageFactory()->createRequest($method, $uri, $headers, $body, $protocolVersion); |
|
61
|
|
|
|
|
62
|
|
|
$this->lastRequest = $request; |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$response = $this->getHttpClient()->sendRequest($request); |
|
66
|
|
|
$this->lastResponse = $response; |
|
67
|
|
|
return $response; |
|
68
|
|
|
} catch (TransferException $e) { |
|
69
|
|
|
throw new LinkedInTransferException('Error while requesting data from LinkedIn.com: '.$e->getMessage(), $e->getCode(), $e); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setHttpClient(HttpClient $httpClient) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->httpClient = $httpClient; |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return HttpClient |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getHttpClient() |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->httpClient === null) { |
|
89
|
|
|
$this->httpClient = HttpClientDiscovery::find(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->httpClient; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param MessageFactory $messageFactory |
|
97
|
|
|
* |
|
98
|
|
|
* @return RequestManager |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setMessageFactory(MessageFactory $messageFactory) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->messageFactory = $messageFactory; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return \Http\Message\MessageFactory |
|
109
|
|
|
*/ |
|
110
|
|
|
private function getMessageFactory() |
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->messageFactory === null) { |
|
113
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this->messageFactory; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|