Passed
Pull Request — master (#16)
by Mihail
14:44 queued 04:45
created

ClientFactory::client()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
/*
3
 * This file is part of the Koded package.
4
 *
5
 * (c) Mihail Binev <[email protected]>
6
 *
7
 * Please view the LICENSE distributed with this source code
8
 * for the full copyright and license information.
9
 *
10
 */
11
12
namespace Koded\Http\Client;
13
14
use Koded\Http\Interfaces\{HttpRequestClient, Request};
15
16
class ClientFactory
17
{
18
    const CURL = 0;
19
    const PHP  = 1;
20
21
    private int $clientType = self::CURL;
22
23
    public function __construct(int $clientType = ClientFactory::CURL)
24
    {
25
        $this->clientType = $clientType;
26 29
    }
27
28 29
    public function get($uri, array $headers = []): HttpRequestClient
29 29
    {
30
        return $this->new(Request::GET, $uri, null, $headers);
31 23
    }
32
33 23
    public function post($uri, $body, array $headers = []): HttpRequestClient
34
    {
35
        return $this->new(Request::POST, $uri, $body, $headers);
36 1
    }
37
38 1
    public function put($uri, $body, array $headers = []): HttpRequestClient
39
    {
40
        return $this->new(Request::PUT, $uri, $body, $headers);
41 1
    }
42
43 1
    public function patch($uri, $body, array $headers = []): HttpRequestClient
44
    {
45
        return $this->new(Request::PATCH, $uri, $body, $headers);
46 1
    }
47
48 1
    public function delete($uri, array $headers = []): HttpRequestClient
49
    {
50
        return $this->new(Request::DELETE, $uri, null, $headers);
51 1
    }
52
53 1
    public function head($uri, array $headers = []): HttpRequestClient
54
    {
55
        return $this->new(Request::HEAD, $uri, null, $headers)->maxRedirects(0);
56 1
    }
57
58 1
    public function client(): HttpRequestClient
59
    {
60
        return $this->new('HEAD', '');
61 1
    }
62
63 1
    protected function new(string $method, $uri, $body = null, array $headers = []): HttpRequestClient
64
    {
65
        return match ($this->clientType) {
66 29
            self::CURL => new CurlClient($method, $uri, $body, $headers),
67
            self::PHP => new PhpClient($method, $uri, $body, $headers),
68 29
            default => throw new \InvalidArgumentException("{$this->clientType} is not a valid HTTP client"),
69 29
        };
70 19
    }
71
}
72