src/App/RabbitMq/Handler/DownloadableContentHandler.php line 113

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\RabbitMq\Handler;
  3. use App\Contract\Content\DownloadableContent;
  4. use App\Contract\Content\DownloadableContentBid;
  5. use App\Entity\Deal;
  6. use App\Entity\DealBid;
  7. use App\Entity\Download;
  8. use App\Entity\Message\MessageBid;
  9. use App\Entity\Message\MessageReception;
  10. use App\Entity\Project;
  11. use App\Entity\ProjectBid;
  12. use App\Entity\ShopVideo;
  13. use App\Entity\ShopVideoBid;
  14. use App\RabbitMq\Processing\PopProcessingEvent;
  15. use App\RabbitMq\Processing\ProcessingEvent;
  16. use App\RabbitMq\Processing\PushProcessingEvent;
  17. use App\Repository\DownloadRepository;
  18. use App\Repository\Filter\DownloadableContentFilter;
  19. use Doctrine\Common\Collections\Collection;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. use InvalidArgumentException;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  24. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  25. class DownloadableContentHandler implements RabbitMqHandlerInterfaceEventSubscriberInterface
  26. {
  27.     public const ADD 'add';
  28.     public const REMOVE 'remove';
  29.     private ManagerRegistry $doctrine;
  30.     private DownloadRepository $downloadRepository;
  31.     public function __construct(ManagerRegistry $doctrineDownloadRepository $downloadRepository)
  32.     {
  33.         $this->doctrine $doctrine;
  34.         $this->downloadRepository $downloadRepository;
  35.     }
  36.     public function supports(string $service): bool
  37.     {
  38.         return $service === self::class;
  39.     }
  40.     /**
  41.      * @param mixed[] $task
  42.      */
  43.     public function handle(?object $entity, array $task): void
  44.     {
  45.         if (!is_array($task['arguments']) || !== count($task['arguments'])) {
  46.             throw new InvalidArgumentException('Task should contain only one argument.');
  47.         }
  48.         if (!$entity instanceof DownloadableContent) {
  49.             throw new UnexpectedTypeException($entityDownloadableContent::class);
  50.         }
  51.         if (!$entity instanceof Deal && !$entity instanceof Project && !$entity instanceof ShopVideo && !$entity instanceof MessageReception) {
  52.             throw new UnexpectedTypeException($entityimplode('|', [Deal::class, Project::class, ShopVideo::class, MessageReception::class]));
  53.         }
  54.         if (!is_string($task['arguments'][0])) {
  55.             return;
  56.         }
  57.         $action $task['arguments'][0];
  58.         if (self::ADD !== $action && self::REMOVE !== $action) {
  59.             throw new UnexpectedValueException($action'"add"|"remove"');
  60.         }
  61.         if ($action === self::REMOVE) {
  62.             if ($entity->isContentAccessible()) {
  63.                 return;
  64.             }
  65.             $downloads $this->downloadRepository->filterResults([
  66.                 DownloadableContentFilter::FILTER => $entity,
  67.             ]);
  68.             foreach ($downloads as $download) {
  69.                 $this->doctrine->getManager()->remove($download);
  70.             }
  71.             return;
  72.         }
  73.         if (!$entity->isContentAccessible()) {
  74.             return;
  75.         }
  76.         /** @var Collection<int, DownloadableContentBid<DealBid|ProjectBid|ShopVideoBid|MessageBid>> $bids */
  77.         $bids $entity->getBids();
  78.         foreach ($bids as $bid) {
  79.             if (!$bid->isStillAccessible()) {
  80.                 continue;
  81.             }
  82.             $download = new Download($bid->getUser(), $entity);
  83.             $this->doctrine->getManager()->persist($download);
  84.         }
  85.     }
  86.     public function onPushProcess(PushProcessingEvent $event): void
  87.     {
  88.         $this->onProcess($event);
  89.     }
  90.     public function onPopProcess(PopProcessingEvent $event): void
  91.     {
  92.         $this->onProcess($event);
  93.     }
  94.     private function onProcess(ProcessingEvent $event): void
  95.     {
  96.         if ($event->getService() !== self::class) {
  97.             return;
  98.         }
  99.         $entity $event->getEntity();
  100.         if (!$entity instanceof DownloadableContent) {
  101.             return;
  102.         }
  103.         $event->stopPropagation();
  104.     }
  105.     /**
  106.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  107.      */
  108.     public static function getSubscribedEvents(): array
  109.     {
  110.         return [
  111.             PushProcessingEvent::class => ['onPushProcess'1000],
  112.             PopProcessingEvent::class => ['onPopProcess'1000],
  113.         ];
  114.     }
  115. }