-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientFactory.php
More file actions
71 lines (58 loc) · 1.95 KB
/
Copy pathClientFactory.php
File metadata and controls
71 lines (58 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Http\Client;
use Koded\Http\Interfaces\{HttpRequestClient, Request};
class ClientFactory
{
const CURL = 0;
const PHP = 1;
private int $clientType = self::CURL;
public function __construct(int $clientType = ClientFactory::CURL)
{
$this->clientType = $clientType;
}
public function get($uri, array $headers = []): HttpRequestClient
{
return $this->new(Request::GET, $uri, null, $headers);
}
public function post($uri, $body, array $headers = []): HttpRequestClient
{
return $this->new(Request::POST, $uri, $body, $headers);
}
public function put($uri, $body, array $headers = []): HttpRequestClient
{
return $this->new(Request::PUT, $uri, $body, $headers);
}
public function patch($uri, $body, array $headers = []): HttpRequestClient
{
return $this->new(Request::PATCH, $uri, $body, $headers);
}
public function delete($uri, array $headers = []): HttpRequestClient
{
return $this->new(Request::DELETE, $uri, null, $headers);
}
public function head($uri, array $headers = []): HttpRequestClient
{
return $this->new(Request::HEAD, $uri, null, $headers)->maxRedirects(0);
}
public function client(): HttpRequestClient
{
return $this->new('HEAD', '');
}
protected function new(string $method, $uri, $body = null, array $headers = []): HttpRequestClient
{
return match ($this->clientType) {
self::CURL => new CurlClient($method, $uri, $body, $headers),
self::PHP => new PhpClient($method, $uri, $body, $headers),
default => throw new \InvalidArgumentException("{$this->clientType} is not a valid HTTP client"),
};
}
}