diff --git a/spec/XApiClientBuilderSpec.php b/spec/XApiClientBuilderSpec.php index a2b4524..1399928 100644 --- a/spec/XApiClientBuilderSpec.php +++ b/spec/XApiClientBuilderSpec.php @@ -3,7 +3,7 @@ namespace spec\Xabbuh\XApi\Client; use Http\Discovery\HttpClientDiscovery; -use Http\Discovery\MessageFactoryDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; use PhpSpec\Exception\Example\SkippingException; use PhpSpec\ObjectBehavior; use Psr\Http\Client\ClientInterface; @@ -151,7 +151,7 @@ private function isAbleToDiscoverHttpClient() private function isAbleToDiscoverRequestFactory() { try { - MessageFactoryDiscovery::find(); + Psr17FactoryDiscovery::findRequestFactory(); return true; } catch (\Exception $e) { diff --git a/src/Request/Handler.php b/src/Request/Handler.php index 91b957b..cf190fa 100644 --- a/src/Request/Handler.php +++ b/src/Request/Handler.php @@ -12,8 +12,6 @@ namespace Xabbuh\XApi\Client\Request; use Http\Client\Exception; -use Http\Message\RequestFactory; - use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; @@ -40,8 +38,12 @@ final class Handler implements HandlerInterface * @param string $baseUri The APIs base URI (all end points will be created relatively to this URI) * @param string $version The xAPI version */ - public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, $baseUri, $version) - { + public function __construct( + ClientInterface $httpClient, + RequestFactoryInterface $requestFactory, + $baseUri, + $version + ) { $this->httpClient = $httpClient; $this->requestFactory = $requestFactory; $this->baseUri = $baseUri; @@ -53,15 +55,19 @@ public function __construct(ClientInterface $httpClient, RequestFactoryInterface */ public function createRequest($method, $uri, array $urlParameters = array(), $body = null, array $headers = array()) { - if (!in_array(strtoupper($method), array('GET', 'POST', 'PUT', 'DELETE'))) { - throw new \InvalidArgumentException(sprintf('"%s" is no valid HTTP method (expected one of [GET, POST, PUT, DELETE]) in an xAPI context.', $method)); + throw new \InvalidArgumentException( + sprintf( + '"%s" is no valid HTTP method (expected one of [GET, POST, PUT, DELETE]) in an xAPI context.', + $method + ) + ); } - $uri = rtrim($this->baseUri, '/').'/'.ltrim($uri, '/'); + $uri = rtrim($this->baseUri, '/') . '/' . ltrim($uri, '/'); if (count($urlParameters) > 0) { - $uri .= '?'.http_build_query($urlParameters); + $uri .= '?' . http_build_query($urlParameters); } if (!isset($headers['X-Experience-API-Version'])) { @@ -105,17 +111,17 @@ public function executeRequest(RequestInterface $request, array $validStatusCode // catch some common errors if (in_array($response->getStatusCode(), array(401, 403))) { throw new AccessDeniedException( - (string) $response->getBody(), + (string)$response->getBody(), $response->getStatusCode() ); } elseif (404 === $response->getStatusCode()) { - throw new NotFoundException((string) $response->getBody()); + throw new NotFoundException((string)$response->getBody()); } elseif (409 === $response->getStatusCode()) { - throw new ConflictException((string) $response->getBody()); + throw new ConflictException((string)$response->getBody()); } if (!in_array($response->getStatusCode(), $validStatusCodes)) { - throw new XApiException((string) $response->getBody(), $response->getStatusCode()); + throw new XApiException((string)$response->getBody(), $response->getStatusCode()); } return $response; diff --git a/src/Request/HandlerInterface.php b/src/Request/HandlerInterface.php index 7e63d8a..d63f68f 100644 --- a/src/Request/HandlerInterface.php +++ b/src/Request/HandlerInterface.php @@ -13,7 +13,6 @@ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\StreamInterface; use Xabbuh\XApi\Common\Exception\XApiException; /** @@ -24,23 +23,29 @@ interface HandlerInterface { /** - * @param string $method The HTTP method - * @param string $uri The URI to send the request to - * @param array $urlParameters Optional url parameters - * @param string $body An optional request body - * @param array $headers Optional additional HTTP headers + * @param string $method The HTTP method + * @param string $uri The URI to send the request to + * @param array $urlParameters Optional url parameters + * @param string $body An optional request body + * @param array $headers Optional additional HTTP headers * * @return RequestInterface The request * * @throws \InvalidArgumentException when no valid HTTP method is given */ - public function createRequest($method, $uri, array $urlParameters = array(), $body = null, array $headers = array()); + public function createRequest( + $method, + $uri, + array $urlParameters = array(), + $body = null, + array $headers = array() + ); /** * Performs the given HTTP request. * - * @param RequestInterface $request The HTTP request to perform - * @param int[] $validStatusCodes A list of HTTP status codes + * @param RequestInterface $request The HTTP request to perform + * @param int[] $validStatusCodes A list of HTTP status codes * the calling method is able to * handle * diff --git a/src/XApiClient.php b/src/XApiClient.php index b6780ff..15259b4 100644 --- a/src/XApiClient.php +++ b/src/XApiClient.php @@ -13,7 +13,6 @@ use Xabbuh\XApi\Client\Api\ActivityProfileApiClient; use Xabbuh\XApi\Client\Api\AgentProfileApiClient; -use Xabbuh\XApi\Client\Api\ApiClient; use Xabbuh\XApi\Client\Api\StateApiClient; use Xabbuh\XApi\Client\Api\StatementsApiClient; use Xabbuh\XApi\Client\Request\HandlerInterface; @@ -32,12 +31,15 @@ final class XApiClient implements XApiClientInterface private $serializerRegistry; /** - * @param HandlerInterface $requestHandler The HTTP request handler + * @param HandlerInterface $requestHandler The HTTP request handler * @param SerializerRegistryInterface $serializerRegistry The serializer registry - * @param string $version The xAPI version + * @param string $version The xAPI version */ - public function __construct(HandlerInterface $requestHandler, SerializerRegistryInterface $serializerRegistry, $version) - { + public function __construct( + HandlerInterface $requestHandler, + SerializerRegistryInterface $serializerRegistry, + $version + ) { $this->requestHandler = $requestHandler; $this->serializerRegistry = $serializerRegistry; $this->version = $version; diff --git a/src/XApiClientBuilder.php b/src/XApiClientBuilder.php index cad0d11..3e8051a 100644 --- a/src/XApiClientBuilder.php +++ b/src/XApiClientBuilder.php @@ -18,8 +18,8 @@ use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner; use Http\Client\Common\Plugin\AuthenticationPlugin; use Http\Client\Common\PluginClient; -use Http\Discovery\HttpClientDiscovery; -use Http\Discovery\MessageFactoryDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; use Http\Message\Authentication\BasicAuth; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; @@ -131,9 +131,9 @@ public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $toke */ public function build() { - if (null === $this->httpClient && class_exists(HttpClientDiscovery::class)) { + if (null === $this->httpClient && class_exists(Psr18ClientDiscovery::class)) { try { - $this->httpClient = HttpClientDiscovery::find(); + $this->httpClient = Psr18ClientDiscovery::find(); } catch (\Exception $e) { } } @@ -142,9 +142,9 @@ public function build() throw new \LogicException('No HTTP client was configured.'); } - if (null === $this->requestFactory && class_exists(MessageFactoryDiscovery::class)) { + if (null === $this->requestFactory && class_exists(Psr17FactoryDiscovery::class)) { try { - $this->requestFactory = MessageFactoryDiscovery::find(); + $this->requestFactory = Psr17FactoryDiscovery::findRequestFactory(); } catch (\Exception $e) { } } @@ -171,11 +171,18 @@ public function build() if (null !== $this->consumerKey && null !== $this->consumerSecret && null !== $this->accessToken && null !== $this->tokenSecret) { if (!class_exists(OAuth1::class)) { - throw new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.'); + throw new \LogicException( + 'The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.' + ); } - $requestSigner = new RequestSigner(new ConsumerKey($this->consumerKey), new ConsumerSecret($this->consumerSecret)); - $oauth = new OAuth1($requestSigner, new AccessToken($this->accessToken), new TokenSecret($this->tokenSecret)); + $requestSigner = new RequestSigner( + new ConsumerKey($this->consumerKey), + new ConsumerSecret($this->consumerSecret) + ); + $oauth = new OAuth1( + $requestSigner, new AccessToken($this->accessToken), new TokenSecret($this->tokenSecret) + ); $plugins[] = new AuthenticationPlugin($oauth); } diff --git a/src/XApiClientBuilderInterface.php b/src/XApiClientBuilderInterface.php index 1e4c078..87ec519 100644 --- a/src/XApiClientBuilderInterface.php +++ b/src/XApiClientBuilderInterface.php @@ -70,10 +70,10 @@ public function setAuth($username, $password); /** * Sets OAuth credentials. * - * @param string $consumerKey The consumer key + * @param string $consumerKey The consumer key * @param string $consumerSecret The consumer secret - * @param string $token The token - * @param string $tokenSecret The secret token + * @param string $token The token + * @param string $tokenSecret The secret token * * @return XApiClientBuilderInterface The builder */