diff --git a/composer.json b/composer.json index bbbcd62..76b0088 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ ], "require": { "php": ">=5.4", - "php-http/client": "dev-utility_separation" + "php-http/client": "dev-utility_separation", + "php-http/message-factory": "dev-master@dev" }, "require-dev": { "phpspec/phpspec": "^2.2", diff --git a/spec/HttpMethodsSpec.php b/spec/HttpMethodsSpec.php index 005a5ba..eecf5f5 100644 --- a/spec/HttpMethodsSpec.php +++ b/spec/HttpMethodsSpec.php @@ -2,78 +2,75 @@ namespace spec\Http\Client\Util; -use Http\Client\Util\HttpMethods; -use Http\Client\HttpMethodsClient; +use Http\Client\Util\HttpMethodsClient; use PhpSpec\ObjectBehavior; class HttpMethodsSpec extends ObjectBehavior { function let() { - $this->beAnInstanceOf('spec\Http\Client\HttpMethodsStub'); + $this->beAnInstanceOf('spec\Http\Client\Util\HttpMethodsClientStub'); } function it_sends_a_get_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->get($data['uri'], $data['headers'], $data['options'])->shouldReturn(true); } function it_sends_a_head_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->head($data['uri'], $data['headers'], $data['options'])->shouldReturn(true); } function it_sends_a_trace_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->trace($data['uri'], $data['headers'], $data['options'])->shouldReturn(true); } function it_sends_a_post_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->post($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); } function it_sends_a_put_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->put($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); } function it_sends_a_patch_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->patch($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); } function it_sends_a_delete_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->delete($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); } function it_sends_a_options_request() { - $data = HttpMethodsStub::$requestData; + $data = HttpMethodsClientStub::$requestData; $this->options($data['uri'], $data['headers'], $data['body'], $data['options'])->shouldReturn(true); } } -class HttpMethodsStub implements HttpMethodsClient +class HttpMethodsClientStub extends HttpMethodsClient { - use HttpMethods; - public static $requestData = [ 'uri' => '/uri', 'headers' => [ @@ -88,7 +85,7 @@ class HttpMethodsStub implements HttpMethodsClient /** * {@inheritdoc} */ - public function send($method, $uri, array $headers = [], $body = null, array $options = []) + protected function send($method, $uri, array $headers = [], $body = null, array $options = []) { if (in_array($method, ['GET', 'HEAD', 'TRACE'])) { return $uri === self::$requestData['uri'] && diff --git a/src/HttpMethods.php b/src/HttpMethods.php deleted file mode 100644 index e3563c2..0000000 --- a/src/HttpMethods.php +++ /dev/null @@ -1,82 +0,0 @@ - - */ -trait HttpMethods -{ - /** - * {@inheritdoc} - */ - public function get($uri, array $headers = [], array $options = []) - { - return $this->send('GET', $uri, $headers, null, $options); - } - - /** - * {@inheritdoc} - */ - public function head($uri, array $headers = [], array $options = []) - { - return $this->send('HEAD', $uri, $headers, null, $options); - } - - /** - * {@inheritdoc} - */ - public function trace($uri, array $headers = [], array $options = []) - { - return $this->send('TRACE', $uri, $headers, null, $options); - } - - /** - * {@inheritdoc} - */ - public function post($uri, array $headers = [], $body = null, array $options = []) - { - return $this->send('POST', $uri, $headers, $body, $options); - } - - /** - * {@inheritdoc} - */ - public function put($uri, array $headers = [], $body = null, array $options = []) - { - return $this->send('PUT', $uri, $headers, $body, $options); - } - - /** - * {@inheritdoc} - */ - public function patch($uri, array $headers = [], $body = null, array $options = []) - { - return $this->send('PATCH', $uri, $headers, $body, $options); - } - - /** - * {@inheritdoc} - */ - public function delete($uri, array $headers = [], $body = null, array $options = []) - { - return $this->send('DELETE', $uri, $headers, $body, $options); - } - - /** - * {@inheritdoc} - */ - public function options($uri, array $headers = [], $body = null, array $options = []) - { - return $this->send('OPTIONS', $uri, $headers, $body, $options); - } - - /** - * {@inheritdoc} - */ - abstract public function send($method, $uri, array $headers = [], $body = null, array $options = []); -} diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php new file mode 100644 index 0000000..678ebe0 --- /dev/null +++ b/src/HttpMethodsClient.php @@ -0,0 +1,195 @@ +get('/foo') + * ->post('/bar') + * ; + * + * @author Márk Sági-Kazár + * @author David Buchmann + */ +class HttpMethodsClient +{ + /** + * @var HttpClient + */ + private $httpClient; + + /** + * @var MessageFactory + */ + private $messageFactory; + + /** + * Instantiate the message client with a client and a message factory. + * + * @param HttpClient $httpClient The client to send requests with. + * @param MessageFactory $messageFactory The message factory to create requests. + */ + public function __construct(HttpClient $httpClient, MessageFactory $messageFactory) + { + $this->httpClient = $httpClient; + $this->messageFactory = $messageFactory; + } + + /** + * Sends a GET request + * + * @param string|UriInterface $uri + * @param array $headers + * + * @throws Exception + * + * @return ResponseInterface + */ + public function get($uri, array $headers = []) + { + return $this->send('GET', $uri, $headers, null); + } + + /** + * Sends an HEAD request + * + * @param string|UriInterface $uri + * @param array $headers + * + * @throws Exception + * + * @return ResponseInterface + */ + public function head($uri, array $headers = []) + { + return $this->send('HEAD', $uri, $headers, null); + } + + /** + * Sends a TRACE request + * + * @param string|UriInterface $uri + * @param array $headers + * + * @throws Exception + * + * @return ResponseInterface + */ + public function trace($uri, array $headers = []) + { + return $this->send('TRACE', $uri, $headers, null); + } + + /** + * Sends a POST request + * + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body + * + * @throws Exception + * + * @return ResponseInterface + */ + public function post($uri, array $headers = [], $body = null) + { + return $this->send('POST', $uri, $headers, $body); + } + + /** + * Sends a PUT request + * + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body + * + * @throws Exception + * + * @return ResponseInterface + */ + public function put($uri, array $headers = [], $body = null) + { + return $this->send('PUT', $uri, $headers, $body); + } + + /** + * Sends a PATCH request + * + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body + * + * @throws Exception + * + * @return ResponseInterface + */ + public function patch($uri, array $headers = [], $body = null) + { + return $this->send('PATCH', $uri, $headers, $body); + } + + /** + * Sends a DELETE request + * + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body + * + * @throws Exception + * + * @return ResponseInterface + */ + public function delete($uri, array $headers = [], $body = null) + { + return $this->send('DELETE', $uri, $headers, $body); + } + + /** + * Sends an OPTIONS request + * + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body + * + * @throws Exception + * + * @return ResponseInterface + */ + public function options($uri, array $headers = [], $body = null) + { + return $this->send('OPTIONS', $uri, $headers, $body); + } + + /** + * Sends a request + * + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body + * + * @throws Exception + * + * @return ResponseInterface + */ + protected function send($method, $uri, array $headers = [], $body = null) + { + return $this->httpClient->sendRequest($this->messageFactory->createRequest( + $method, + $uri, + '1.1', + $headers, + $body + )); + } +}