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
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function sendRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1') |
33
|
|
|
{ |
34
|
|
|
$request = $this->getMessageFactory()->createRequest($method, $uri, $headers, $body, $protocolVersion); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
return $this->getHttpClient()->sendRequest($request); |
38
|
|
|
} catch (TransferException $e) { |
39
|
|
|
throw new LinkedInTransferException('Error while requesting data from LinkedIn.com: '.$e->getMessage(), $e->getCode(), $e); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function setHttpClient(HttpClient $httpClient) |
47
|
|
|
{ |
48
|
|
|
$this->httpClient = $httpClient; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return HttpClient |
55
|
|
|
*/ |
56
|
|
|
protected function getHttpClient() |
57
|
|
|
{ |
58
|
|
|
if ($this->httpClient === null) { |
59
|
|
|
$this->httpClient = HttpClientDiscovery::find(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->httpClient; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param MessageFactory $messageFactory |
67
|
|
|
* |
68
|
|
|
* @return RequestManager |
69
|
|
|
*/ |
70
|
|
|
public function setMessageFactory(MessageFactory $messageFactory) |
71
|
|
|
{ |
72
|
|
|
$this->messageFactory = $messageFactory; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
* @return \Http\Message\MessageFactory |
80
|
|
|
*/ |
81
|
|
|
private function getMessageFactory() |
82
|
|
|
{ |
83
|
|
|
if ($this->messageFactory === null) { |
84
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->messageFactory; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|