Passed
Push — master ( 4ac306...d2aa89 )
by Mihail
05:30
created

ClientFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 19
c 3
b 1
f 1
dl 0
loc 58
ccs 24
cts 24
cp 1
rs 10
wmc 11

9 Methods

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