src/App/Doctrine/Listener/DownloadableContentListener.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Doctrine\Listener;
  3. use App\Contract\Content\DownloadableContent;
  4. use App\Entity\Deal;
  5. use App\Entity\Message\MessageReception;
  6. use App\Entity\Project;
  7. use App\Entity\ShopVideo;
  8. use App\RabbitMq\Handler\DownloadableContentHandler;
  9. use App\RabbitMq\TasksPool;
  10. use Doctrine\Common\Util\ClassUtils;
  11. use Doctrine\ORM\Event\PostLoadEventArgs;
  12. use Doctrine\ORM\Event\PostPersistEventArgs;
  13. use Doctrine\ORM\Event\PostUpdateEventArgs;
  14. use Symfony\Component\Console\ConsoleEvents;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. class DownloadableContentListener implements EventSubscriberInterface
  18. {
  19.     private TasksPool $tasksPool;
  20.     private bool $dirty false;
  21.     public function __construct(TasksPool $tasksPool)
  22.     {
  23.         $this->tasksPool $tasksPool;
  24.     }
  25.     /**
  26.      * @param Deal|Project|ShopVideo|MessageReception $content
  27.      */
  28.     public function postLoad(DownloadableContent $contentPostLoadEventArgs $args): void
  29.     {
  30.         $content->setWasAccessible($content->isContentAccessible());
  31.     }
  32.     /**
  33.      * @param Deal|Project|ShopVideo|MessageReception $content
  34.      */
  35.     public function postPersist(DownloadableContent $contentPostPersistEventArgs $args): void
  36.     {
  37.         $content->setWasAccessible($content->isContentAccessible());
  38.     }
  39.     /**
  40.      * @param Deal|Project|ShopVideo|MessageReception $content
  41.      */
  42.     public function postUpdate(DownloadableContent $contentPostUpdateEventArgs $args): void
  43.     {
  44.         // do not process new or unchanged content.
  45.         if (null === $content->getId() || $content->wasAccessible() === $content->isContentAccessible()) {
  46.             return;
  47.         }
  48.         $this->dirty true;
  49.         if (!$content->isContentAccessible()) {
  50.             $this->tasksPool->addTask(
  51.                 DownloadableContentHandler::class,
  52.                 'handle',
  53.                 ClassUtils::getClass($content),
  54.                 $content->getId(),
  55.                 [DownloadableContentHandler::REMOVE]
  56.             );
  57.             return;
  58.         }
  59.         $this->tasksPool->addTask(
  60.             DownloadableContentHandler::class,
  61.             'handle',
  62.             ClassUtils::getClass($content),
  63.             $content->getId(),
  64.             [DownloadableContentHandler::ADD]
  65.         );
  66.     }
  67.     public function sendPool(): void
  68.     {
  69.         if (!$this->dirty) {
  70.             return;
  71.         }
  72.         $this->tasksPool->send();
  73.     }
  74.     /**
  75.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  76.      */
  77.     public static function getSubscribedEvents(): array
  78.     {
  79.         $events = [];
  80.         if (class_exists(KernelEvents::class)) {
  81.             $events[KernelEvents::TERMINATE] = 'sendPool';
  82.         }
  83.         if (class_exists(ConsoleEvents::class)) {
  84.             $events[ConsoleEvents::TERMINATE] = 'sendPool';
  85.         }
  86.         return $events;
  87.     }
  88. }