9
9
use App \Entity \Tenant \InteractiveSlide ;
10
10
use App \Entity \Tenant \Slide ;
11
11
use App \Entity \User ;
12
+ use App \Exceptions \InteractiveSlideException ;
12
13
use App \Service \InteractiveSlideService ;
13
14
use App \Service \KeyVaultService ;
14
15
use Psr \Cache \CacheItemInterface ;
15
16
use Psr \Cache \InvalidArgumentException ;
16
17
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 ;
20
18
use Symfony \Component \Security \Core \User \UserInterface ;
21
19
use Symfony \Contracts \Cache \CacheInterface ;
22
20
use Symfony \Contracts \HttpClient \HttpClientInterface ;
@@ -90,7 +88,7 @@ public function performAction(UserInterface $user, Slide $slide, InteractionSlid
90
88
return match ($ interactionRequest ->action ) {
91
89
self ::ACTION_GET_QUICK_BOOK_OPTIONS => $ this ->getQuickBookOptions ($ slide , $ interactionRequest ),
92
90
self ::ACTION_QUICK_BOOK => $ this ->quickBook ($ slide , $ interactionRequest ),
93
- default => throw new BadRequestHttpException ('Action not allowed ' ),
91
+ default => throw new InteractiveSlideException ('Action not allowed ' ),
94
92
};
95
93
}
96
94
@@ -105,7 +103,7 @@ private function authenticate(array $configuration): array
105
103
$ password = $ this ->keyValueService ->getValue ($ configuration ['password ' ]);
106
104
107
105
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. ' );
109
107
}
110
108
111
109
$ url = self ::LOGIN_ENDPOINT .$ tenantId .self ::OAUTH_PATH ;
@@ -124,14 +122,14 @@ private function authenticate(array $configuration): array
124
122
}
125
123
126
124
/**
127
- * @throws InvalidArgumentException
125
+ * @throws InteractiveSlideException| InvalidArgumentException
128
126
*/
129
127
private function getToken (Tenant $ tenant , InteractiveSlide $ interactive ): string
130
128
{
131
129
$ configuration = $ interactive ->getConfiguration ();
132
130
133
131
if (null === $ configuration ) {
134
- throw new BadRequestHttpException ('InteractiveSlide has no configuration ' );
132
+ throw new InteractiveSlideException ('InteractiveSlide has no configuration ' );
135
133
}
136
134
137
135
return $ this ->interactiveSlideCache ->get (
@@ -147,14 +145,15 @@ function (CacheItemInterface $item) use ($configuration): mixed {
147
145
}
148
146
149
147
/**
150
- * @throws \Throwable
148
+ * @throws InteractiveSlideException
149
+ * @throws InvalidArgumentException
151
150
*/
152
151
private function getQuickBookOptions (Slide $ slide , InteractionSlideRequest $ interactionRequest ): array
153
152
{
154
153
$ resource = $ interactionRequest ->data ['resource ' ] ?? null ;
155
154
156
155
if (null === $ resource ) {
157
- throw new \ Exception ('Resource not set. ' );
156
+ throw new InteractiveSlideException ('Resource not set. ' , 400 );
158
157
}
159
158
160
159
return $ this ->interactiveSlideCache ->get (self ::CACHE_KEY_OPTIONS_PREFIX .$ resource ,
@@ -168,7 +167,7 @@ function (CacheItemInterface $item) use ($slide, $resource, $interactionRequest)
168
167
$ interactive = $ this ->interactiveService ->getInteractiveSlide ($ tenant , $ interactionRequest ->implementationClass );
169
168
170
169
if (null === $ interactive ) {
171
- throw new \ Exception ('InteractiveSlide not found ' );
170
+ throw new InteractiveSlideException ('InteractiveSlide not found ' , 400 );
172
171
}
173
172
174
173
// Optional limiting of available resources.
@@ -177,11 +176,11 @@ function (CacheItemInterface $item) use ($slide, $resource, $interactionRequest)
177
176
$ feed = $ slide ->getFeed ();
178
177
179
178
if (null === $ feed ) {
180
- throw new \ Exception ('Slide feed not set. ' );
179
+ throw new InteractiveSlideException ('Slide feed not set. ' , 400 );
181
180
}
182
181
183
182
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 );
185
184
}
186
185
187
186
$ token = $ this ->getToken ($ tenant , $ interactive );
@@ -273,7 +272,7 @@ function (CacheItemInterface $item) use ($now): \DateTime {
273
272
);
274
273
275
274
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 );
277
276
}
278
277
279
278
/** @var User|ScreenUser $activeUser */
@@ -283,7 +282,7 @@ function (CacheItemInterface $item) use ($now): \DateTime {
283
282
$ interactive = $ this ->interactiveService ->getInteractiveSlide ($ tenant , $ interactionRequest ->implementationClass );
284
283
285
284
if (null === $ interactive ) {
286
- throw new BadRequestHttpException ('Interactive not found ' );
285
+ throw new InteractiveSlideException ('Interactive not found ' , 400 );
287
286
}
288
287
289
288
// Optional limiting of available resources.
@@ -292,19 +291,19 @@ function (CacheItemInterface $item) use ($now): \DateTime {
292
291
$ feed = $ slide ->getFeed ();
293
292
294
293
if (null === $ feed ) {
295
- throw new BadRequestHttpException ('Slide feed not set. ' );
294
+ throw new InteractiveSlideException ('Slide feed not set. ' , 400 );
296
295
}
297
296
298
297
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 );
300
299
}
301
300
302
301
$ token = $ this ->getToken ($ tenant , $ interactive );
303
302
304
303
$ configuration = $ interactive ->getConfiguration ();
305
304
306
305
if (null === $ configuration ) {
307
- throw new BadRequestHttpException ('Interactive no configuration ' );
306
+ throw new InteractiveSlideException ('Interactive has no configuration ' , 400 );
308
307
}
309
308
310
309
$ username = $ this ->keyValueService ->getValue ($ configuration ['username ' ]);
@@ -315,7 +314,7 @@ function (CacheItemInterface $item) use ($now): \DateTime {
315
314
// Make sure interval is free.
316
315
$ busyIntervals = $ this ->getBusyIntervals ($ token , [$ resource ], $ start , $ startPlusDuration );
317
316
if (count ($ busyIntervals [$ resource ]) > 0 ) {
318
- throw new ConflictHttpException ('Interval booked already ' );
317
+ throw new InteractiveSlideException ('Interval booked already ' , 409 );
319
318
}
320
319
321
320
$ requestBody = [
@@ -419,18 +418,21 @@ public function intervalFree(array $schedule, \DateTime $from, \DateTime $to): b
419
418
return true ;
420
419
}
421
420
421
+ /**
422
+ * @throws InteractiveSlideException
423
+ */
422
424
private function getValueFromInterval (string $ key , InteractionSlideRequest $ interactionRequest ): string |int
423
425
{
424
426
$ interval = $ interactionRequest ->data ['interval ' ] ?? null ;
425
427
426
428
if (null === $ interval ) {
427
- throw new BadRequestHttpException ('interval not set. ' );
429
+ throw new InteractiveSlideException ('interval not set. ' , 400 );
428
430
}
429
431
430
432
$ value = $ interval [$ key ] ?? null ;
431
433
432
434
if (null === $ value ) {
433
- throw new BadRequestHttpException ("interval.'. $ key.' not set. " );
435
+ throw new InteractiveSlideException ("interval.'. $ key.' not set. " , 400 );
434
436
}
435
437
436
438
return $ value ;
@@ -445,6 +447,9 @@ private function getHeaders(string $token): array
445
447
];
446
448
}
447
449
450
+ /**
451
+ * @throws InteractiveSlideException
452
+ */
448
453
private function checkPermission (InteractiveSlide $ interactive , string $ resource ): void
449
454
{
450
455
$ configuration = $ interactive ->getConfiguration ();
@@ -453,7 +458,7 @@ private function checkPermission(InteractiveSlide $interactive, string $resource
453
458
$ allowedResources = $ this ->getAllowedResources ($ interactive );
454
459
455
460
if (!in_array ($ resource , $ allowedResources )) {
456
- throw new \ Exception ('Not allowed ' );
461
+ throw new InteractiveSlideException ('Not allowed ' , 403 );
457
462
}
458
463
}
459
464
}
@@ -468,13 +473,13 @@ private function getAllowedResources(InteractiveSlide $interactive): array
468
473
$ key = $ configuration ['resourceEndpoint ' ] ?? null ;
469
474
470
475
if (null === $ key ) {
471
- throw new \ Exception ('resourceEndpoint not set ' );
476
+ throw new InteractiveSlideException ('resourceEndpoint not set ' , 400 );
472
477
}
473
478
474
479
$ resourceEndpoint = $ this ->keyValueService ->getValue ($ key );
475
480
476
481
if (null === $ resourceEndpoint ) {
477
- throw new \ Exception ('resourceEndpoint value not set ' );
482
+ throw new InteractiveSlideException ('resourceEndpoint value not set ' , 400 );
478
483
}
479
484
480
485
$ response = $ this ->client ->request ('GET ' , $ resourceEndpoint );
0 commit comments