Skip to content

Commit 2144d0d

Browse files
committed
cr remarks 3
1 parent 77b3865 commit 2144d0d

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,6 @@ parameters:
792792
count: 1
793793
path: src/lib/Input/Handler/Json.php
794794

795-
-
796-
message: '#^Argument of an invalid type \(DOMNamedNodeMap&iterable\<DOMAttr\>\)\|null supplied for foreach, only iterables are supported\.$#'
797-
identifier: foreach.nonIterable
798-
count: 1
799-
path: src/lib/Input/Handler/Xml.php
800-
801795
-
802796
message: '#^Cannot assign new offset to array\|string\.$#'
803797
identifier: offsetAssign.dimType

src/bundle/EventListener/SupportedMediaTypesSubscriber.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Ibexa\Bundle\Rest\EventListener;
1010

11+
use RuntimeException;
1112
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1213
use Symfony\Component\HttpFoundation\Response;
1314
use Symfony\Component\HttpKernel\Event\RequestEvent;
@@ -39,16 +40,24 @@ public function allowOnlySupportedMediaTypes(RequestEvent $event): void
3940
return;
4041
}
4142

43+
$contentTypeHeader = $request->headers->get('Content-Type') ?? '';
4244
$acceptHeader = $request->headers->get('Accept') ?? '';
4345

44-
preg_match(self::SUPPORTED_MEDIA_TYPES_REGEX, $acceptHeader, $matches);
46+
try {
47+
$isContentTypeHeaderSupported = $this->isMediaTypeSupported(
48+
$contentTypeHeader,
49+
$supportedMediaTypes
50+
);
4551

46-
$match = reset($matches);
47-
if ($match === false) {
48-
return;
49-
}
52+
$isAcceptHeaderSupported = $this->isMediaTypeSupported(
53+
$acceptHeader,
54+
$supportedMediaTypes
55+
);
5056

51-
if (in_array($match, $supportedMediaTypes, true)) {
57+
if ($isContentTypeHeaderSupported && $isAcceptHeaderSupported) {
58+
return;
59+
}
60+
} catch (RuntimeException $e) {
5261
return;
5362
}
5463

@@ -61,4 +70,24 @@ public function allowOnlySupportedMediaTypes(RequestEvent $event): void
6170
Response::HTTP_UNSUPPORTED_MEDIA_TYPE
6271
);
6372
}
73+
74+
/**
75+
* @param string[] $supportedMediaTypes
76+
*/
77+
private function isMediaTypeSupported(
78+
string $header,
79+
array $supportedMediaTypes
80+
): bool {
81+
preg_match(self::SUPPORTED_MEDIA_TYPES_REGEX, $header, $matches);
82+
83+
$match = reset($matches);
84+
if ($match === false) {
85+
throw new RuntimeException(sprintf(
86+
'Failed to extract media type from header: %s',
87+
$header
88+
));
89+
}
90+
91+
return in_array($match, $supportedMediaTypes, true);
92+
}
6493
}

tests/bundle/EventListener/SupportedMediaTypesSubscriberTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function testDoesNothingWhenMediaTypeIsSupported(): void
5656
$request = new Request();
5757
$request->attributes->set('supported_media_types', ['json', 'xml']);
5858
$request->headers = new HeaderBag([
59+
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+json',
5960
'Accept' => 'application/vnd.ibexa.api.ContentCreate+json',
6061
]);
6162

@@ -72,6 +73,7 @@ public function testThrowsExceptionWhenHeaderTypeIsNotSupported(): void
7273
$request = new Request();
7374
$request->attributes->set('supported_media_types', ['json']);
7475
$request->headers = new HeaderBag([
76+
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+xml',
7577
'Accept' => 'application/vnd.ibexa.api.ContentCreate+xml',
7678
]);
7779

@@ -87,6 +89,7 @@ public function testThrowsExceptionWhenUnknownMediaTypeIsUsed(): void
8789
$request = new Request();
8890
$request->attributes->set('supported_media_types', ['yaml']);
8991
$request->headers = new HeaderBag([
92+
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+unknown',
9093
'Accept' => 'application/vnd.ibexa.api.ContentCreate+unknown',
9194
]);
9295

@@ -97,11 +100,28 @@ public function testThrowsExceptionWhenUnknownMediaTypeIsUsed(): void
97100
$subscriber->allowOnlySupportedMediaTypes($event);
98101
}
99102

103+
public function testThrowsExceptionWhenDifferentMediaTypesInHeadersAreUsed(): void
104+
{
105+
$request = new Request();
106+
$request->attributes->set('supported_media_types', ['json']);
107+
$request->headers = new HeaderBag([
108+
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+json',
109+
'Accept' => 'application/vnd.ibexa.api.ContentCreate+xml',
110+
]);
111+
112+
$subscriber = new SupportedMediaTypesSubscriber();
113+
$event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST);
114+
115+
$this->expectException(UnsupportedMediaTypeHttpException::class);
116+
$subscriber->allowOnlySupportedMediaTypes($event);
117+
}
118+
100119
public function testApplicationJsonHeaderIsSupported(): void
101120
{
102121
$request = new Request();
103122
$request->attributes->set('supported_media_types', ['json']);
104123
$request->headers = new HeaderBag([
124+
'Content-Type' => 'application/json',
105125
'Accept' => 'application/json',
106126
]);
107127

0 commit comments

Comments
 (0)