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

GuzzleRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 94
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setClient() 0 4 1
A setUserAgent() 0 8 2
A setTimeout() 0 4 1
A setVerifySsl() 0 4 1
A setProxy() 0 4 1
A setProxyAuth() 0 7 1
A setUrl() 0 4 1
A setBody() 0 4 1
A setOutputDestination() 0 4 1
A execute() 0 6 1
A getHttpStatusCode() 0 4 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