diff --git a/src/BestAvailableObjectsNotFoundException.php b/src/BestAvailableObjectsNotFoundException.php new file mode 100644 index 0000000..71a0837 --- /dev/null +++ b/src/BestAvailableObjectsNotFoundException.php @@ -0,0 +1,14 @@ +errors = $info['errors']; - $this->requestId = $requestId; + $code = $response->getStatusCode(); + $parsedResponse = self::extractInfo($response); + $message = self::message($request, $response, $parsedResponse['messages']); + if ($code == 429) { + return new RateLimitExceededException($request, $parsedResponse, $message); + } else if (self::isBestAvailableObjectsNotFound($parsedResponse['errors'])) { + throw new BestAvailableObjectsNotFoundException($request, $parsedResponse, $message); + } + return new SeatsioException($request, $parsedResponse, $message); + } + + public function __construct(RequestInterface $request, array $parsedResponse, string $message) + { + parent::__construct($message); + $this->errors = $parsedResponse['errors']; + $this->requestId = $parsedResponse['requestId']; } private static function message($request, $response, $messages) @@ -45,10 +56,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'); @@ -56,4 +67,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..c0096e4 100644 --- a/tests/Events/ChangeBestAvailableObjectStatusTest.php +++ b/tests/Events/ChangeBestAvailableObjectStatusTest.php @@ -2,8 +2,10 @@ namespace Seatsio\Events; +use Seatsio\BestAvailableObjectsNotFoundException; use Seatsio\Common\IDs; use Seatsio\SeatsioClientTest; +use Seatsio\SeatsioException; class ChangeBestAvailableObjectStatusTest extends SeatsioClientTest { @@ -203,4 +205,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); + } + + } } 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()); }