| 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 |
use Psr\Http\Message\StreamFactoryInterface; |
| 13 |
use Psr\Http\Message\StreamInterface; |
| 14 |
use Psr\Http\Message\UriInterface; |
| 15 |
|
| 16 |
final class HttpMethodsClient implements HttpMethodsClientInterface |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var ClientInterface |
| 20 |
*/ |
| 21 |
private $httpClient; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var RequestFactory|RequestFactoryInterface |
| 25 |
*/ |
| 26 |
private $requestFactory; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var StreamFactoryInterface|null |
| 30 |
*/ |
| 31 |
private $streamFactory; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param RequestFactory|RequestFactoryInterface $requestFactory |
| 35 |
*/ |
| 36 |
public function __construct(ClientInterface $httpClient, $requestFactory, StreamFactoryInterface $streamFactory = null) |
| 37 |
{ |
| 38 |
if (!$requestFactory instanceof RequestFactory && !$requestFactory instanceof RequestFactoryInterface) { |
| 39 |
throw new \TypeError( |
| 40 |
sprintf('%s::__construct(): Argument #2 ($requestFactory) must be of type %s|%s, %s given', self::class, RequestFactory::class, RequestFactoryInterface::class, get_debug_type($requestFactory)) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
if (!$requestFactory instanceof RequestFactory && null === $streamFactory) { |
| 45 |
@trigger_error(sprintf('Passing a %s without a %s to %s::__construct() is deprecated as of version 2.3 and will be disallowed in version 3.0. A stream factory is required to create a request with a non-empty string body.', RequestFactoryInterface::class, StreamFactoryInterface::class, self::class)); |
| 46 |
} |
| 47 |
|
| 48 |
$this->httpClient = $httpClient; |
| 49 |
$this->requestFactory = $requestFactory; |
| 50 |
$this->streamFactory = $streamFactory; |
| 51 |
} |
| 52 |
|
| 53 |
public function get($uri, array $headers = []): ResponseInterface |
| 54 |
{ |
| 55 |
return $this->send('GET', $uri, $headers, null); |
| 56 |
} |
| 57 |
|
| 58 |
public function head($uri, array $headers = []): ResponseInterface |
| 59 |
{ |
| 60 |
return $this->send('HEAD', $uri, $headers, null); |
| 61 |
} |
| 62 |
|
| 63 |
public function trace($uri, array $headers = []): ResponseInterface |
| 64 |
{ |
| 65 |
return $this->send('TRACE', $uri, $headers, null); |
| 66 |
} |
| 67 |
|
| 68 |
public function post($uri, array $headers = [], $body = null): ResponseInterface |
| 69 |
{ |
| 70 |
return $this->send('POST', $uri, $headers, $body); |
| 71 |
} |
| 72 |
|
| 73 |
public function put($uri, array $headers = [], $body = null): ResponseInterface |
| 74 |
{ |
| 75 |
return $this->send('PUT', $uri, $headers, $body); |
| 76 |
} |
| 77 |
|
| 78 |
public function patch($uri, array $headers = [], $body = null): ResponseInterface |
| 79 |
{ |
| 80 |
return $this->send('PATCH', $uri, $headers, $body); |
| 81 |
} |
| 82 |
|
| 83 |
public function delete($uri, array $headers = [], $body = null): ResponseInterface |
| 84 |
{ |
| 85 |
return $this->send('DELETE', $uri, $headers, $body); |
| 86 |
} |
| 87 |
|
| 88 |
public function options($uri, array $headers = [], $body = null): ResponseInterface |
| 89 |
{ |
| 90 |
return $this->send('OPTIONS', $uri, $headers, $body); |
| 91 |
} |
| 92 |
|
| 93 |
public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface |
| 94 |
{ |
| 95 |
if (!is_string($uri) && !$uri instanceof UriInterface) { |
| 96 |
throw new \TypeError( |
| 97 |
sprintf('%s::send(): Argument #2 ($uri) must be of type string|%s, %s given', self::class, UriInterface::class, get_debug_type($uri)) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
if (!is_string($body) && !$body instanceof StreamInterface && null !== $body) { |
| 102 |
throw new \TypeError( |
| 103 |
sprintf('%s::send(): Argument #4 ($body) must be of type string|%s|null, %s given', self::class, StreamInterface::class, get_debug_type($body)) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
return $this->sendRequest( |
| 108 |
self::createRequest($method, $uri, $headers, $body) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param string|UriInterface $uri |
| 114 |
* @param string|StreamInterface|null $body |
| 115 |
*/ |
| 116 |
private function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface |
| 117 |
{ |
| 118 |
if ($this->requestFactory instanceof RequestFactory) { |
| 119 |
return $this->requestFactory->createRequest( |
| 120 |
$method, |
| 121 |
$uri, |
| 122 |
$headers, |
| 123 |
$body |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
$request = $this->requestFactory->createRequest($method, $uri); |
| 128 |
|
| 129 |
foreach ($headers as $key => $value) { |
| 130 |
$request = $request->withHeader($key, $value); |
| 131 |
} |
| 132 |
|
| 133 |
if (null !== $body && '' !== $body) { |
| 134 |
if (null === $this->streamFactory) { |
| 135 |
throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); |
| 136 |
} |
| 137 |
|
| 138 |
$request = $request->withBody( |
| 139 |
is_string($body) ? $this->streamFactory->createStream($body) : $body |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
return $request; |
| 144 |
} |
| 145 |
|
| 146 |
public function sendRequest(RequestInterface $request): ResponseInterface |
| 147 |
{ |
| 148 |
return $this->httpClient->sendRequest($request); |
| 149 |
} |
| 150 |
} |
| 151 |
|