Completed
Push — develop ( 5ff94a...9cbb35 )
by Barry
01:49
created

GuzzleRequest::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\PdfcrowdClient\Http;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\RequestOptions;
9
10
class GuzzleRequest implements RequestInterface
11
{
12
    /** @var \GuzzleHttp\Client  */
13
    protected $client;
14
15
    /** @var \GuzzleHttp\Psr7\Request  */
16
    protected $request;
17
18
    /** @var array  */
19
    protected $requestOptions = [];
20
21
    /** @var \GuzzleHttp\Psr7\Response */
22
    protected $response;
23
24
    /** @var string */
25
    protected $url;
26
27 6
    public function __construct()
28
    {
29 6
        $this->client = new Client();
30
31 6
        $this->requestOptions[RequestOptions::CONNECT_TIMEOUT] = 10;
32 6
        $this->requestOptions[RequestOptions::HTTP_ERRORS] = false;
33 6
    }
34
35
    /**
36
     * Override the client, used for testing purposes.
37
     *
38
     * @param \GuzzleHttp\Client $client
39
     */
40 2
    public function setClient(Client $client)
41
    {
42 2
        $this->client = $client;
43 2
    }
44
45 2
    public function setUserAgent(string $userAgentString)
46
    {
47 2
        if (!isset($this->requestOptions[RequestOptions::HEADERS])) {
48 2
            $this->requestOptions[RequestOptions::HEADERS] = [];
49
        }
50
51 2
        $this->requestOptions[RequestOptions::HEADERS]['User-Agent'] = $userAgentString;
52 2
    }
53
54 2
    public function setTimeout(float $timeout)
55
    {
56 2
        $this->requestOptions[RequestOptions::TIMEOUT] = $timeout;
57 2
    }
58
59 2
    public function setVerifySsl(bool $verify)
60
    {
61 2
        $this->requestOptions[RequestOptions::VERIFY] = $verify;
62 2
    }
63
64 2
    public function setProxy(string $proxy, int $port)
65
    {
66 2
        $this->requestOptions[RequestOptions::PROXY] = $proxy.':'.$port;
67 2
    }
68
69 2
    public function setProxyAuth(string $username, string $password)
70
    {
71 2
        $this->requestOptions[RequestOptions::AUTH] = [
72 2
            $username,
73 2
            $password,
74
        ];
75 2
    }
76
77 2
    public function setUrl(string $url)
78
    {
79 2
        $this->url = $url;
80 2
    }
81
82 2
    public function setBody(array $body)
83
    {
84 2
        $this->requestOptions[RequestOptions::FORM_PARAMS] = $body;
85 2
    }
86
87 2
    public function setOutputDestination($output_destination)
88
    {
89 2
        $this->requestOptions[RequestOptions::SINK] = $output_destination;
90 2
    }
91
92 2
    public function execute()
93
    {
94 2
        $this->response = $this->client->request('POST', $this->url, $this->requestOptions);
95
96 2
        return $this->response->getBody();
97
    }
98
99 2
    public function getHttpStatusCode(): int
100
    {
101 2
        return (int) $this->response->getStatusCode();
102
    }
103
}
104