src/EventSubscriber/CheckNumberUndanitaMediaObjectSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Undanita;
  5. use App\Entity\UndanitaMediaObject;
  6. use App\Exception\Api\UndanitaMediaObjectMaximumNumberException;
  7. use App\Repository\UndanitaMediaObjectRepository;
  8. use phpDocumentor\Reflection\DocBlock\Tags\Return_;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class CheckNumberUndanitaMediaObjectSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var UndanitaMediaObjectRepository
  18.      */
  19.     private $undanitaMediaObjectRepository;
  20.     /**
  21.      * @var Filesystem
  22.      */
  23.     private $filesystem;
  24.     public function __construct(UndanitaMediaObjectRepository $undanitaMediaObjectRepositoryFilesystem $filesystem)
  25.     {
  26.         $this->undanitaMediaObjectRepository $undanitaMediaObjectRepository;
  27.         $this->filesystem $filesystem;
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             KernelEvents::VIEW => ['checkNumberUndanitaMediaObject'EventPriorities::POST_WRITE],
  33.         ];
  34.     }
  35.     /**
  36.      * @throws UndanitaMediaObjectMaximumNumberException
  37.      */
  38.     public function checkNumberUndanitaMediaObject(ViewEvent $viewEvent)
  39.     {
  40.         $undanita $viewEvent->getControllerResult();
  41.         $method $viewEvent->getRequest()->getMethod();
  42.         if (!$undanita instanceof Undanita || (Request::METHOD_PUT !== $method && Request::METHOD_POST !== $method)) {
  43.             return;
  44.         }
  45.         $json_decode json_decode($viewEvent->getRequest()->getContent(), true);
  46.         if(isset($json_decode['undanitaMediaObjects'])) {
  47.             $undanitaMediaObjetsRequest $json_decode['undanitaMediaObjects'];
  48.         } else {
  49.             return;
  50.         }
  51.         $numberOfUndanitaMediaObjetsAssigned $this->undanitaMediaObjectRepository->count(['undanita' => $undanita->getId()]);
  52.         /** @var UndanitaMediaObject $undanitaMediaObjetsNotAssigned */
  53.         $undanitaMediaObjetsNotAssigned $this->undanitaMediaObjectRepository->findBy(['undanita' => null]);
  54.         /** @var UndanitaMediaObject $itemToRemove */
  55.         foreach ($undanitaMediaObjetsNotAssigned as $itemToRemove) {
  56.             $this->filesystem->remove($itemToRemove->getFile());
  57.             $this->undanitaMediaObjectRepository->remove($itemToRemovetrue);
  58.         }
  59.         if (count($undanitaMediaObjetsRequest) > $numberOfUndanitaMediaObjetsAssigned) {
  60.             throw new UndanitaMediaObjectMaximumNumberException(sprintf('The undanita %s whith title %s has the maximum number of photos %s'$undanita->getId(), $undanita->getTitle(), count($undanita->getUndanitaMediaObjects())));
  61.         }
  62.     }
  63. }