From b5612534756a827b993e9f84271c4e80a472cac3 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Tue, 30 Jan 2024 12:32:50 +0100 Subject: [PATCH] Fix the Screenshot and Icon Normalizers --- src/Normalizer/IconNormalizer.php | 10 ++++------ src/Normalizer/ScreenshotNormalizer.php | 9 ++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Normalizer/IconNormalizer.php b/src/Normalizer/IconNormalizer.php index b591849..5787f95 100644 --- a/src/Normalizer/IconNormalizer.php +++ b/src/Normalizer/IconNormalizer.php @@ -6,7 +6,6 @@ use SpomkyLabs\PwaBundle\Dto\Icon; use Symfony\Component\AssetMapper\AssetMapperInterface; -use Symfony\Component\AssetMapper\MappedAsset; use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use function assert; @@ -33,7 +32,7 @@ public function normalize(mixed $object, string $format = null, array $context = if ($url === null) { $url = $object->src; } - $format = $this->getFormat($object, $asset); + $format = $this->getFormat($object, $asset === null ? $object->src : $asset->sourcePath); $result = [ 'src' => $url, @@ -65,17 +64,16 @@ public function getSupportedTypes(?string $format): array ]; } - private function getFormat(Icon $object, ?MappedAsset $asset): ?string + private function getFormat(Icon $object, string $sourcePath): ?string { if ($object->format !== null) { return $object->format; } - if ($asset === null || ! class_exists(MimeTypes::class)) { + if (! file_exists($sourcePath) || ! class_exists(MimeTypes::class)) { return null; } - $mime = MimeTypes::getDefault(); - return $mime->guessMimeType($asset->sourcePath); + return MimeTypes::getDefault()->guessMimeType($sourcePath); } } diff --git a/src/Normalizer/ScreenshotNormalizer.php b/src/Normalizer/ScreenshotNormalizer.php index 22ef59f..d97238f 100644 --- a/src/Normalizer/ScreenshotNormalizer.php +++ b/src/Normalizer/ScreenshotNormalizer.php @@ -36,7 +36,7 @@ public function normalize(mixed $object, string $format = null, array $context = $url = $object->src; } ['sizes' => $sizes, 'formFactor' => $formFactor] = $this->getSizes($object, $asset); - $format = $this->getFormat($object, $asset); + $format = $this->getFormat($object, $asset === null ? $object->src : $asset->sourcePath); $result = [ 'src' => $url, @@ -98,18 +98,17 @@ private function getSizes(Screenshot $object, null|MappedAsset $asset): array ]; } - private function getFormat(Screenshot $object, ?MappedAsset $asset): ?string + private function getFormat(Screenshot $object, string $sourcePath): ?string { if ($object->format !== null) { return $object->format; } - if ($this->imageProcessor === null || $asset === null || ! class_exists(MimeTypes::class)) { + if (! file_exists($sourcePath) || ! class_exists(MimeTypes::class)) { return null; } - $mime = MimeTypes::getDefault(); - return $mime->guessMimeType($asset->sourcePath); + return MimeTypes::getDefault()->guessMimeType($sourcePath); } private function getFormFactor(?int $width, ?int $height): ?string