Completed
Push — master ( 528705...013ef7 )
by Radu
02:04
created

AbstractClient::setMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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