Client::request()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Postpay\HttpClients;
4
5
use Postpay\Http\Request;
6
7
class Client
8
{
9
    /**
10
     * @const int The timeout in seconds for requests.
11
     */
12
    const DEFAULT_REQUEST_TIMEOUT = 20;
13
14
    /**
15
     * @var ClientInterface HTTP client handler.
16
     */
17
    protected $clientHandler;
18
19
    /**
20
     * Instantiates a new Client object.
21
     *
22
     * @param ClientInterface|null $clientHandler
23
     */
24 14
    public function __construct(ClientInterface $clientHandler = null)
25
    {
26 14
        $this->clientHandler = $clientHandler ?: new GuzzleClient();
27 14
    }
28
29
    /**
30
     * Returns the HTTP client handler.
31
     *
32
     * @return ClientInterface
33
     */
34 2
    public function getClientHandler()
35
    {
36 2
        return $this->clientHandler;
37
    }
38
39
    /**
40
     * Sets the HTTP client handler.
41
     *
42
     * @param ClientInterface $clientHandler
43
     */
44 11
    public function setClientHandler(ClientInterface $clientHandler)
45
    {
46 11
        $this->clientHandler = $clientHandler;
47 11
    }
48
49
    /**
50
     * Sends the request to API and returns the response.
51
     *
52
     * @param Request $request
53
     *
54
     * @return \Postpay\Http\Response
55
     *
56
     * @throws \Postpay\Exceptions\PostpayException
57
     */
58 9
    public function request(Request $request)
59
    {
60 9
        $response = $this->clientHandler->send(
61 9
            $request,
62 9
            static::DEFAULT_REQUEST_TIMEOUT
63
        );
64
65 9
        if ($response->isError()) {
66 1
            throw $response->getThrownException();
67
        }
68 8
        return $response;
69
    }
70
}
71