diff --git a/composer.json b/composer.json index d712c94..2968387 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ ], "require": { "php": ">=5.4.0", - "egeloen/http-adapter": "0.6.*@dev" + "php-http/httplug": "^1.0", + "php-http/message-factory": "^1.0" }, "require-dev": { "tedivm/stash": "dev-master", @@ -27,6 +28,8 @@ "henrikbjorn/phpspec-code-coverage" : "~1.0" }, "suggest": { + "php-http/client-implementation": "An HTTP Client implementation", + "php-http/message": "Provides Message Factory implementations", "tedivm/stash": "Used for caching URLs" }, "autoload": { diff --git a/spec/Provider/BitlySpec.php b/spec/Provider/BitlySpec.php index 51a715a..5705b61 100644 --- a/spec/Provider/BitlySpec.php +++ b/spec/Provider/BitlySpec.php @@ -2,16 +2,18 @@ namespace spec\Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Psr\Http\Message\IncomingResponseInterface as Response; +use Http\Client\HttpClient; +use Http\Message\RequestFactory; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; class BitlySpec extends ObjectBehavior { - function let(HttpAdapterInterface $adapter) + function let(HttpClient $httpClient, RequestFactory $requestFactory) { - $this->beConstructedWith($adapter, 'access_token'); + $this->beConstructedWith('access_token', $httpClient, $requestFactory); } function it_is_initializable() @@ -24,18 +26,46 @@ function it_is_a_provider() $this->shouldImplement('Concise\Provider'); } - function it_shortens_a_url(HttpAdapterInterface $adapter, Response $response) - { - $response->getBody()->willReturn('{"data": {"url": "http://bit.ly/shortened"}}'); - $adapter->get(Argument::type('string'))->willReturn($response); + function it_shortens_a_url( + RequestFactory $requestFactory, + RequestInterface $request, + HttpClient $httpClient, + ResponseInterface $response, + StreamInterface $body + ) { + $requestFactory + ->createRequest( + 'GET', + 'https://api-ssl.bitly.com/v3/shorten?access_token=access_token&longUrl='.urlencode('http://any.url') + ) + ->willReturn($request); + ; + + $httpClient->sendRequest($request)->willReturn($response); + $response->getBody()->willReturn($body); + $body->__toString()->willReturn('{"data": {"url": "http://bit.ly/shortened"}}'); $this->shorten('http://any.url')->shouldReturn("http://bit.ly/shortened"); } - function it_expands_a_url(HttpAdapterInterface $adapter, Response $response) - { - $response->getBody()->willReturn('{"data": {"expand": [{"long_url": "http://any.url"}]}}'); - $adapter->get(Argument::type('string'))->willReturn($response); + function it_expands_a_url( + RequestFactory $requestFactory, + RequestInterface $request, + HttpClient $httpClient, + ResponseInterface $response, + StreamInterface $body + ) { + $requestFactory + ->createRequest( + 'GET', + 'https://api-ssl.bitly.com/v3/expand?access_token=access_token&shortUrl='.urlencode('http://bit.ly/shortened') + ) + ->willReturn($request); + ; + + $httpClient->sendRequest($request)->willReturn($response); + $response->getBody()->willReturn($body); + $body->__toString()->willReturn('{"data": {"expand": [{"long_url": "http://any.url"}]}}'); $this->expand('http://bit.ly/shortened')->shouldReturn("http://any.url"); } diff --git a/spec/Provider/GoogleSpec.php b/spec/Provider/GoogleSpec.php index 0dd8092..d8bab13 100644 --- a/spec/Provider/GoogleSpec.php +++ b/spec/Provider/GoogleSpec.php @@ -2,16 +2,18 @@ namespace spec\Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Psr\Http\Message\IncomingResponseInterface as Response; +use Http\Client\HttpClient; +use Http\Message\RequestFactory; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; class GoogleSpec extends ObjectBehavior { - function let(HttpAdapterInterface $adapter) + function let(HttpClient $httpClient, RequestFactory $requestFactory) { - $this->beConstructedWith($adapter); + $this->beConstructedWith($httpClient, $requestFactory); } function it_is_initializable() @@ -24,18 +26,51 @@ function it_is_a_provider() $this->shouldImplement('Concise\Provider'); } - function it_shortens_a_url(HttpAdapterInterface $adapter, Response $response) - { - $response->getBody()->willReturn('{"id": "http://goo.gl/shortened"}'); - $adapter->post(Argument::type('string'), Argument::type('array'), Argument::type('string'))->willReturn($response); + function it_shortens_a_url( + RequestFactory $requestFactory, + RequestInterface $request, + HttpClient $httpClient, + ResponseInterface $response, + StreamInterface $body + ) { + $requestFactory + ->createRequest( + 'POST', + 'https://www.googleapis.com/urlshortener/v1/url', + ['Content-Type' => 'application/json'], + json_encode([ + 'key' => null, + 'longUrl' => 'http://any.url', + ]) + ) + ->willReturn($request); + ; + + $httpClient->sendRequest($request)->willReturn($response); + $response->getBody()->willReturn($body); + $body->__toString()->willReturn('{"id": "http://goo.gl/shortened"}'); $this->shorten('http://any.url')->shouldReturn("http://goo.gl/shortened"); } - function it_expands_a_url(HttpAdapterInterface $adapter, Response $response) - { - $response->getBody()->willReturn('{"longUrl": "http://any.url"}'); - $adapter->get(Argument::type('string'))->willReturn($response); + function it_expands_a_url( + RequestFactory $requestFactory, + RequestInterface $request, + HttpClient $httpClient, + ResponseInterface $response, + StreamInterface $body + ) { + $requestFactory + ->createRequest( + 'GET', + 'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.urlencode('http://goo.gl/shortened') + ) + ->willReturn($request); + ; + + $httpClient->sendRequest($request)->willReturn($response); + $response->getBody()->willReturn($body); + $body->__toString()->willReturn('{"longUrl": "http://any.url"}'); $this->expand('http://goo.gl/shortened')->shouldReturn("http://any.url"); } diff --git a/spec/Provider/TinyccSpec.php b/spec/Provider/TinyccSpec.php index 3dfe04e..f637e6b 100644 --- a/spec/Provider/TinyccSpec.php +++ b/spec/Provider/TinyccSpec.php @@ -2,16 +2,26 @@ namespace spec\Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Psr\Http\Message\IncomingResponseInterface as Response; +use Http\Client\HttpClient; +use Http\Message\RequestFactory; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; class TinyccSpec extends ObjectBehavior { - function let(HttpAdapterInterface $adapter) + private $params = [ + 'c' => 'rest_api', + 'version' => '2.0.3', + 'format' => 'json', + 'login' => 'login', + 'apiKey' => 'apiKey', + ]; + + function let(HttpClient $httpClient, RequestFactory $requestFactory) { - $this->beConstructedWith($adapter, 'login', 'apiKey'); + $this->beConstructedWith('login', 'apiKey', $httpClient, $requestFactory); } function it_is_initializable() @@ -24,18 +34,55 @@ function it_is_a_provider() $this->shouldImplement('Concise\Provider'); } - function it_shortens_a_url(HttpAdapterInterface $adapter, Response $response) - { - $response->getBody()->willReturn('{"results": {"short_url": "http://tiny.cc/shortened"}}'); - $adapter->get(Argument::type('string'))->willReturn($response); + function it_shortens_a_url( + RequestFactory $requestFactory, + RequestInterface $request, + HttpClient $httpClient, + ResponseInterface $response, + StreamInterface $body + ) { + $params = array_merge($this->params, [ + 'm' => 'shorten', + 'longUrl' => 'http://any.url', + ]); + $requestFactory + ->createRequest( + 'GET', + sprintf('http://tiny.cc?%s', http_build_query($params)) + ) + ->willReturn($request); + ; + + $httpClient->sendRequest($request)->willReturn($response); + $response->getBody()->willReturn($body); + $body->__toString()->willReturn('{"results": {"short_url": "http://tiny.cc/shortened"}}'); $this->shorten('http://any.url')->shouldReturn("http://tiny.cc/shortened"); } - function it_expands_a_url(HttpAdapterInterface $adapter, Response $response) - { - $response->getBody()->willReturn('{"results": {"longUrl": "http://any.url"}}'); - $adapter->get(Argument::type('string'))->willReturn($response); + function it_expands_a_url( + RequestFactory $requestFactory, + RequestInterface $request, + HttpClient $httpClient, + ResponseInterface $response, + StreamInterface $body + ) { + $params = array_merge($this->params, [ + 'm' => 'expand', + 'shortUrl' => 'http://tiny.cc/shortened', + ]); + + $requestFactory + ->createRequest( + 'GET', + sprintf('http://tiny.cc?%s', http_build_query($params)) + ) + ->willReturn($request); + ; + + $httpClient->sendRequest($request)->willReturn($response); + $response->getBody()->willReturn($body); + $body->__toString()->willReturn('{"results": {"longUrl": "http://any.url"}}'); $this->expand('http://tiny.cc/shortened')->shouldReturn("http://any.url"); } diff --git a/src/Provider/Bitly.php b/src/Provider/Bitly.php index ecbd86a..b6850e4 100644 --- a/src/Provider/Bitly.php +++ b/src/Provider/Bitly.php @@ -11,12 +11,14 @@ namespace Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Concise\Provider; +use Http\Client\HttpClient; +use Http\Message\RequestFactory; /** * @author Márk Sági-Kazár */ -class Bitly extends HttpAdapterAware +class Bitly implements Provider { /** * @var string @@ -26,17 +28,28 @@ class Bitly extends HttpAdapterAware /** * @var string */ - protected $accessToken; + private $accessToken; /** - * @param HttpAdapterInterface $adapter - * @param string $accessToken + * @var HttpClient */ - public function __construct(HttpAdapterInterface $adapter, $accessToken) - { - parent::__construct($adapter); + private $httpClient; + + /** + * @var RequestFactory + */ + private $requestFactory; + /** + * @param string $accessToken + * @param HttpClient $httpClient + * @param RequestFactory $requestFactory + */ + public function __construct($accessToken, HttpClient $httpClient, RequestFactory $requestFactory) + { $this->accessToken = $accessToken; + $this->httpClient = $httpClient; + $this->requestFactory = $requestFactory; } /** @@ -49,8 +62,11 @@ public function shorten($url) 'longUrl' => trim($url), ])); - $response = $this->adapter->get($url); - $response = json_decode($response->getBody()); + $request = $this->requestFactory->createRequest('GET', $url); + + $response = $this->httpClient->sendRequest($request); + + $response = json_decode((string) $response->getBody()); return $response->data->url; } @@ -65,8 +81,11 @@ public function expand($url) 'shortUrl' => trim($url), ])); - $response = $this->adapter->get($url); - $response = json_decode($response->getBody()); + $request = $this->requestFactory->createRequest('GET', $url); + + $response = $this->httpClient->sendRequest($request); + + $response = json_decode((string) $response->getBody()); return $response->data->expand[0]->long_url; } diff --git a/src/Provider/Google.php b/src/Provider/Google.php index 7b553d2..0aa63eb 100644 --- a/src/Provider/Google.php +++ b/src/Provider/Google.php @@ -11,31 +11,44 @@ namespace Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Concise\Provider; +use Http\Client\HttpClient; +use Http\Message\RequestFactory; /** * @author Antoine Corcy */ -class Google extends HttpAdapterAware +class Google implements Provider { /** * @var string */ const ENDPOINT = 'https://www.googleapis.com/urlshortener/v1/url'; + /** + * @var HttpClient + */ + private $httpClient; + + /** + * @var RequestFactory + */ + private $requestFactory; + /** * @var string|null */ - protected $apiKey; + private $apiKey; /** - * @param HttpAdapterInterface $adapter - * @param string|null $apiKey + * @param HttpClient $httpClient + * @param RequestFactory $requestFactory + * @param string|null $apiKey */ - public function __construct(HttpAdapterInterface $adapter, $apiKey = null) + public function __construct(HttpClient $httpClient, RequestFactory $requestFactory, $apiKey = null) { - parent::__construct($adapter); - + $this->httpClient = $httpClient; + $this->requestFactory = $requestFactory; $this->apiKey = $apiKey; } @@ -51,8 +64,11 @@ public function shorten($url) 'longUrl' => $url, ]); - $response = $this->adapter->post(self::ENDPOINT, $headers, $body); - $response = json_decode($response->getBody()); + $request = $this->requestFactory->createRequest('POST', self::ENDPOINT, $headers, $body); + + $response = $this->httpClient->sendRequest($request); + + $response = json_decode((string) $response->getBody()); return $response->id; } @@ -67,8 +83,11 @@ public function expand($url) 'shortUrl' => $url, ])); - $response = $this->adapter->get($url); - $response = json_decode($response->getBody()); + $request = $this->requestFactory->createRequest('GET', $url); + + $response = $this->httpClient->sendRequest($request); + + $response = json_decode((string) $response->getBody()); return $response->longUrl; } diff --git a/src/Provider/HttpAdapterAware.php b/src/Provider/HttpAdapterAware.php deleted file mode 100644 index 2841acb..0000000 --- a/src/Provider/HttpAdapterAware.php +++ /dev/null @@ -1,34 +0,0 @@ - -* -* For the full copyright and license information, please view the LICENSE -* file that was distributed with this source code. -*/ - -namespace Concise\Provider; - -use Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; - -/** - * @author Márk Sági-Kazár - */ -abstract class HttpAdapterAware implements Provider -{ - /** - * @var HttpAdapterInterface - */ - protected $adapter; - - /** - * @param HttpAdapterInterface $adapter - */ - public function __construct(HttpAdapterInterface $adapter) - { - $this->adapter = $adapter; - } -} diff --git a/src/Provider/Tinycc.php b/src/Provider/Tinycc.php index 65e7e08..9ecc0c8 100644 --- a/src/Provider/Tinycc.php +++ b/src/Provider/Tinycc.php @@ -11,48 +11,42 @@ namespace Concise\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Concise\Provider; +use Http\Client\HttpClient; +use Http\Message\RequestFactory; /** * @author Márk Sági-Kazár */ -class Tinycc extends HttpAdapterAware +class Tinycc implements Provider { /** * @var string */ const ENDPOINT = 'http://tiny.cc'; - /** - * @var string - */ - protected $login; - - /** - * @var string - */ - protected $apiKey; - /** * @var array */ - protected $defaultParams = [ + private $params = [ 'c' => 'rest_api', 'version' => '2.0.3', 'format' => 'json', ]; /** - * @param HttpAdapterInterface $adapter - * @param string $login - * @param string $apiKey + * @param string $login + * @param string $apiKey + * @param HttpClient $httpClient + * @param RequestFactory $requestFactory */ - public function __construct(HttpAdapterInterface $adapter, $login, $apiKey) + public function __construct($login, $apiKey, HttpClient $httpClient, RequestFactory $requestFactory) { - parent::__construct($adapter); + $this->params['login'] = $login; + $this->params['apiKey'] = $apiKey; - $this->defaultParams['login'] = $this->login = $login; - $this->defaultParams['apiKey'] = $this->apiKey = $apiKey; + $this->httpClient = $httpClient; + $this->requestFactory = $requestFactory; } /** @@ -60,15 +54,18 @@ public function __construct(HttpAdapterInterface $adapter, $login, $apiKey) */ public function shorten($url) { - $params = array_merge($this->defaultParams, [ + $params = array_merge($this->params, [ 'm' => 'shorten', 'longUrl' => trim($url), ]); $url = sprintf('%s?%s', self::ENDPOINT, http_build_query($params)); - $response = $this->adapter->get($url); - $response = json_decode($response->getBody()); + $request = $this->requestFactory->createRequest('GET', $url); + + $response = $this->httpClient->sendRequest($request); + + $response = json_decode((string) $response->getBody()); return $response->results->short_url; } @@ -78,15 +75,18 @@ public function shorten($url) */ public function expand($url) { - $params = array_merge($this->defaultParams, [ + $params = array_merge($this->params, [ 'm' => 'expand', 'shortUrl' => trim($url), ]); $url = sprintf('%s?%s', self::ENDPOINT, http_build_query($params)); - $response = $this->adapter->get($url); - $response = json_decode($response->getBody()); + $request = $this->requestFactory->createRequest('GET', $url); + + $response = $this->httpClient->sendRequest($request); + + $response = json_decode((string) $response->getBody()); return $response->results->longUrl; }