Skip to content

Commit c1c7610

Browse files
committed
cleanup
1 parent ca373af commit c1c7610

File tree

6 files changed

+60
-40
lines changed

6 files changed

+60
-40
lines changed

spec/XApiClientBuilderSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace spec\Xabbuh\XApi\Client;
44

55
use Http\Discovery\HttpClientDiscovery;
6-
use Http\Discovery\MessageFactoryDiscovery;
6+
use Http\Discovery\Psr17FactoryDiscovery;
77
use PhpSpec\Exception\Example\SkippingException;
88
use PhpSpec\ObjectBehavior;
99
use Psr\Http\Client\ClientInterface;
@@ -151,7 +151,7 @@ private function isAbleToDiscoverHttpClient()
151151
private function isAbleToDiscoverRequestFactory()
152152
{
153153
try {
154-
MessageFactoryDiscovery::find();
154+
Psr17FactoryDiscovery::findRequestFactory();
155155

156156
return true;
157157
} catch (\Exception $e) {

src/Request/Handler.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Xabbuh\XApi\Client\Request;
1313

1414
use Http\Client\Exception;
15-
use Http\Message\RequestFactory;
16-
1715
use Psr\Http\Client\ClientInterface;
1816
use Psr\Http\Message\RequestFactoryInterface;
1917
use Psr\Http\Message\RequestInterface;
@@ -40,8 +38,12 @@ final class Handler implements HandlerInterface
4038
* @param string $baseUri The APIs base URI (all end points will be created relatively to this URI)
4139
* @param string $version The xAPI version
4240
*/
43-
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, $baseUri, $version)
44-
{
41+
public function __construct(
42+
ClientInterface $httpClient,
43+
RequestFactoryInterface $requestFactory,
44+
$baseUri,
45+
$version
46+
) {
4547
$this->httpClient = $httpClient;
4648
$this->requestFactory = $requestFactory;
4749
$this->baseUri = $baseUri;
@@ -53,15 +55,19 @@ public function __construct(ClientInterface $httpClient, RequestFactoryInterface
5355
*/
5456
public function createRequest($method, $uri, array $urlParameters = array(), $body = null, array $headers = array())
5557
{
56-
5758
if (!in_array(strtoupper($method), array('GET', 'POST', 'PUT', 'DELETE'))) {
58-
throw new \InvalidArgumentException(sprintf('"%s" is no valid HTTP method (expected one of [GET, POST, PUT, DELETE]) in an xAPI context.', $method));
59+
throw new \InvalidArgumentException(
60+
sprintf(
61+
'"%s" is no valid HTTP method (expected one of [GET, POST, PUT, DELETE]) in an xAPI context.',
62+
$method
63+
)
64+
);
5965
}
6066

61-
$uri = rtrim($this->baseUri, '/').'/'.ltrim($uri, '/');
67+
$uri = rtrim($this->baseUri, '/') . '/' . ltrim($uri, '/');
6268

6369
if (count($urlParameters) > 0) {
64-
$uri .= '?'.http_build_query($urlParameters);
70+
$uri .= '?' . http_build_query($urlParameters);
6571
}
6672

6773
if (!isset($headers['X-Experience-API-Version'])) {
@@ -105,17 +111,17 @@ public function executeRequest(RequestInterface $request, array $validStatusCode
105111
// catch some common errors
106112
if (in_array($response->getStatusCode(), array(401, 403))) {
107113
throw new AccessDeniedException(
108-
(string) $response->getBody(),
114+
(string)$response->getBody(),
109115
$response->getStatusCode()
110116
);
111117
} elseif (404 === $response->getStatusCode()) {
112-
throw new NotFoundException((string) $response->getBody());
118+
throw new NotFoundException((string)$response->getBody());
113119
} elseif (409 === $response->getStatusCode()) {
114-
throw new ConflictException((string) $response->getBody());
120+
throw new ConflictException((string)$response->getBody());
115121
}
116122

117123
if (!in_array($response->getStatusCode(), $validStatusCodes)) {
118-
throw new XApiException((string) $response->getBody(), $response->getStatusCode());
124+
throw new XApiException((string)$response->getBody(), $response->getStatusCode());
119125
}
120126

121127
return $response;

src/Request/HandlerInterface.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Http\Message\RequestInterface;
1515
use Psr\Http\Message\ResponseInterface;
16-
use Psr\Http\Message\StreamInterface;
1716
use Xabbuh\XApi\Common\Exception\XApiException;
1817

1918
/**
@@ -24,23 +23,29 @@
2423
interface HandlerInterface
2524
{
2625
/**
27-
* @param string $method The HTTP method
28-
* @param string $uri The URI to send the request to
29-
* @param array $urlParameters Optional url parameters
30-
* @param string $body An optional request body
31-
* @param array $headers Optional additional HTTP headers
26+
* @param string $method The HTTP method
27+
* @param string $uri The URI to send the request to
28+
* @param array $urlParameters Optional url parameters
29+
* @param string $body An optional request body
30+
* @param array $headers Optional additional HTTP headers
3231
*
3332
* @return RequestInterface The request
3433
*
3534
* @throws \InvalidArgumentException when no valid HTTP method is given
3635
*/
37-
public function createRequest($method, $uri, array $urlParameters = array(), $body = null, array $headers = array());
36+
public function createRequest(
37+
$method,
38+
$uri,
39+
array $urlParameters = array(),
40+
$body = null,
41+
array $headers = array()
42+
);
3843

3944
/**
4045
* Performs the given HTTP request.
4146
*
42-
* @param RequestInterface $request The HTTP request to perform
43-
* @param int[] $validStatusCodes A list of HTTP status codes
47+
* @param RequestInterface $request The HTTP request to perform
48+
* @param int[] $validStatusCodes A list of HTTP status codes
4449
* the calling method is able to
4550
* handle
4651
*

src/XApiClient.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Xabbuh\XApi\Client\Api\ActivityProfileApiClient;
1515
use Xabbuh\XApi\Client\Api\AgentProfileApiClient;
16-
use Xabbuh\XApi\Client\Api\ApiClient;
1716
use Xabbuh\XApi\Client\Api\StateApiClient;
1817
use Xabbuh\XApi\Client\Api\StatementsApiClient;
1918
use Xabbuh\XApi\Client\Request\HandlerInterface;
@@ -32,12 +31,15 @@ final class XApiClient implements XApiClientInterface
3231
private $serializerRegistry;
3332

3433
/**
35-
* @param HandlerInterface $requestHandler The HTTP request handler
34+
* @param HandlerInterface $requestHandler The HTTP request handler
3635
* @param SerializerRegistryInterface $serializerRegistry The serializer registry
37-
* @param string $version The xAPI version
36+
* @param string $version The xAPI version
3837
*/
39-
public function __construct(HandlerInterface $requestHandler, SerializerRegistryInterface $serializerRegistry, $version)
40-
{
38+
public function __construct(
39+
HandlerInterface $requestHandler,
40+
SerializerRegistryInterface $serializerRegistry,
41+
$version
42+
) {
4143
$this->requestHandler = $requestHandler;
4244
$this->serializerRegistry = $serializerRegistry;
4345
$this->version = $version;

src/XApiClientBuilder.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner;
1919
use Http\Client\Common\Plugin\AuthenticationPlugin;
2020
use Http\Client\Common\PluginClient;
21-
use Http\Discovery\HttpClientDiscovery;
22-
use Http\Discovery\MessageFactoryDiscovery;
21+
use Http\Discovery\Psr17FactoryDiscovery;
22+
use Http\Discovery\Psr18ClientDiscovery;
2323
use Http\Message\Authentication\BasicAuth;
2424
use Psr\Http\Client\ClientInterface;
2525
use Psr\Http\Message\RequestFactoryInterface;
@@ -131,9 +131,9 @@ public function setOAuthCredentials($consumerKey, $consumerSecret, $token, $toke
131131
*/
132132
public function build()
133133
{
134-
if (null === $this->httpClient && class_exists(HttpClientDiscovery::class)) {
134+
if (null === $this->httpClient && class_exists(Psr18ClientDiscovery::class)) {
135135
try {
136-
$this->httpClient = HttpClientDiscovery::find();
136+
$this->httpClient = Psr18ClientDiscovery::find();
137137
} catch (\Exception $e) {
138138
}
139139
}
@@ -142,9 +142,9 @@ public function build()
142142
throw new \LogicException('No HTTP client was configured.');
143143
}
144144

145-
if (null === $this->requestFactory && class_exists(MessageFactoryDiscovery::class)) {
145+
if (null === $this->requestFactory && class_exists(Psr17FactoryDiscovery::class)) {
146146
try {
147-
$this->requestFactory = MessageFactoryDiscovery::find();
147+
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
148148
} catch (\Exception $e) {
149149
}
150150
}
@@ -171,11 +171,18 @@ public function build()
171171

172172
if (null !== $this->consumerKey && null !== $this->consumerSecret && null !== $this->accessToken && null !== $this->tokenSecret) {
173173
if (!class_exists(OAuth1::class)) {
174-
throw new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.');
174+
throw new \LogicException(
175+
'The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.'
176+
);
175177
}
176178

177-
$requestSigner = new RequestSigner(new ConsumerKey($this->consumerKey), new ConsumerSecret($this->consumerSecret));
178-
$oauth = new OAuth1($requestSigner, new AccessToken($this->accessToken), new TokenSecret($this->tokenSecret));
179+
$requestSigner = new RequestSigner(
180+
new ConsumerKey($this->consumerKey),
181+
new ConsumerSecret($this->consumerSecret)
182+
);
183+
$oauth = new OAuth1(
184+
$requestSigner, new AccessToken($this->accessToken), new TokenSecret($this->tokenSecret)
185+
);
179186
$plugins[] = new AuthenticationPlugin($oauth);
180187
}
181188

src/XApiClientBuilderInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public function setAuth($username, $password);
7070
/**
7171
* Sets OAuth credentials.
7272
*
73-
* @param string $consumerKey The consumer key
73+
* @param string $consumerKey The consumer key
7474
* @param string $consumerSecret The consumer secret
75-
* @param string $token The token
76-
* @param string $tokenSecret The secret token
75+
* @param string $token The token
76+
* @param string $tokenSecret The secret token
7777
*
7878
* @return XApiClientBuilderInterface The builder
7979
*/

0 commit comments

Comments
 (0)