Skip to content

Commit

Permalink
Add support for 402 - Payment Required response code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Benedetti committed Sep 11, 2020
1 parent a26f9a4 commit 9d9cf20
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/Exception/BadRequestResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

class BadRequestResponseException extends UnexpectedResponseException
{
const STATUS_CODE = 400;

public function __construct(string $serializedErrors = '')
{
parent::__construct(400, $serializedErrors);
parent::__construct(self::STATUS_CODE, $serializedErrors);
}

public function getStatusCode(): int
{
return 400;
return self::STATUS_CODE;
}
}
6 changes: 4 additions & 2 deletions src/Exception/ForbiddenResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

class ForbiddenResponseException extends UnexpectedResponseException
{
const STATUS_CODE = 403;

public function __construct(string $serializedErrors = '')
{
parent::__construct(403, $serializedErrors);
parent::__construct(self::STATUS_CODE, $serializedErrors);
}

public function getStatusCode(): int
{
return 403;
return self::STATUS_CODE;
}
}
6 changes: 4 additions & 2 deletions src/Exception/NotFoundResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

class NotFoundResponseException extends UnexpectedResponseException
{
const STATUS_CODE = 404;

public function __construct(string $serializedErrors = '')
{
parent::__construct(404, $serializedErrors);
parent::__construct(self::STATUS_CODE, $serializedErrors);
}

public function getStatusCode(): int
{
return 404;
return self::STATUS_CODE;
}
}
18 changes: 18 additions & 0 deletions src/Exception/PaymentRequiredResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace DoclerLabs\ApiClientBase\Exception;

class PaymentRequiredResponseException extends UnexpectedResponseException
{
const STATUS_CODE = 402;

public function __construct(string $serializedErrors = '')
{
parent::__construct(self::STATUS_CODE, $serializedErrors);
}

public function getStatusCode(): int
{
return self::STATUS_CODE;
}
}
29 changes: 29 additions & 0 deletions src/Exception/ResponseExceptionsPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace DoclerLabs\ApiClientBase\Exception;

class ResponseExceptionsPool
{
/** @var string[] */
private $responseExceptions;

public function __construct()
{
$this->responseExceptions = [
BadRequestResponseException::STATUS_CODE => BadRequestResponseException::class,
UnauthorizedResponseException::STATUS_CODE => UnauthorizedResponseException::class,
PaymentRequiredResponseException::STATUS_CODE => PaymentRequiredResponseException::class,
ForbiddenResponseException::STATUS_CODE => ForbiddenResponseException::class,
NotFoundResponseException::STATUS_CODE => NotFoundResponseException::class,
];
}

public function getException(int $statusCode, string $responseBody): UnexpectedResponseException
{
if (isset($this->responseExceptions[$statusCode])) {
return new $this->responseExceptions[$statusCode]($responseBody);
}

return new UnexpectedResponseException($statusCode, $responseBody);
}
}
6 changes: 4 additions & 2 deletions src/Exception/UnauthorizedResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

class UnauthorizedResponseException extends UnexpectedResponseException
{
const STATUS_CODE = 401;

public function __construct(string $serializedErrors = '')
{
parent::__construct(401, $serializedErrors);
parent::__construct(self::STATUS_CODE, $serializedErrors);
}

public function getStatusCode(): int
{
return 401;
return self::STATUS_CODE;
}
}
33 changes: 15 additions & 18 deletions src/Response/Handler/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use DoclerLabs\ApiClientBase\Exception\BadRequestResponseException;
use DoclerLabs\ApiClientBase\Exception\ForbiddenResponseException;
use DoclerLabs\ApiClientBase\Exception\NotFoundResponseException;
use DoclerLabs\ApiClientBase\Exception\PaymentRequiredResponseException;
use DoclerLabs\ApiClientBase\Exception\ResponseExceptionsPool;
use DoclerLabs\ApiClientBase\Exception\UnauthorizedResponseException;
use DoclerLabs\ApiClientBase\Exception\UnexpectedResponseException;
use DoclerLabs\ApiClientBase\Json\Json;
Expand All @@ -17,13 +19,24 @@ class ResponseHandler implements ResponseHandlerInterface
{
const JSON_OPTIONS = JSON_PRESERVE_ZERO_FRACTION + JSON_BIGINT_AS_STRING;

/** @var ResponseExceptionsPool */
private $responseExceptionsPool;

public function __construct()
{
$this->responseExceptionsPool = new ResponseExceptionsPool();
}

/**
* @param ResponseInterface $response
*
* @return Response
*
* @throws NotFoundResponseException
* @throws BadRequestResponseException
* @throws UnauthorizedResponseException
* @throws PaymentRequiredResponseException
* @throws ForbiddenResponseException
* @throws NotFoundResponseException
* @throws UnexpectedResponseException
* @throws JsonException
*/
Expand All @@ -49,23 +62,7 @@ public function handle(ResponseInterface $response): Response
return new Response($statusCode, $decodedPayload, $headers);
}

if ($statusCode === 400) {
throw new BadRequestResponseException($responseBody);
}

if ($statusCode === 401) {
throw new UnauthorizedResponseException($responseBody);
}

if ($statusCode === 403) {
throw new ForbiddenResponseException($responseBody);
}

if ($statusCode === 404) {
throw new NotFoundResponseException($responseBody);
}

throw new UnexpectedResponseException($statusCode, $responseBody);
throw $this->responseExceptionsPool->getException($statusCode, $responseBody);
}

private function isResponseBodyEmpty(StreamInterface $responseBody = null): bool
Expand Down
26 changes: 26 additions & 0 deletions test/suite/unit/Exception/PaymentRequiredResponseExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace DoclerLabs\ApiClientBase\Test\Unit\Exception;

use DoclerLabs\ApiClientBase\Exception\PaymentRequiredResponseException;
use PHPUnit\Framework\TestCase;
use Throwable;

/**
* @coversDefaultClass \DoclerLabs\ApiClientBase\Exception\PaymentRequiredResponseException
*/
class PaymentRequiredResponseExceptionTest extends TestCase
{
/**
* @covers ::__construct
* @covers ::getStatusCode
*/
public function testException()
{
$statusCode = 402;
$sut = new PaymentRequiredResponseException();

self::assertInstanceOf(Throwable::class, $sut);
self::assertEquals($statusCode, $sut->getStatusCode());
}
}
2 changes: 2 additions & 0 deletions test/suite/unit/Response/Handler/ResponseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DoclerLabs\ApiClientBase\Exception\BadRequestResponseException;
use DoclerLabs\ApiClientBase\Exception\ForbiddenResponseException;
use DoclerLabs\ApiClientBase\Exception\NotFoundResponseException;
use DoclerLabs\ApiClientBase\Exception\PaymentRequiredResponseException;
use DoclerLabs\ApiClientBase\Exception\UnauthorizedResponseException;
use DoclerLabs\ApiClientBase\Exception\UnexpectedResponseException;
use DoclerLabs\ApiClientBase\Response\Handler\ResponseHandler;
Expand Down Expand Up @@ -234,6 +235,7 @@ public function exceptionsDataProvider(): array
return [
[400, BadRequestResponseException::class],
[401, UnauthorizedResponseException::class],
[402, PaymentRequiredResponseException::class],
[403, ForbiddenResponseException::class],
[404, NotFoundResponseException::class],
[500, UnexpectedResponseException::class],
Expand Down

0 comments on commit 9d9cf20

Please sign in to comment.