-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 402 - Payment Required response code
- Loading branch information
Jason Benedetti
committed
Sep 11, 2020
1 parent
a26f9a4
commit 9d9cf20
Showing
9 changed files
with
106 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
test/suite/unit/Exception/PaymentRequiredResponseExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters