Skip to content
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

Code style fixes #13

Merged
merged 2 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/Api/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
final class Cart extends HttpApi
{
/**
* @param int $id
*
* @throws Exception
*
* @return Cart|ResponseInterface
* @return Model|ResponseInterface
*/
public function get(int $id)
{
Expand All @@ -49,13 +47,9 @@ public function get(int $id)
}

/**
* @param string $message
* @param string $location
* @param array $hashtags
*
* @throws Exception
*
* @return Cart|ResponseInterface
* @return Model|ResponseInterface
*/
public function create(string $customer, string $channel, string $localeCode)
{
Expand Down Expand Up @@ -100,12 +94,7 @@ public function create(string $customer, string $channel, string $localeCode)
}

/**
* @param int $cartId
* @param string $variant
* @param int $quantity
*
* @throws Exception\DomainException
* @throws Exception\Domain\ValidationException
* @throws Exception
*
* @return CartItem|ResponseInterface
*/
Expand Down
44 changes: 10 additions & 34 deletions src/Api/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FAPI\Sylius\Exception\InvalidArgumentException;
use FAPI\Sylius\Model\Checkout\PaymentCollection;
use FAPI\Sylius\Model\Checkout\ShipmentCollection;
use Psr\Http\Message\ResponseInterface;

/**
* @author Kasim Taskin <taskinkasim@gmail.com>
Expand All @@ -30,11 +31,7 @@ final class Checkout extends HttpApi
];

/**
* @param int $id
*
* @throws Exception
*
* @return bool
*/
public function putAddress(int $cartId, array $shippingAddress, bool $differentBillingAddress = false, array $billingAddress = []): bool
{
Expand Down Expand Up @@ -77,15 +74,9 @@ public function putAddress(int $cartId, array $shippingAddress, bool $differentB
}

/**
* @param int $cartId
* @param string $paymentMethodCode
*
* @throws Exception\DomainException
* @throws Exception\Domain\ValidationException
*
* @return bool
* @throws Exception
*/
public function putPaymentMethod(int $cartId, string $paymentMethodCode): bool
public function putPaymentMethod(int $cartId, string $paymentMethodCode)
{
if (empty($cartId)) {
throw new InvalidArgumentException('Cart id cannot be empty');
Expand Down Expand Up @@ -117,17 +108,10 @@ public function putPaymentMethod(int $cartId, string $paymentMethodCode): bool
break;
}
}

return true;
}

/**
* @param int $cartId
*
* @throws Exception\DomainException
* @throws Exception\Domain\ValidationException
*
* @return bool
* @throws Exception
*/
public function complete(int $cartId)
{
Expand All @@ -149,19 +133,14 @@ public function complete(int $cartId)
break;
}
}

return true;
}

/**
* @param int $cartId
*
* @throws Exception\DomainException
* @throws Exception\Domain\ValidationException
* @throws Exception
*
* @return ShipmentCollection
* @return ResponseInterface|ShipmentCollection
*/
public function getShippingMethods(int $cartId): ShipmentCollection
public function getShippingMethods(int $cartId)
{
if (empty($cartId)) {
throw new InvalidArgumentException('Cart id cannot be empty');
Expand All @@ -186,14 +165,11 @@ public function getShippingMethods(int $cartId): ShipmentCollection
}

/**
* @param int $cartId
*
* @throws Exception\DomainException
* @throws Exception\Domain\ValidationException
* @throws Exception
*
* @return PaymentCollection
* @return PaymentCollection|ResponseInterface
*/
public function getPaymentMethods(int $cartId): PaymentCollection
public function getPaymentMethods(int $cartId)
{
if (empty($cartId)) {
throw new InvalidArgumentException('Cart id cannot be empty');
Expand Down
6 changes: 1 addition & 5 deletions src/Api/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
final class Customer extends HttpApi
{
/**
* @param string $message
* @param string $location
* @param array $hashtags
*
* @throws Exception
*
* @return Customer|ResponseInterface
Expand All @@ -47,7 +43,7 @@ public function create(string $email, string $firstName, string $lastName, strin
throw new InvalidArgumentException('Gender cannot be empty');
}

$params = array_merge([
$params = \array_merge([
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email,
Expand Down
21 changes: 2 additions & 19 deletions src/Api/HttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ abstract class HttpApi
*/
protected $requestBuilder;

/**
* @param HttpClient $httpClient
* @param RequestBuilder $requestBuilder
* @param Hydrator $hydrator
*/
public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder)
{
$this->httpClient = $httpClient;
Expand All @@ -57,13 +52,11 @@ public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestB
* @param string $path Request path
* @param array $params GET parameters
* @param array $requestHeaders Request Headers
*
* @return ResponseInterface
*/
protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
if (\count($params) > 0) {
$path .= '?'.http_build_query($params);
$path .= '?'.\http_build_query($params);
}

return $this->httpClient->sendRequest(
Expand All @@ -77,8 +70,6 @@ protected function httpGet(string $path, array $params = [], array $requestHeade
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*
* @return ResponseInterface
*/
protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
Expand All @@ -91,8 +82,6 @@ protected function httpPost(string $path, array $params = [], array $requestHead
* @param string $path Request path
* @param array|string $body Request body
* @param array $requestHeaders Request headers
*
* @return ResponseInterface
*/
protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface
{
Expand All @@ -107,8 +96,6 @@ protected function httpPostRaw(string $path, $body, array $requestHeaders = []):
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*
* @return ResponseInterface
*/
protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
Expand All @@ -123,8 +110,6 @@ protected function httpPut(string $path, array $params = [], array $requestHeade
* @param string $path Request path
* @param array $params POST parameters to be JSON encoded
* @param array $requestHeaders Request headers
*
* @return ResponseInterface
*/
protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface
{
Expand All @@ -138,8 +123,6 @@ protected function httpDelete(string $path, array $params = [], array $requestHe
*
* Call is controlled by the specific API methods.
*
* @param ResponseInterface $response
*
* @throws DomainException
*/
protected function handleErrors(ResponseInterface $response)
Expand All @@ -163,6 +146,6 @@ protected function handleErrors(ResponseInterface $response)
*/
private function createJsonBody(array $params)
{
return (0 === \count($params)) ? null : json_encode($params, empty($params) ? JSON_FORCE_OBJECT : 0);
return (0 === \count($params)) ? null : \json_encode($params, empty($params) ? \JSON_FORCE_OBJECT : 0);
}
}
11 changes: 3 additions & 8 deletions src/Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
namespace FAPI\Sylius\Api;

use FAPI\Sylius\Exception;
use FAPI\Sylius\Model\Product\Product as Model;
use FAPI\Sylius\Model\Product\ProductCollection;
use FAPI\Sylius\Model\Product\VariantCollection;
use FAPI\Sylius\Model\Product\Product as Model;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -37,8 +37,6 @@ public function get(string $productCode)
}

/**
* @param array $params
*
* @throws Exception\DomainException
*
* @return ProductCollection|ResponseInterface
Expand All @@ -60,12 +58,9 @@ public function getAll(array $params = [])
}

/**
* @param string $productCode
* @param array $params
*
* @return VariantCollection|ResponseInterface
*
* @throws Exception\DomainException
*
* @return ResponseInterface|VariantCollection
*/
public function getVariants(string $productCode, array $params = [])
{
Expand Down
18 changes: 9 additions & 9 deletions src/Http/AuthenticationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace FAPI\Sylius\Http;

use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -29,17 +28,18 @@ class AuthenticationPlugin implements Plugin
public function __construct(Authenticator $authenticator, string $accessToken)
{
$this->authenticator = $authenticator;
$this->accessToken = json_decode($accessToken, true);
$this->accessToken = \json_decode($accessToken, true);
}

public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$header = sprintf('Bearer %s', $this->accessToken['access_token']);
$request = $request->withHeader('Authorization', $header);
$header = \sprintf('Bearer %s', $this->accessToken['access_token']);
$request = $request->withHeader('Authorization', $header);

$promise = $next($request);

return $promise->then(function (ResponseInterface $response) use ($request, $next, $first) {
if ($response->getStatusCode() !== 401) {
if (401 !== $response->getStatusCode()) {
return $response;
}

Expand All @@ -49,11 +49,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}

// Save new token
$this->accessToken = json_decode($accessToken, true);
$this->accessToken = \json_decode($accessToken, true);

// Add new token to request
$header = sprintf('Bearer %s', $this->accessToken['access_token']);
$request = $request->withHeader('Authorization', $header);
$header = \sprintf('Bearer %s', $this->accessToken['access_token']);
$request = $request->withHeader('Authorization', $header);

// Retry
$promise = $this->handleRequest($request, $next, $first);
Expand All @@ -66,6 +66,6 @@ public function handleRequest(RequestInterface $request, callable $next, callabl

public function getAccessToken(): string
{
return json_encode($this->accessToken);
return \json_encode($this->accessToken);
}
}
24 changes: 12 additions & 12 deletions src/Http/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Http\Client\HttpClient;

/**
* Class that gets access tokens
* Class that gets access tokens.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*
* @internal
*/
final class Authenticator
Expand All @@ -26,7 +27,7 @@ final class Authenticator
private $httpClient;

/**
* @var string|null
* @var null|string
*/
private $accessToken;

Expand All @@ -48,21 +49,20 @@ public function __construct(RequestBuilder $requestBuilder, HttpClient $httpClie
$this->clientSecret = $clientSecret;
}


public function createAccessToken(string $username, string $password): ?string
{
$request = $this->requestBuilder->create('POST', '/api/oauth/v2/token', [
'Content-type' => 'application/x-www-form-urlencoded',
], http_build_query([
'client_id'=>$this->clientId,
'client_secret'=>$this->clientSecret,
], \http_build_query([
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'grant_type' => 'password',
'username' => $username,
'password' => $password,
]));

$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() !== 200) {
if (200 !== $response->getStatusCode()) {
return null;
}

Expand All @@ -74,17 +74,17 @@ public function createAccessToken(string $username, string $password): ?string
public function refreshAccessToken(string $accessToken, string $refreshToken): string
{
$request = $this->requestBuilder->create('POST', '/api/oauth/v2/token', [
'Authorization' => sprintf('Bearer %s', $accessToken),
'Authorization' => \sprintf('Bearer %s', $accessToken),
'Content-type' => 'application/x-www-form-urlencoded',
], http_build_query([
], \http_build_query([
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
'client_id'=>$this->clientId,
'client_secret'=>$this->clientSecret,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
]));

$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() !== 200) {
if (200 !== $response->getStatusCode()) {
return null;
}

Expand Down