1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Http\Client\Common; |
6
|
|
|
|
7
|
|
|
use Http\Message\RequestFactory; |
8
|
|
|
use Psr\Http\Client\ClientInterface; |
9
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
final class HttpMethodsClient implements HttpMethodsClientInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ClientInterface |
17
|
|
|
*/ |
18
|
|
|
private $httpClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var RequestFactory|RequestFactoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $requestFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param RequestFactory|RequestFactoryInterface |
27
|
|
|
*/ |
28
|
8 |
View Code Duplication |
public function __construct(ClientInterface $httpClient, $requestFactory) |
|
|
|
|
29
|
|
|
{ |
30
|
8 |
|
if (!$requestFactory instanceof RequestFactory && !$requestFactory instanceof RequestFactoryInterface) { |
31
|
|
|
throw new \TypeError( |
32
|
|
|
sprintf('%s::__construct(): Argument #2 ($requestFactory) must be of type %s|%s, %s given', self::class, RequestFactory::class, RequestFactoryInterface::class, get_debug_type($requestFactory)) |
|
|
|
|
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
$this->httpClient = $httpClient; |
37
|
8 |
|
$this->requestFactory = $requestFactory; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
1 |
|
public function get($uri, array $headers = []): ResponseInterface |
41
|
|
|
{ |
42
|
1 |
|
return $this->send('GET', $uri, $headers, null); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function head($uri, array $headers = []): ResponseInterface |
46
|
|
|
{ |
47
|
1 |
|
return $this->send('HEAD', $uri, $headers, null); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function trace($uri, array $headers = []): ResponseInterface |
51
|
|
|
{ |
52
|
1 |
|
return $this->send('TRACE', $uri, $headers, null); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function post($uri, array $headers = [], $body = null): ResponseInterface |
56
|
|
|
{ |
57
|
1 |
|
return $this->send('POST', $uri, $headers, $body); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function put($uri, array $headers = [], $body = null): ResponseInterface |
61
|
|
|
{ |
62
|
1 |
|
return $this->send('PUT', $uri, $headers, $body); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function patch($uri, array $headers = [], $body = null): ResponseInterface |
66
|
|
|
{ |
67
|
1 |
|
return $this->send('PATCH', $uri, $headers, $body); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function delete($uri, array $headers = [], $body = null): ResponseInterface |
71
|
|
|
{ |
72
|
1 |
|
return $this->send('DELETE', $uri, $headers, $body); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function options($uri, array $headers = [], $body = null): ResponseInterface |
76
|
|
|
{ |
77
|
1 |
|
return $this->send('OPTIONS', $uri, $headers, $body); |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface |
81
|
|
|
{ |
82
|
8 |
|
return $this->sendRequest($this->requestFactory->createRequest( |
83
|
8 |
|
$method, |
84
|
|
|
$uri, |
85
|
|
|
$headers, |
|
|
|
|
86
|
|
|
$body |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
8 |
|
public function sendRequest(RequestInterface $request): ResponseInterface |
91
|
|
|
{ |
92
|
8 |
|
return $this->httpClient->sendRequest($request); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.