src/App/RabbitMq/Handler/DownloadableContentBidHandler.php line 107

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