Skip to content

Commit 230ae58

Browse files
committed
4992: Changed how exceptions are handled in InstantBook
1 parent a800c77 commit 230ae58

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

src/Controller/InteractiveController.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use App\Entity\ScreenUser;
88
use App\Entity\Tenant\Slide;
99
use App\Entity\User;
10-
use App\Exceptions\NotFoundException;
10+
use App\Exceptions\InteractiveSlideException;
1111
use App\Service\InteractiveSlideService;
1212
use Symfony\Bundle\SecurityBundle\Security;
1313
use Symfony\Component\HttpFoundation\JsonResponse;
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpKernel\Attribute\AsController;
16+
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1618

1719
#[AsController]
1820
final readonly class InteractiveController
@@ -26,14 +28,24 @@ public function __invoke(Request $request, Slide $slide): JsonResponse
2628
{
2729
$requestBody = $request->toArray();
2830

29-
$interactionRequest = $this->interactiveSlideService->parseRequestBody($requestBody);
31+
try {
32+
$interactionRequest = $this->interactiveSlideService->parseRequestBody($requestBody);
33+
} catch (InteractiveSlideException $e) {
34+
throw new HttpException($e->getCode(), $e->getMessage());
35+
}
3036

3137
$user = $this->security->getUser();
3238

3339
if (!($user instanceof User || $user instanceof ScreenUser)) {
34-
throw new NotFoundException('User not found');
40+
throw new NotFoundHttpException('User not found');
41+
}
42+
43+
try {
44+
$actionResult = $this->interactiveSlideService->performAction($user, $slide, $interactionRequest);
45+
} catch (InteractiveSlideException $e) {
46+
throw new HttpException($e->getCode(), $e->getMessage());
3547
}
3648

37-
return new JsonResponse($this->interactiveSlideService->performAction($user, $slide, $interactionRequest));
49+
return new JsonResponse($actionResult);
3850
}
3951
}

src/InteractiveSlide/InstantBook.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
use App\Entity\Tenant\InteractiveSlide;
1010
use App\Entity\Tenant\Slide;
1111
use App\Entity\User;
12+
use App\Exceptions\InteractiveSlideException;
1213
use App\Service\InteractiveSlideService;
1314
use App\Service\KeyVaultService;
1415
use Psr\Cache\CacheItemInterface;
1516
use Psr\Cache\InvalidArgumentException;
1617
use Symfony\Bundle\SecurityBundle\Security;
17-
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
18-
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
19-
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
2018
use Symfony\Component\Security\Core\User\UserInterface;
2119
use Symfony\Contracts\Cache\CacheInterface;
2220
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -90,7 +88,7 @@ public function performAction(UserInterface $user, Slide $slide, InteractionSlid
9088
return match ($interactionRequest->action) {
9189
self::ACTION_GET_QUICK_BOOK_OPTIONS => $this->getQuickBookOptions($slide, $interactionRequest),
9290
self::ACTION_QUICK_BOOK => $this->quickBook($slide, $interactionRequest),
93-
default => throw new BadRequestHttpException('Action not allowed'),
91+
default => throw new InteractiveSlideException('Action not allowed'),
9492
};
9593
}
9694

@@ -105,7 +103,7 @@ private function authenticate(array $configuration): array
105103
$password = $this->keyValueService->getValue($configuration['password']);
106104

107105
if (4 !== count(array_filter([$tenantId, $clientId, $username, $password]))) {
108-
throw new BadRequestHttpException('tenantId, clientId, username, password must all be set.');
106+
throw new InteractiveSlideException('tenantId, clientId, username, password must all be set.');
109107
}
110108

111109
$url = self::LOGIN_ENDPOINT.$tenantId.self::OAUTH_PATH;
@@ -124,14 +122,14 @@ private function authenticate(array $configuration): array
124122
}
125123

126124
/**
127-
* @throws InvalidArgumentException
125+
* @throws InteractiveSlideException|InvalidArgumentException
128126
*/
129127
private function getToken(Tenant $tenant, InteractiveSlide $interactive): string
130128
{
131129
$configuration = $interactive->getConfiguration();
132130

133131
if (null === $configuration) {
134-
throw new BadRequestHttpException('InteractiveSlide has no configuration');
132+
throw new InteractiveSlideException('InteractiveSlide has no configuration');
135133
}
136134

137135
return $this->interactiveSlideCache->get(
@@ -147,14 +145,15 @@ function (CacheItemInterface $item) use ($configuration): mixed {
147145
}
148146

149147
/**
150-
* @throws \Throwable
148+
* @throws InteractiveSlideException
149+
* @throws InvalidArgumentException
151150
*/
152151
private function getQuickBookOptions(Slide $slide, InteractionSlideRequest $interactionRequest): array
153152
{
154153
$resource = $interactionRequest->data['resource'] ?? null;
155154

156155
if (null === $resource) {
157-
throw new \Exception('Resource not set.');
156+
throw new InteractiveSlideException('Resource not set.', 400);
158157
}
159158

160159
return $this->interactiveSlideCache->get(self::CACHE_KEY_OPTIONS_PREFIX.$resource,
@@ -168,7 +167,7 @@ function (CacheItemInterface $item) use ($slide, $resource, $interactionRequest)
168167
$interactive = $this->interactiveService->getInteractiveSlide($tenant, $interactionRequest->implementationClass);
169168

170169
if (null === $interactive) {
171-
throw new \Exception('InteractiveSlide not found');
170+
throw new InteractiveSlideException('InteractiveSlide not found', 400);
172171
}
173172

174173
// Optional limiting of available resources.
@@ -177,11 +176,11 @@ function (CacheItemInterface $item) use ($slide, $resource, $interactionRequest)
177176
$feed = $slide->getFeed();
178177

179178
if (null === $feed) {
180-
throw new \Exception('Slide feed not set.');
179+
throw new InteractiveSlideException('Slide feed not set.', 400);
181180
}
182181

183182
if (!in_array($resource, $feed->getConfiguration()['resources'] ?? [])) {
184-
throw new \Exception('Resource not in feed resources');
183+
throw new InteractiveSlideException('Resource not in feed resources', 400);
185184
}
186185

187186
$token = $this->getToken($tenant, $interactive);
@@ -273,7 +272,7 @@ function (CacheItemInterface $item) use ($now): \DateTime {
273272
);
274273

275274
if ($lastRequestDateTime->add(new \DateInterval(self::CACHE_LIFETIME_QUICK_BOOK_SPAM_PROTECT)) > $now) {
276-
throw new ServiceUnavailableHttpException(60);
275+
throw new InteractiveSlideException('Service unavailable', 503);
277276
}
278277

279278
/** @var User|ScreenUser $activeUser */
@@ -283,7 +282,7 @@ function (CacheItemInterface $item) use ($now): \DateTime {
283282
$interactive = $this->interactiveService->getInteractiveSlide($tenant, $interactionRequest->implementationClass);
284283

285284
if (null === $interactive) {
286-
throw new BadRequestHttpException('Interactive not found');
285+
throw new InteractiveSlideException('Interactive not found', 400);
287286
}
288287

289288
// Optional limiting of available resources.
@@ -292,19 +291,19 @@ function (CacheItemInterface $item) use ($now): \DateTime {
292291
$feed = $slide->getFeed();
293292

294293
if (null === $feed) {
295-
throw new BadRequestHttpException('Slide feed not set.');
294+
throw new InteractiveSlideException('Slide feed not set.', 400);
296295
}
297296

298297
if (!in_array($resource, $feed->getConfiguration()['resources'] ?? [])) {
299-
throw new BadRequestHttpException('Resource not in feed resources');
298+
throw new InteractiveSlideException('Resource not in feed resources', 400);
300299
}
301300

302301
$token = $this->getToken($tenant, $interactive);
303302

304303
$configuration = $interactive->getConfiguration();
305304

306305
if (null === $configuration) {
307-
throw new BadRequestHttpException('Interactive no configuration');
306+
throw new InteractiveSlideException('Interactive has no configuration', 400);
308307
}
309308

310309
$username = $this->keyValueService->getValue($configuration['username']);
@@ -315,7 +314,7 @@ function (CacheItemInterface $item) use ($now): \DateTime {
315314
// Make sure interval is free.
316315
$busyIntervals = $this->getBusyIntervals($token, [$resource], $start, $startPlusDuration);
317316
if (count($busyIntervals[$resource]) > 0) {
318-
throw new ConflictHttpException('Interval booked already');
317+
throw new InteractiveSlideException('Interval booked already', 409);
319318
}
320319

321320
$requestBody = [
@@ -419,18 +418,21 @@ public function intervalFree(array $schedule, \DateTime $from, \DateTime $to): b
419418
return true;
420419
}
421420

421+
/**
422+
* @throws InteractiveSlideException
423+
*/
422424
private function getValueFromInterval(string $key, InteractionSlideRequest $interactionRequest): string|int
423425
{
424426
$interval = $interactionRequest->data['interval'] ?? null;
425427

426428
if (null === $interval) {
427-
throw new BadRequestHttpException('interval not set.');
429+
throw new InteractiveSlideException('interval not set.', 400);
428430
}
429431

430432
$value = $interval[$key] ?? null;
431433

432434
if (null === $value) {
433-
throw new BadRequestHttpException("interval.'.$key.' not set.");
435+
throw new InteractiveSlideException("interval.'.$key.' not set.", 400);
434436
}
435437

436438
return $value;
@@ -445,6 +447,9 @@ private function getHeaders(string $token): array
445447
];
446448
}
447449

450+
/**
451+
* @throws InteractiveSlideException
452+
*/
448453
private function checkPermission(InteractiveSlide $interactive, string $resource): void
449454
{
450455
$configuration = $interactive->getConfiguration();
@@ -453,7 +458,7 @@ private function checkPermission(InteractiveSlide $interactive, string $resource
453458
$allowedResources = $this->getAllowedResources($interactive);
454459

455460
if (!in_array($resource, $allowedResources)) {
456-
throw new \Exception('Not allowed');
461+
throw new InteractiveSlideException('Not allowed', 403);
457462
}
458463
}
459464
}
@@ -468,13 +473,13 @@ private function getAllowedResources(InteractiveSlide $interactive): array
468473
$key = $configuration['resourceEndpoint'] ?? null;
469474

470475
if (null === $key) {
471-
throw new \Exception('resourceEndpoint not set');
476+
throw new InteractiveSlideException('resourceEndpoint not set', 400);
472477
}
473478

474479
$resourceEndpoint = $this->keyValueService->getValue($key);
475480

476481
if (null === $resourceEndpoint) {
477-
throw new \Exception('resourceEndpoint value not set');
482+
throw new InteractiveSlideException('resourceEndpoint value not set', 400);
478483
}
479484

480485
$response = $this->client->request('GET', $resourceEndpoint);

0 commit comments

Comments
 (0)