src/EventSubscriber/CheckNumberUndanewsMediaObjectSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Undanews;
  5. use App\Entity\UndanewsMediaObject;
  6. use App\Exception\Api\UndanewsMediaObjectMaximumNumberException;
  7. use App\Repository\UndanewsMediaObjectRepository;
  8. use App\Repository\UndanewsRepository;
  9. use App\Service\RemoveMediaObjectsNotLinkedService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. class CheckNumberUndanewsMediaObjectSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var UndanewsMediaObjectRepository
  19.      */
  20.     private $undanewsMediaObjectRepository;
  21.     /**
  22.      * @var UndanewsRepository
  23.      */
  24.     private $undanewsRepository;
  25.     /**
  26.      * @var Filesystem
  27.      */
  28.     private $filesystem;
  29.     /**
  30.      * @var RemoveMediaObjectsNotLinkedService
  31.      */
  32.     private $removeMediaObjectsNotLinkedService;
  33.     public function __construct(UndanewsMediaObjectRepository $undanewsMediaObjectRepository,
  34.                                 UndanewsRepository $undanewsRepositoryFilesystem $filesystemRemoveMediaObjectsNotLinkedService $removeMediaObjectsNotLinkedService)
  35.     {
  36.         $this->undanewsMediaObjectRepository $undanewsMediaObjectRepository;
  37.         $this->undanewsRepository $undanewsRepository;
  38.         $this->filesystem $filesystem;
  39.         $this->removeMediaObjectsNotLinkedService $removeMediaObjectsNotLinkedService;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             KernelEvents::VIEW => ['checkNumberUndanewsMediaObject'EventPriorities::POST_WRITE],
  45.         ];
  46.     }
  47.     /**
  48.      * @throws UndanewsMediaObjectMaximumNumberException
  49.      */
  50.     public function checkNumberUndanewsMediaObject(ViewEvent $viewEvent)
  51.     {
  52.         $undanews $viewEvent->getControllerResult();
  53.         $method $viewEvent->getRequest()->getMethod();
  54.         if (!$undanews instanceof Undanews || (Request::METHOD_PUT !== $method && Request::METHOD_POST !== $method)) {
  55.             return;
  56.         }
  57.         $json_decode json_decode($viewEvent->getRequest()->getContent(), true);
  58.         if(isset($json_decode['undanewsMediaObjects'])) {
  59.             $undanewsMediaObjetsRequest $json_decode['undanewsMediaObjects'];
  60.         } else {
  61.             return;
  62.         }
  63.         $numberOfUndanewsMediaObjetsAssigned $this->undanewsMediaObjectRepository->count(['undanews' => $undanews->getId()]);
  64.         /** @var UndanewsMediaObject $undanewsMediaObjetsNotAssigned */
  65.         $undanewsMediaObjetsNotAssigned $this->undanewsMediaObjectRepository->findBy(['undanews' => null]);
  66.         /** @var UndanewsMediaObject $itemToRemove */
  67.         foreach ($undanewsMediaObjetsNotAssigned as $itemToRemove) {
  68.             $this->filesystem->remove($itemToRemove->getFile());
  69.             $this->undanewsMediaObjectRepository->remove($itemToRemovetrue);
  70.         }
  71.         if (count($undanewsMediaObjetsRequest) > $numberOfUndanewsMediaObjetsAssigned) {
  72.             throw new UndanewsMediaObjectMaximumNumberException(sprintf('The undanews %s whith title %s has the maximum number of photos %s'$undanews->getId(), $undanews->getTitle(), count($undanews->getUndanewsMediaObjects())));
  73.         }
  74.     }
  75. }