ClientFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 17
c 3
b 1
f 1
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A patch() 0 3 1
A client() 0 3 1
A new() 0 6 1
A delete() 0 3 1
A __construct() 0 3 1
A post() 0 3 1
A head() 0 3 1
A put() 0 3 1
A get() 0 3 1
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