From 57cce78c91489dc471df2117b8e98eb0962b00fe Mon Sep 17 00:00:00 2001 From: Ben Verbeken Date: Fri, 20 Dec 2024 09:56:25 +0100 Subject: [PATCH 1/5] refactoring --- src/SeatsioClient.php | 5 +---- src/SeatsioException.php | 9 +++++++++ tests/SeatsioExceptionTest.php | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/SeatsioClient.php b/src/SeatsioClient.php index b3fdf6d..ded2d2b 100644 --- a/src/SeatsioClient.php +++ b/src/SeatsioClient.php @@ -118,10 +118,7 @@ function (ResponseInterface $response) use ($request, $handler) { if ($code < 400) { return $response; } - if ($code == 429) { - throw new RateLimitExceededException($request, $response); - } - throw new SeatsioException($request, $response); + throw SeatsioException::from($request, $response); } ); }; diff --git a/src/SeatsioException.php b/src/SeatsioException.php index b0e7b16..ddd146c 100644 --- a/src/SeatsioException.php +++ b/src/SeatsioException.php @@ -19,6 +19,15 @@ class SeatsioException extends RuntimeException */ public $requestId; + public static function from(RequestInterface $request, ResponseInterface $response): SeatsioException + { + $code = $response->getStatusCode(); + if ($code == 429) { + return new RateLimitExceededException($request, $response); + } + return new SeatsioException($request, $response); + } + public function __construct(RequestInterface $request, ResponseInterface $response) { $info = self::extractInfo($response); diff --git a/tests/SeatsioExceptionTest.php b/tests/SeatsioExceptionTest.php index d0b764b..32e9757 100644 --- a/tests/SeatsioExceptionTest.php +++ b/tests/SeatsioExceptionTest.php @@ -19,7 +19,7 @@ public function testCanInstantiateSeatsioExceptionWithoutRequestId() ["Content-Type" => "application/json"], "{\"errors\": [], \"messages\":[]}" ); - $exception = new SeatsioException($request, $response); + $exception = SeatsioException::from($request, $response); self::assertNull($exception->requestId); self::assertStringStartsWith("GET http://dummy.uri resulted in a `400 Bad Request` response.", $exception->getMessage()); } From 2c32656a58b0e185e7342001156d9ec720b228b8 Mon Sep 17 00:00:00 2001 From: Ben Verbeken Date: Fri, 20 Dec 2024 10:22:04 +0100 Subject: [PATCH 2/5] throw new exception type when no best available objects have been found, instead of the generic SeatsioException --- .../BestAvailableObjectsNotFoundException.php | 16 +++++++++ src/RateLimitExceededException.php | 4 +-- src/SeatsioException.php | 36 ++++++++++++++----- .../ChangeBestAvailableObjectStatusTest.php | 29 +++++++++++++++ 4 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/Events/BestAvailableObjectsNotFoundException.php diff --git a/src/Events/BestAvailableObjectsNotFoundException.php b/src/Events/BestAvailableObjectsNotFoundException.php new file mode 100644 index 0000000..c90f91e --- /dev/null +++ b/src/Events/BestAvailableObjectsNotFoundException.php @@ -0,0 +1,16 @@ +getStatusCode(); + $parsedResponse = self::extractInfo($response); + $message = self::message($request, $response, $parsedResponse['messages']); if ($code == 429) { - return new RateLimitExceededException($request, $response); + return new RateLimitExceededException($request, $response, $parsedResponse, $message); + } else if (self::isBestAvailableObjectsNotFound($parsedResponse['errors'])) { + throw new BestAvailableObjectsNotFoundException($request, $response, $parsedResponse, $message); } - return new SeatsioException($request, $response); + return new SeatsioException($request, $response, $parsedResponse, $message); } - public function __construct(RequestInterface $request, ResponseInterface $response) + public function __construct(RequestInterface $request, ResponseInterface $response, array $info, string $message) { - $info = self::extractInfo($response); - $requestId = $info['requestId']; - parent::__construct(self::message($request, $response, $info['messages'])); + parent::__construct($message); $this->errors = $info['errors']; - $this->requestId = $requestId; + $this->requestId = $info['requestId']; } private static function message($request, $response, $messages) @@ -54,10 +57,10 @@ private static function message($request, $response, $messages) } } - private static function extractInfo($response) + private static function extractInfo($response): array { $contentType = $response->getHeaderLine("content-type"); - if (strpos($contentType, 'application/json') !== false) { + if (str_contains($contentType, 'application/json')) { $json = GuzzleResponseDecoder::decodeToObject($response); $mapper = SeatsioJsonMapper::create(); $errors = $mapper->mapArray($json->errors, array(), 'Seatsio\ApiError'); @@ -65,4 +68,19 @@ private static function extractInfo($response) } return ["messages" => [], "errors" => [], "requestId" => null]; } + + private static function isBestAvailableObjectsNotFound($errors): bool + { + if (!is_array($errors) || empty($errors)) { + return false; + } + foreach ($errors as $error) { + if (is_object($error) && property_exists($error, 'code')) { + if ($error->code === 'BEST_AVAILABLE_OBJECTS_NOT_FOUND') { + return true; + } + } + } + return false; + } } diff --git a/tests/Events/ChangeBestAvailableObjectStatusTest.php b/tests/Events/ChangeBestAvailableObjectStatusTest.php index c6cf3ae..a25e181 100644 --- a/tests/Events/ChangeBestAvailableObjectStatusTest.php +++ b/tests/Events/ChangeBestAvailableObjectStatusTest.php @@ -4,6 +4,7 @@ use Seatsio\Common\IDs; use Seatsio\SeatsioClientTest; +use Seatsio\SeatsioException; class ChangeBestAvailableObjectStatusTest extends SeatsioClientTest { @@ -203,4 +204,32 @@ public function accessibleSeats() self::assertTrue($bestAvailableObjects->nextToEachOther); self::assertEquals(["A-6", "A-7", "A-8"], $bestAvailableObjects->objects); } + + public function testNotFoundThrowsBestAvailableObjectsNotFoundException() + { + $chartKey = $this->createTestChart(); + $event = $this->seatsioClient->events->create($chartKey); + + try + { + $this->seatsioClient->events->changeBestAvailableObjectStatus($event->key, (new BestAvailableParams())->setNumber(3000), "lolzor"); + self::fail("expected exception"); + } catch (BestAvailableObjectsNotFoundException $exception) { + self::assertInstanceOf(BestAvailableObjectsNotFoundException::class, $exception); + self::assertEquals("Best available objects not found", $exception->getMessage()); + } + + } + + public function testNormalSeatsioExceptionIfEventNotFound() + { + try + { + $this->seatsioClient->events->changeBestAvailableObjectStatus("unexistingEvent", (new BestAvailableParams())->setNumber(3), "lolzor"); + self::fail("expected exception"); + } catch (SeatsioException $exception) { + self::assertNotInstanceOf(BestAvailableObjectsNotFoundException::class, $exception); + } + + } } From 23bb0b7dff7f7d931e09ebcddfae9aa10e6f8fbd Mon Sep 17 00:00:00 2001 From: Ben Verbeken Date: Fri, 20 Dec 2024 10:23:12 +0100 Subject: [PATCH 3/5] refactoring - moved BestAvailableObjectsNotFoundException to root Seatsio namespace --- src/{Events => }/BestAvailableObjectsNotFoundException.php | 3 +-- src/SeatsioException.php | 1 - tests/Events/ChangeBestAvailableObjectStatusTest.php | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) rename src/{Events => }/BestAvailableObjectsNotFoundException.php (87%) diff --git a/src/Events/BestAvailableObjectsNotFoundException.php b/src/BestAvailableObjectsNotFoundException.php similarity index 87% rename from src/Events/BestAvailableObjectsNotFoundException.php rename to src/BestAvailableObjectsNotFoundException.php index c90f91e..17e6769 100644 --- a/src/Events/BestAvailableObjectsNotFoundException.php +++ b/src/BestAvailableObjectsNotFoundException.php @@ -1,11 +1,10 @@ Date: Fri, 20 Dec 2024 10:25:39 +0100 Subject: [PATCH 4/5] refactoring - removed unused $response constructor parameter --- src/BestAvailableObjectsNotFoundException.php | 5 ++--- src/RateLimitExceededException.php | 5 ++--- src/SeatsioException.php | 8 ++++---- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/BestAvailableObjectsNotFoundException.php b/src/BestAvailableObjectsNotFoundException.php index 17e6769..5947ff9 100644 --- a/src/BestAvailableObjectsNotFoundException.php +++ b/src/BestAvailableObjectsNotFoundException.php @@ -4,12 +4,11 @@ use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; class BestAvailableObjectsNotFoundException extends SeatsioException { - public function __construct(RequestInterface $request, ResponseInterface $response, array $parsedResponseInfo, string $message) + public function __construct(RequestInterface $request, array $parsedResponseInfo, string $message) { - parent::__construct($request, $response, $parsedResponseInfo, $message); + parent::__construct($request, $parsedResponseInfo, $message); } } diff --git a/src/RateLimitExceededException.php b/src/RateLimitExceededException.php index 73729b3..06585e9 100644 --- a/src/RateLimitExceededException.php +++ b/src/RateLimitExceededException.php @@ -3,12 +3,11 @@ namespace Seatsio; use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; class RateLimitExceededException extends SeatsioException { - public function __construct(RequestInterface $request, ResponseInterface $response, array $parsedResponseInfo, string $message) + public function __construct(RequestInterface $request,array $parsedResponseInfo, string $message) { - parent::__construct($request, $response, $parsedResponseInfo, $message); + parent::__construct($request, $parsedResponseInfo, $message); } } diff --git a/src/SeatsioException.php b/src/SeatsioException.php index 6988c2e..d3a32ed 100644 --- a/src/SeatsioException.php +++ b/src/SeatsioException.php @@ -25,14 +25,14 @@ public static function from(RequestInterface $request, ResponseInterface $respon $parsedResponse = self::extractInfo($response); $message = self::message($request, $response, $parsedResponse['messages']); if ($code == 429) { - return new RateLimitExceededException($request, $response, $parsedResponse, $message); + return new RateLimitExceededException($request, $parsedResponse, $message); } else if (self::isBestAvailableObjectsNotFound($parsedResponse['errors'])) { - throw new BestAvailableObjectsNotFoundException($request, $response, $parsedResponse, $message); + throw new BestAvailableObjectsNotFoundException($request, $parsedResponse, $message); } - return new SeatsioException($request, $response, $parsedResponse, $message); + return new SeatsioException($request, $parsedResponse, $message); } - public function __construct(RequestInterface $request, ResponseInterface $response, array $info, string $message) + public function __construct(RequestInterface $request, array $info, string $message) { parent::__construct($message); $this->errors = $info['errors']; From 2a0022becfcca1be2fbd2c957d55a42b18d2ef50 Mon Sep 17 00:00:00 2001 From: Ben Verbeken Date: Fri, 20 Dec 2024 10:28:04 +0100 Subject: [PATCH 5/5] refactoring - rename --- src/BestAvailableObjectsNotFoundException.php | 4 ++-- src/RateLimitExceededException.php | 4 ++-- src/SeatsioException.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/BestAvailableObjectsNotFoundException.php b/src/BestAvailableObjectsNotFoundException.php index 5947ff9..71a0837 100644 --- a/src/BestAvailableObjectsNotFoundException.php +++ b/src/BestAvailableObjectsNotFoundException.php @@ -7,8 +7,8 @@ class BestAvailableObjectsNotFoundException extends SeatsioException { - public function __construct(RequestInterface $request, array $parsedResponseInfo, string $message) + public function __construct(RequestInterface $request, array $parsedResponse, string $message) { - parent::__construct($request, $parsedResponseInfo, $message); + parent::__construct($request, $parsedResponse, $message); } } diff --git a/src/RateLimitExceededException.php b/src/RateLimitExceededException.php index 06585e9..83cb064 100644 --- a/src/RateLimitExceededException.php +++ b/src/RateLimitExceededException.php @@ -6,8 +6,8 @@ class RateLimitExceededException extends SeatsioException { - public function __construct(RequestInterface $request,array $parsedResponseInfo, string $message) + public function __construct(RequestInterface $request, array $parsedResponse, string $message) { - parent::__construct($request, $parsedResponseInfo, $message); + parent::__construct($request, $parsedResponse, $message); } } diff --git a/src/SeatsioException.php b/src/SeatsioException.php index d3a32ed..6c13e09 100644 --- a/src/SeatsioException.php +++ b/src/SeatsioException.php @@ -32,11 +32,11 @@ public static function from(RequestInterface $request, ResponseInterface $respon return new SeatsioException($request, $parsedResponse, $message); } - public function __construct(RequestInterface $request, array $info, string $message) + public function __construct(RequestInterface $request, array $parsedResponse, string $message) { parent::__construct($message); - $this->errors = $info['errors']; - $this->requestId = $info['requestId']; + $this->errors = $parsedResponse['errors']; + $this->requestId = $parsedResponse['requestId']; } private static function message($request, $response, $messages)