| @@ -6,19 +6,18 @@ use App\ApiResource\ContactApi; | |||
| use App\ApiResource\PartnerApi; | |||
| use App\ApiResource\PostingApi; | |||
| use App\Entity\Contact; | |||
| use App\Entity\Partner; | |||
| use App\Entity\Posting; | |||
| use App\Service\FileUrlService; | |||
| use Symfonycasts\MicroMapper\AsMapper; | |||
| use Symfonycasts\MicroMapper\MapperInterface; | |||
| use Symfonycasts\MicroMapper\MicroMapperInterface; | |||
| use Vich\UploaderBundle\Storage\StorageInterface; | |||
| #[AsMapper(from: Contact::class, to: ContactApi::class)] | |||
| class ContactEntityToApiMapper implements MapperInterface | |||
| { | |||
| public function __construct( | |||
| private MicroMapperInterface $microMapper, | |||
| private StorageInterface $storage | |||
| private FileUrlService $fileUrlService | |||
| ) | |||
| { | |||
| } | |||
| @@ -50,7 +49,7 @@ class ContactEntityToApiMapper implements MapperInterface | |||
| ); | |||
| $dto->birthday = $entity->getBirthday(); | |||
| $dto->image = $entity->getImage(); | |||
| $dto->imageUrl = $entity->getImage() !== null ? $this->storage->resolveUri($entity->getImage(), 'file') : null; | |||
| $dto->imageUrl = $this->fileUrlService->getFileUrl($entity->getImage()); | |||
| $dto->position = $entity->getPosition(); | |||
| $dto->phone = $entity->getPhone(); | |||
| $dto->email = $entity->getEmail(); | |||
| @@ -6,6 +6,7 @@ use App\ApiResource\ContactApi; | |||
| use App\ApiResource\PartnerApi; | |||
| use App\Entity\Contact; | |||
| use App\Entity\Partner; | |||
| use App\Service\FileUrlService; | |||
| use Symfonycasts\MicroMapper\AsMapper; | |||
| use Symfonycasts\MicroMapper\MapperInterface; | |||
| use Symfonycasts\MicroMapper\MicroMapperInterface; | |||
| @@ -16,7 +17,7 @@ class PartnerEntityToApiMapper implements MapperInterface | |||
| { | |||
| public function __construct( | |||
| private MicroMapperInterface $microMapper, | |||
| private StorageInterface $storage | |||
| private FileUrlService $fileUrlService | |||
| ) { | |||
| } | |||
| @@ -47,7 +48,7 @@ class PartnerEntityToApiMapper implements MapperInterface | |||
| $dto->country = $entity->getCountry(); | |||
| $dto->website = $entity->getWebsite(); | |||
| $dto->logo = $entity->getLogo(); | |||
| $dto->logoUrl = $entity->getLogo() !== null ? $this->storage->resolveUri($entity->getLogo(), 'file') : null; | |||
| $dto->logoUrl = $this->fileUrlService->getFileUrl($entity->getLogo()); | |||
| $dto->createdAt = $entity->getCreatedAt(); | |||
| $dto->contacts = array_map(function(Contact $contact) { | |||
| return $this->microMapper->map($contact, ContactApi::class, [ | |||
| @@ -0,0 +1,66 @@ | |||
| <?php | |||
| /** | |||
| * @author Daniel Knudsen <d.knudsen@spawntree.de> | |||
| * @date 25.01.24 | |||
| */ | |||
| namespace App\Serializer; | |||
| use App\Entity\MediaObject; | |||
| use Symfony\Component\DependencyInjection\Attribute\AsDecorator; | |||
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |||
| use Symfony\Component\Serializer\SerializerAwareInterface; | |||
| use Symfony\Component\Serializer\SerializerInterface; | |||
| use Vich\UploaderBundle\Storage\StorageInterface; | |||
| #[AsDecorator('api_platform.jsonld.normalizer.item')] | |||
| final class MediaObjectNormalizer implements NormalizerInterface, SerializerAwareInterface | |||
| { | |||
| private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED'; | |||
| public function __construct( | |||
| private StorageInterface $storage, | |||
| private NormalizerInterface $normalizer | |||
| ) | |||
| { | |||
| } | |||
| public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null | |||
| { | |||
| $context[self::ALREADY_CALLED] = true; | |||
| if ($object instanceof MediaObject) { | |||
| // Nur für MediaObject die URI generieren | |||
| $object->contentUrl = $this->storage->resolveUri($object, 'file'); | |||
| } | |||
| return $this->normalizer->normalize($object, $format, $context); | |||
| } | |||
| public function supportsNormalization($data, ?string $format = null, array $context = []): bool | |||
| { | |||
| if (isset($context[self::ALREADY_CALLED])) { | |||
| return false; | |||
| } | |||
| return $this->normalizer->supportsNormalization($data, $format, $context); | |||
| } | |||
| public function setSerializer(SerializerInterface $serializer): void | |||
| { | |||
| if ($this->normalizer instanceof SerializerAwareInterface) { | |||
| $this->normalizer->setSerializer($serializer); | |||
| } | |||
| } | |||
| public function getSupportedTypes(?string $format): array | |||
| { | |||
| if (method_exists($this->normalizer, 'getSupportedTypes')) { | |||
| return $this->normalizer->getSupportedTypes($format); | |||
| } | |||
| return 'jsonld' === $format ? ['*' => true] : []; | |||
| } | |||
| } | |||
| @@ -0,0 +1,35 @@ | |||
| <?php | |||
| /** | |||
| * @author Daniel Knudsen <d.knudsen@spawntree.de> | |||
| * @date 16.02.24 | |||
| */ | |||
| namespace App\Service; | |||
| use App\Entity\MediaObject; | |||
| use Symfony\Component\HttpFoundation\RequestStack; | |||
| use Vich\UploaderBundle\Storage\StorageInterface; | |||
| class FileUrlService | |||
| { | |||
| public function __construct( | |||
| private RequestStack $requestStack, | |||
| private StorageInterface $storage, | |||
| ){} | |||
| public function getFileUrl(?MediaObject $mediaObject) | |||
| { | |||
| if ($mediaObject !== null) { | |||
| $currentRequest = $this->requestStack->getCurrentRequest(); | |||
| if ($currentRequest !== null) { | |||
| $apiUrl = $currentRequest->getSchemeAndHttpHost(); | |||
| $imageUri = $this->storage->resolveUri($mediaObject, 'file'); | |||
| return $apiUrl . $imageUri; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| } | |||