src/Service/Serializer/ResolveCustomerFileContentUrlSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\Service\Serializer;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Model\CustomerFile\CustomerFile;
  5. use App\Model\CustomerFolder\CustomerFolder;
  6. use App\Model\Prospect\Prospect;
  7. use App\Model\Quote\Quote;
  8. use App\Model\Task\Task;
  9. use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\Routing\RouterInterface;
  17. /**
  18.  * Class ResolveCustomerFileContentUrlSubscriber.
  19.  */
  20. final class ResolveCustomerFileContentUrlSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var RouterInterface
  24.      */
  25.     private $router;
  26.     /**
  27.      * @var RequestStack
  28.      */
  29.     private $requestStack;
  30.     /**
  31.      * @var TokenExtractorInterface
  32.      */
  33.     private $tokenExtractor;
  34.     /**
  35.      * ResolveCustomerFileContentUrlSubscriber constructor.
  36.      *
  37.      * @param RouterInterface         $router
  38.      * @param RequestStack            $requestStack
  39.      * @param TokenExtractorInterface $tokenExtractor
  40.      */
  41.     public function __construct(
  42.         RouterInterface $router,
  43.         RequestStack $requestStack,
  44.         TokenExtractorInterface $tokenExtractor
  45.     ) {
  46.         $this->router $router;
  47.         $this->requestStack $requestStack;
  48.         $this->tokenExtractor $tokenExtractor;
  49.     }
  50.     /**
  51.      * @return array
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             KernelEvents::VIEW => ['onPreSerialize'EventPriorities::PRE_SERIALIZE],
  57.         ];
  58.     }
  59.     /**
  60.      * @param ViewEvent $event
  61.      */
  62.     public function onPreSerialize(ViewEvent $event): void
  63.     {
  64.         $controllerResult $event->getControllerResult();
  65.         $request $event->getRequest();
  66.         if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond'true)) {
  67.             return;
  68.         }
  69.         if (!$controllerResult instanceof Prospect && !$controllerResult instanceof Task
  70.             && !$controllerResult instanceof Quote) {
  71.             return;
  72.         }
  73.         $customerFiles $controllerResult->getCustomerFiles();
  74.         if (!is_iterable($customerFiles)) {
  75.             $customerFiles = [$customerFiles];
  76.         }
  77.         foreach ($customerFiles as $customerFile) {
  78.             if (!$customerFile instanceof CustomerFile && !$customerFile instanceof CustomerFolder) {
  79.                 continue;
  80.             }
  81.             if ($customerFile instanceof CustomerFile) {
  82.                 $customerFile->setDownloadLink(
  83.                     $this->router->generate(
  84.                         'files_download_route',
  85.                         ['id' => $customerFile->getId()],
  86.                         UrlGeneratorInterface::ABSOLUTE_URL
  87.                     )
  88.                 );
  89.                 $customerFile->setDeleteLink(
  90.                     $this->router->generate(
  91.                         'customer_files_delete_route',
  92.                         ['id' => $customerFile->getId()],
  93.                         UrlGeneratorInterface::ABSOLUTE_URL
  94.                     )
  95.                 );
  96.             }
  97.         }
  98.     }
  99. }