Skip to content

Commit aa5ac91

Browse files
konradobozaadamwojs
authored andcommitted
cr remarks
1 parent 791ca99 commit aa5ac91

File tree

2 files changed

+18
-43
lines changed

2 files changed

+18
-43
lines changed

src/bundle/EventListener/SupportedMediaTypesSubscriber.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
final class SupportedMediaTypesSubscriber implements EventSubscriberInterface
1818
{
19-
private const SUPPORTED_MEDIA_TYPES_PATTERN = '(^application/vnd\.ibexa\.api(\.[A-Za-z]+)+\+%s$)';
19+
private const SUPPORTED_MEDIA_TYPES_REGEX = '/(?<=\+)[A-Za-z0-9]+/';
2020

2121
public static function getSubscribedEvents(): array
2222
{
@@ -35,33 +35,26 @@ public function allowOnlySupportedMediaTypes(RequestEvent $event): void
3535
}
3636

3737
$supportedMediaTypes = $request->attributes->get('supported_media_types');
38-
$regexps = array_map(
39-
static fn (string $mediaType): string => sprintf(
40-
self::SUPPORTED_MEDIA_TYPES_PATTERN,
41-
strtolower($mediaType)
42-
),
43-
$supportedMediaTypes
44-
);
38+
if (empty($supportedMediaTypes)) {
39+
return;
40+
}
4541

4642
$contentTypeHeader = $request->headers->get('Content-Type') ?? '';
47-
$acceptHeader = $request->headers->get('Accept') ?? '';
4843

49-
foreach ($regexps as $regexp) {
50-
if (
51-
preg_match($regexp, $contentTypeHeader) === 1
52-
|| preg_match($regexp, $acceptHeader) === 1
53-
) {
54-
break;
55-
}
44+
preg_match(self::SUPPORTED_MEDIA_TYPES_REGEX, $contentTypeHeader, $matches);
5645

57-
throw new UnsupportedMediaTypeHttpException(
58-
sprintf(
59-
'Unsupported media type was used. Available ones are: %s',
60-
implode(', ', $supportedMediaTypes)
61-
),
62-
null,
63-
Response::HTTP_UNSUPPORTED_MEDIA_TYPE
64-
);
46+
$match = reset($matches);
47+
if ($match !== false && in_array($match, $supportedMediaTypes, true)) {
48+
return;
6549
}
50+
51+
throw new UnsupportedMediaTypeHttpException(
52+
sprintf(
53+
'Unsupported media type was used. Available ones are: %s',
54+
implode(', ', $supportedMediaTypes)
55+
),
56+
null,
57+
Response::HTTP_UNSUPPORTED_MEDIA_TYPE
58+
);
6659
}
6760
}

tests/bundle/EventListener/SupportedMediaTypesSubscriberTest.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function testDoesNothingWhenSupportedMediaTypesParameterIsEmpty(): void
4848
$event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST);
4949

5050
$subscriber->allowOnlySupportedMediaTypes($event);
51-
5251
self::expectNotToPerformAssertions();
5352
}
5453

@@ -57,8 +56,7 @@ public function testDoesNothingWhenMediaTypeIsSupported(): void
5756
$request = new Request();
5857
$request->attributes->set('supported_media_types', ['json', 'xml']);
5958
$request->headers = new HeaderBag([
60-
'Content-Type' => 'application/vnd.ibexa.api.ContentCreat+json',
61-
'Accept' => 'application/vnd.ibexa.api.ContentCreat+json',
59+
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+json',
6260
]);
6361

6462
$subscriber = new SupportedMediaTypesSubscriber();
@@ -84,28 +82,12 @@ public function testThrowsExceptionWhenContentTypeHeaderTypeIsNotSupported(): vo
8482
$subscriber->allowOnlySupportedMediaTypes($event);
8583
}
8684

87-
public function testThrowsExceptionWhenAcceptHeaderTypeIsNotSupported(): void
88-
{
89-
$request = new Request();
90-
$request->attributes->set('supported_media_types', ['json']);
91-
$request->headers = new HeaderBag([
92-
'Accept' => 'application/vnd.ibexa.api.ContentCreate+xml',
93-
]);
94-
95-
$subscriber = new SupportedMediaTypesSubscriber();
96-
$event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST);
97-
98-
$this->expectException(UnsupportedMediaTypeHttpException::class);
99-
$subscriber->allowOnlySupportedMediaTypes($event);
100-
}
101-
10285
public function testThrowsExceptionWhenUnknownMediaTypeIsUsed(): void
10386
{
10487
$request = new Request();
10588
$request->attributes->set('supported_media_types', ['yaml']);
10689
$request->headers = new HeaderBag([
10790
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+unknown',
108-
'Accept' => 'application/vnd.ibexa.api.ContentCreate+unknown',
10991
]);
11092

11193
$subscriber = new SupportedMediaTypesSubscriber();

0 commit comments

Comments
 (0)