Completed
Pull Request — master (#73)
by Tobias
02:34
created

RequestManager::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
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
11
/**
12
 * A request manager builds a request.
13
 *
14
 * @author Tobias Nyholm <[email protected]>
15
 */
16
class RequestManager
17
{
18
    /**
19
     * @var \Http\Client\HttpClient
20
     */
21
    private $httpClient;
22
23
    /**
24
     * Send a request.
25
     *
26
     * @param string $method
27
     * @param string $uri
28
     * @param array  $headers
29
     * @param string $body
30
     * @param string $protocolVersion
31
     *
32
     * @return \Psr\Http\Message\ResponseInterface
33
     *
34
     * @throws LinkedInTransferException
35
     */
36
    public function sendRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
37
    {
38
        $request = MessageFactoryDiscovery::find()->createRequest($method, $uri, $headers, $body, $protocolVersion);
39
40
        try {
41
            return $this->getHttpClient()->sendRequest($request);
42
        } catch (TransferException $e) {
43
            throw new LinkedInTransferException('Error while requesting data from LinkedIn.com: '.$e->getMessage(), $e->getCode(), $e);
44
        }
45
    }
46
47
    /**
48
     * @param \Http\Client\HttpClient $httpClient
49
     *
50
     * @return RequestManager
51
     */
52
    public function setHttpClient(HttpClient $httpClient)
53
    {
54
        $this->httpClient = $httpClient;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return HttpClient
61
     */
62
    protected function getHttpClient()
63
    {
64
        if ($this->httpClient === null) {
65
            $this->httpClient = HttpClientDiscovery::find();
66
        }
67
68
        return $this->httpClient;
69
    }
70
}
71