Completed
Push — master ( 6acfe1...0042b2 )
by Radu
11:51 queued 10s
created

AbstractClient::setRequestData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Http;
6
7
use WebServCo\Framework\Exceptions\HttpClientException;
8
9
abstract class AbstractClient
10
{
11
12
    protected bool $debug;
13
14
    protected string $method;
15
16
    protected string $requestContentType;
17
18
    /**
19
    * Request data
20
    *
21
    * @var array<string,string>|string
22
    */
23
    protected $requestData;
24
25
    /**
26
    * Request headers
27
    *
28
    * @var array<string,mixed>
29
    */
30
    protected array $requestHeaders;
31
32
    /**
33
    * Resposne headers
34
    *
35
    * @var array<int,array<string,mixed>>
36
    */
37
    protected array $responseHeaders;
38
39
    protected bool $skipSslVerification;
40
41
    abstract public function retrieve(string $url): Response;
42
43
    public function get(string $url): Response
44
    {
45
        $this->setMethod(Method::GET);
46
        return $this->retrieve($url);
47
    }
48
49
    /**
50
    * @return array<string,string>
51
    */
52
    public function getRequestHeaders(): array
53
    {
54
        return $this->requestHeaders;
55
    }
56
57
    /**
58
    * @return array<int,array<string,mixed>>
59
    */
60
    public function getResponseHeaders(): array
61
    {
62
        return $this->responseHeaders;
63
    }
64
65
    public function head(string $url): Response
66
    {
67
        $this->setMethod(Method::HEAD);
68
        return $this->retrieve($url);
69
    }
70
71
    /**
72
    * @param array<mixed>|string $data
73
    */
74
    public function post(string $url, $data = null): Response
75
    {
76
        $this->setMethod(Method::POST);
77
        if (!empty($data)) {
78
            $this->setRequestData($data);
79
        }
80
        return $this->retrieve($url);
81
    }
82
83
    public function setDebug(bool $debug): bool
84
    {
85
        $this->debug = $debug;
86
        return true;
87
    }
88
89
    public function setMethod(string $method): bool
90
    {
91
        if (!\in_array($method, Method::getSupported(), true)) {
92
            throw new HttpClientException('Unsupported method.');
93
        }
94
        $this->method = $method;
95
        return true;
96
    }
97
98
    /**
99
    * @param array<string,mixed>|string $data
100
    */
101
    public function setRequestData($data): bool
102
    {
103
        if (\is_array($data)) {
104
            $this->requestData = [];
105
            foreach ($data as $key => $value) {
106
                if (\is_array($value)) {
107
                    throw new \InvalidArgumentException('Request data value can not be an array.');
108
                }
109
                $this->requestData[$key] = $value;
110
            }
111
            return true;
112
        }
113
        $this->requestData = $data;
114
        return true;
115
    }
116
117
    public function setRequestContentType(string $contentType): bool
118
    {
119
        $this->requestContentType = $contentType;
120
        return true;
121
    }
122
123
    public function setRequestHeader(string $name, string $value): bool
124
    {
125
        $this->requestHeaders[$name] = $value;
126
        return true;
127
    }
128
129
    public function setSkipSSlVerification(bool $skipSslVerification): bool
130
    {
131
        $this->skipSslVerification = $skipSslVerification;
132
        return true;
133
    }
134
}
135