src/EventSubscriber/ResolveMediaObjectNewsItemUserContentUrlSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Vich\UploaderBundle\Storage\StorageInterface;
  10. use App\Entity\MediaObjectNewsItemUser;
  11. final class ResolveMediaObjectNewsItemUserContentUrlSubscriber implements EventSubscriberInterface
  12. {
  13.     private $storage;
  14.     public function __construct(StorageInterface $storage)
  15.     {
  16.         $this->storage $storage;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             KernelEvents::VIEW => ['onPreSerialize'EventPriorities::PRE_SERIALIZE],
  22.         ];
  23.     }
  24.     public function onPreSerialize(ViewEvent $event): void
  25.     {
  26.         $controllerResult $event->getControllerResult();
  27.         $request $event->getRequest();
  28.         if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond'true)) {
  29.             return;
  30.         }
  31.         if (!($attributes RequestAttributesExtractor::extractAttributes($request)) || !is_a($attributes['resource_class'], MediaObjectNewsItemUser::class, true)) {
  32.             return;
  33.         }
  34.         $mediaObjects $controllerResult;
  35.         if (!is_iterable($mediaObjects)) {
  36.             $mediaObjects = [$mediaObjects];
  37.         }
  38.         foreach ($mediaObjects as $mediaObject) {
  39.             if (!$mediaObject instanceof MediaObjectNewsItemUser ) {
  40.                     
  41.                 continue;
  42.             }
  43.             $mediaObject->contentUrl $this->storage->resolveUri($mediaObject'file');
  44.         }
  45.     }
  46. }