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

CurlRequest::setUserAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\PdfcrowdClient\Http;
6
7
use Swis\PdfcrowdClient\Exceptions\PdfcrowdException;
8
9
class CurlRequest implements RequestInterface
10
{
11
    private $resource;
12
13
    public function __construct()
14
    {
15
        if (!function_exists('curl_init')) {
16
            throw new PdfcrowdException('Curl php extension missing');
17
        }
18
19
        $this->resource = curl_init();
20
21
        curl_setopt($this->resource, CURLOPT_HEADER, false);
22
        curl_setopt($this->resource, CURLOPT_CONNECTTIMEOUT, 10);
23
        curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, true);
24
        curl_setopt($this->resource, CURLOPT_POST, true);
25
        curl_setopt($this->resource, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
26
    }
27
28
    public function setOption($name, $value)
29
    {
30
        curl_setopt($this->resource, $name, $value);
31
    }
32
33
    public function setUserAgent(string $userAgentString)
34
    {
35
        curl_setopt($this->resource, CURLOPT_USERAGENT, $userAgentString);
36
    }
37
38
    public function setTimeout(float $timeout)
39
    {
40
        curl_setopt($this->resource, CURLOPT_TIMEOUT, $timeout);
41
    }
42
43
    public function setVerifySsl(bool $verify)
44
    {
45
        curl_setopt($this->resource, CURLOPT_SSL_VERIFYPEER, $verify);
46
    }
47
48
    public function setProxy(string $proxy, int $port)
49
    {
50
        curl_setopt($this->resource, CURLOPT_PROXY, $proxy.':'.$port);
51
    }
52
53
    public function setProxyAuth(string $username, string $password)
54
    {
55
        curl_setopt($this->resource, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
56
57
        curl_setopt($this->resource, CURLOPT_PROXYUSERPWD, $username.':'.$password);
58
    }
59
60
    public function setUrl(string $url)
61
    {
62
        curl_setopt($this->resource, CURLOPT_URL, $url);
63
    }
64
65
    public function setPort(int $port)
66
    {
67
        curl_setopt($this->resource, CURLOPT_PORT, $port);
68
    }
69
70
    public function setBody(array $body)
71
    {
72
        curl_setopt($this->resource, CURLOPT_POSTFIELDS, $body);
73
    }
74
75
    public function execute()
76
    {
77
        return curl_exec($this->resource);
78
    }
79
80
    public function getHttpStatusCode(): int
81
    {
82
        return (int) curl_getinfo($this->resource, CURLINFO_HTTP_CODE);
83
    }
84
85
    public function getErrorMessage()
86
    {
87
        return curl_error($this->resource);
88
    }
89
90
    public function getErrorNumber()
91
    {
92
        return curl_errno($this->resource);
93
    }
94
95
    public function close()
96
    {
97
        curl_close($this->resource);
98
    }
99
}
100