Completed
Push — develop ( 264d47...05a799 )
by Barry
02:39
created

GuzzleRequest::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
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 int */
25
    protected $port;
26
27
    /** @var string */
28
    protected $url;
29
30 2
    public function __construct()
31
    {
32 2
        $this->client = new Client();
33
34 2
        $this->requestOptions[RequestOptions::CONNECT_TIMEOUT] = 10;
35 2
        $this->requestOptions[RequestOptions::HTTP_ERRORS] = false;
36 2
    }
37
38
    public function setOption($name, $value)
39
    {
40
        // todo: implement
41
    }
42
43
    public function setUserAgent(string $userAgentString)
44
    {
45
        if (!isset($this->requestOptions[RequestOptions::HEADERS])) {
46
            $this->requestOptions[RequestOptions::HEADERS] = [];
47
        }
48
49
        $this->requestOptions[RequestOptions::HEADERS]['User-Agent'] = $userAgentString;
50
    }
51
52
    public function setTimeout(float $timeout)
53
    {
54
        $this->requestOptions[RequestOptions::TIMEOUT] = $timeout;
55
    }
56
57
    public function setVerifySsl(bool $verify)
58
    {
59
        $this->requestOptions[RequestOptions::VERIFY] = $verify;
60
    }
61
62
    public function setProxy(string $proxy, int $port)
63
    {
64
        $this->requestOptions[RequestOptions::PROXY] = $proxy.':'.$port;
65
    }
66
67
    public function setProxyAuth(string $username, string $password)
68
    {
69
        $this->requestOptions[RequestOptions::AUTH] = [
70
            $username,
71
            $password,
72
        ];
73
    }
74
75
    public function setUrl(string $url)
76
    {
77
        $this->url = $url;
78
    }
79
80
    public function setPort(int $port)
81
    {
82
        $this->port = $port;
83
    }
84
85
    public function setBody(array $body)
86
    {
87
        $this->requestOptions[RequestOptions::FORM_PARAMS] = $body;
88
    }
89
90
    public function execute()
91
    {
92
        $this->response = $this->client->request('POST', $this->url, $this->requestOptions);
93
94
        return $this->response->getBody();
95
    }
96
97
    public function getHttpStatusCode(): int
98
    {
99
        return (int) $this->response->getStatusCode();
100
    }
101
102
    public function getErrorMessage()
103
    {
104
        try {
105
            return (string) $this->response;
106
        } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...urn 'Unknown error.'; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
107
            return 'Unknown error.';
108
        }
109
    }
110
111
    public function getErrorNumber()
112
    {
113
        return (int) $this->response->getStatusCode();
114
    }
115
116
    public function close()
117
    {
118
        // todo: this method should be deleted once we our done with Curl
119
    }
120
}
121