Client   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 55
ccs 13
cts 13
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendRequest() 0 8 3
A __construct() 0 4 3
A request() 0 10 1
1
<?php
2
3
namespace Omnipay\Common\Http;
4
5
use function GuzzleHttp\Psr7\str;
6
use Http\Client\HttpClient;
7
use Http\Discovery\HttpClientDiscovery;
8
use Http\Discovery\MessageFactoryDiscovery;
9
use Http\Message\RequestFactory;
10
use Omnipay\Common\Http\Exception\NetworkException;
11
use Omnipay\Common\Http\Exception\RequestException;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\StreamInterface;
15
use Psr\Http\Message\UriInterface;
16
17
class Client implements ClientInterface
18
{
19
    /**
20
     * The Http Client which implements `public function sendRequest(RequestInterface $request)`
21
     * Note: Will be changed to PSR-18 when released
22
     *
23
     * @var HttpClient
24
     */
25
    private $httpClient;
26
27
    /**
28
     * @var RequestFactory
29
     */
30
    private $requestFactory;
31
32 255
    public function __construct($httpClient = null, RequestFactory $requestFactory = null)
33
    {
34 255
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
35 255
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
36 255
    }
37
38
    /**
39
     * @param $method
40
     * @param $uri
41
     * @param array $headers
42
     * @param string|array|resource|StreamInterface|null $body
43
     * @param string $protocolVersion
44
     * @return ResponseInterface
45
     * @throws \Http\Client\Exception
46
     */
47 12
    public function request(
48
        $method,
49
        $uri,
50
        array $headers = [],
51
        $body = null,
52
        $protocolVersion = '1.1'
53
    ) {
54 12
        $request = $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
55
56 12
        return $this->sendRequest($request);
57
    }
58
59
    /**
60
     * @param RequestInterface $request
61
     * @return ResponseInterface
62
     * @throws \Http\Client\Exception
63
     */
64 12
    private function sendRequest(RequestInterface $request)
65
    {
66
        try {
67 12
            return $this->httpClient->sendRequest($request);
68 9
        } catch (\Http\Client\Exception\NetworkException $networkException) {
69 3
            throw new NetworkException($networkException->getMessage(), $request, $networkException);
70 6
        } catch (\Exception $exception) {
71 6
            throw new RequestException($exception->getMessage(), $request, $exception);
72
        }
73
    }
74
}
75