Skip to content

Commit

Permalink
Fix the Screenshot and Icon Normalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jan 30, 2024
1 parent b983496 commit b561253
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/Normalizer/IconNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
9 changes: 4 additions & 5 deletions src/Normalizer/ScreenshotNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check failure on line 39 in src/Normalizer/ScreenshotNormalizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #2 $sourcePath of method SpomkyLabs\PwaBundle\Normalizer\ScreenshotNormalizer::getFormat() expects string, string|null given.

$result = [
'src' => $url,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b561253

Please sign in to comment.