Completed
Push — develop ( 9cbb35...e89664 )
by Barry
01:48
created

GuzzleRequest::setOutputDestination()   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
        $this->requestOptions[RequestOptions::VERIFY] = true;
34 6
    }
35
36
    /**
37
     * Override the client, used for testing purposes.
38
     *
39
     * @param \GuzzleHttp\Client $client
40
     */
41 2
    public function setClient(Client $client)
42
    {
43 2
        $this->client = $client;
44 2
    }
45
46 2
    public function setUserAgent(string $userAgentString)
47
    {
48 2
        if (!isset($this->requestOptions[RequestOptions::HEADERS])) {
49 2
            $this->requestOptions[RequestOptions::HEADERS] = [];
50
        }
51
52 2
        $this->requestOptions[RequestOptions::HEADERS]['User-Agent'] = $userAgentString;
53 2
    }
54
55 2
    public function setTimeout(float $timeout)
56
    {
57 2
        $this->requestOptions[RequestOptions::TIMEOUT] = $timeout;
58 2
    }
59
60 2
    public function setProxy(string $proxy, int $port)
61
    {
62 2
        $this->requestOptions[RequestOptions::PROXY] = $proxy.':'.$port;
63 2
    }
64
65 2
    public function setProxyAuth(string $username, string $password)
66
    {
67 2
        $this->requestOptions[RequestOptions::AUTH] = [
68 2
            $username,
69 2
            $password,
70
        ];
71 2
    }
72
73 2
    public function setUrl(string $url)
74
    {
75 2
        $this->url = $url;
76 2
    }
77
78 2
    public function setBody(array $body)
79
    {
80 2
        $this->requestOptions[RequestOptions::FORM_PARAMS] = $body;
81 2
    }
82
83 2
    public function setOutputDestination($output_destination)
84
    {
85 2
        $this->requestOptions[RequestOptions::SINK] = $output_destination;
86 2
    }
87
88 2
    public function execute()
89
    {
90 2
        $this->response = $this->client->request('POST', $this->url, $this->requestOptions);
91
92 2
        return $this->response->getBody();
93
    }
94
95 2
    public function getHttpStatusCode(): int
96
    {
97 2
        return (int) $this->response->getStatusCode();
98
    }
99
}
100