Completed
Push — develop ( e91627...5ff94a )
by Barry
02:15
created

GuzzleRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 94
ccs 0
cts 57
cp 0
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
    public function __construct()
28
    {
29
        $this->client = new Client();
30
31
        $this->requestOptions[RequestOptions::CONNECT_TIMEOUT] = 10;
32
        $this->requestOptions[RequestOptions::HTTP_ERRORS] = false;
33
    }
34
35
    /**
36
     * Override the client, used for testing purposes.
37
     *
38
     * @param \GuzzleHttp\Client $client
39
     */
40
    public function setClient(Client $client)
41
    {
42
        $this->client = $client;
43
    }
44
45
    public function setUserAgent(string $userAgentString)
46
    {
47
        if (!isset($this->requestOptions[RequestOptions::HEADERS])) {
48
            $this->requestOptions[RequestOptions::HEADERS] = [];
49
        }
50
51
        $this->requestOptions[RequestOptions::HEADERS]['User-Agent'] = $userAgentString;
52
    }
53
54
    public function setTimeout(float $timeout)
55
    {
56
        $this->requestOptions[RequestOptions::TIMEOUT] = $timeout;
57
    }
58
59
    public function setVerifySsl(bool $verify)
60
    {
61
        $this->requestOptions[RequestOptions::VERIFY] = $verify;
62
    }
63
64
    public function setProxy(string $proxy, int $port)
65
    {
66
        $this->requestOptions[RequestOptions::PROXY] = $proxy.':'.$port;
67
    }
68
69
    public function setProxyAuth(string $username, string $password)
70
    {
71
        $this->requestOptions[RequestOptions::AUTH] = [
72
            $username,
73
            $password,
74
        ];
75
    }
76
77
    public function setUrl(string $url)
78
    {
79
        $this->url = $url;
80
    }
81
82
    public function setBody(array $body)
83
    {
84
        $this->requestOptions[RequestOptions::FORM_PARAMS] = $body;
85
    }
86
87
    public function setOutputDestination($output_destination)
88
    {
89
        $this->requestOptions[RequestOptions::SINK] = $output_destination;
90
    }
91
92
    public function execute()
93
    {
94
        $this->response = $this->client->request('POST', $this->url, $this->requestOptions);
95
96
        return $this->response->getBody();
97
    }
98
99
    public function getHttpStatusCode(): int
100
    {
101
        return (int) $this->response->getStatusCode();
102
    }
103
}
104