Skip to content

cleanup #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spec/XApiClientBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,7 +151,7 @@ private function isAbleToDiscoverHttpClient()
private function isAbleToDiscoverRequestFactory()
{
try {
MessageFactoryDiscovery::find();
Psr17FactoryDiscovery::findRequestFactory();

return true;
} catch (\Exception $e) {
Expand Down
30 changes: 18 additions & 12 deletions src/Request/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 14 additions & 9 deletions src/Request/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
*
Expand Down
12 changes: 7 additions & 5 deletions src/XApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
25 changes: 16 additions & 9 deletions src/XApiClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/XApiClientBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading