vendor/crehler/image-booster/src/Subscriber/ProductEventsSubscriber.php line 63

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\ImageBooster\Subscriber;
  3. use Crehler\ImageBooster\Entity\MediaBoosterEntity;
  4. use Crehler\ImageBooster\Enum\MediaBoosterTypeEnum;
  5. use Crehler\ImageBooster\Message\GenerateWatermarkMessage;
  6. use Crehler\ImageBooster\Struct\MediaBoostersStruct;
  7. use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
  8. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  9. use Shopware\Core\Content\Product\ProductEvents;
  10. use Shopware\Core\Content\ProductExport\Command\ProductExportGenerateCommand;
  11. use Shopware\Core\Content\ProductExport\ScheduledTask\ProductExportPartialGeneration;
  12. use Shopware\Core\Content\Sitemap\Commands\SitemapGenerateCommand;
  13. use Shopware\Core\Content\Sitemap\ScheduledTask\SitemapMessage;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  21. use Symfony\Component\Console\ConsoleEvents;
  22. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
  25. use Symfony\Component\Messenger\MessageBusInterface;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. class ProductEventsSubscriber implements EventSubscriberInterface
  28. {
  29.     private EntityRepositoryInterface $mediaBoosterRepository;
  30.     private MessageBusInterface $messageBus;
  31.     private EntityRepositoryInterface $productMediaRepository;
  32.     private EventDispatcherInterface $eventDispatcher;
  33.     private bool $disabled false;
  34.     public function __construct(
  35.         EntityRepositoryInterface $mediaBoosterRepository,
  36.         MessageBusInterface $messageBus,
  37.         EntityRepositoryInterface $productMediaRepository,
  38.         EventDispatcherInterface $eventDispatcher
  39.     )
  40.     {
  41.         $this->mediaBoosterRepository $mediaBoosterRepository;
  42.         $this->messageBus $messageBus;
  43.         $this->productMediaRepository $productMediaRepository;
  44.         $this->eventDispatcher $eventDispatcher;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             ProductEvents::PRODUCT_MEDIA_LOADED_EVENT => 'onLoadResult',
  50.             'crehler.custom_media_wrapped.loaded' => 'onLoadResult',
  51.             ProductEvents::PRODUCT_MEDIA_WRITTEN_EVENT => 'onProductMediaWritten',
  52.             ConsoleEvents::COMMAND => 'onCommand',
  53.             WorkerMessageReceivedEvent::class => 'onMessage'
  54.         ];
  55.     }
  56.     public function onCommand(ConsoleCommandEvent $event)
  57.     {
  58.         if($this->disabled) return;
  59.         $this->disabled = (get_class($event->getCommand()) === SitemapGenerateCommand::class || get_class($event->getCommand()) === ProductExportGenerateCommand::class);
  60.     }
  61.     public function onMessage(WorkerMessageReceivedEvent $event)
  62.     {
  63.         if($this->disabled) return;
  64.         $this->disabled = (get_class($event->getEnvelope()->getMessage()) === SitemapMessage::class || get_class($event->getEnvelope()->getMessage()) === ProductExportPartialGeneration::class);
  65.     }
  66.     public function onLoadResult(EntityLoadedEvent $event): void
  67.     {
  68.         if ($event->getContext()->getScope() === "system" || $this->disabled) return;
  69.         $ids = [];
  70.         /** @var ProductMediaEntity $productMedia */
  71.         foreach ($event->getEntities() as $productMedia) {
  72.             if ($productMedia->getMedia()->getExtension('mediaBoosters')) {
  73.                 continue;
  74.             }
  75.             $ids[] = $productMedia->getMedia()->getId();
  76.         }
  77.         $criteria = new Criteria();
  78.         $criteria->addFilter(new EqualsAnyFilter('mediaId'$ids));
  79.         $criteria->addSorting(new FieldSorting('createdAt''DESC'));
  80.         $result $this->mediaBoosterRepository->search($criteria$event->getContext())->getEntities();
  81.         $mediaBoosters = [];
  82.         /** @var MediaBoosterEntity $mediaBooster */
  83.         foreach ($result as $mediaBooster) {
  84.             $mediaBoosters[$mediaBooster->getMediaId()][] = $mediaBooster;
  85.         }
  86.         foreach ($event->getEntities() as $productMedia) {
  87.             if (isset($mediaBoosters[$productMedia->getMedia()->getId()])) {
  88.                 $mediaBoosterStruct = new MediaBoostersStruct();
  89.                 foreach ($mediaBoosters[$productMedia->getMedia()->getId()] as $mediaBooster) {
  90.                     switch($mediaBooster->getType()) {
  91.                         case MediaBoosterTypeEnum::WATERMARK_TYPE_AVIF:
  92.                             $mediaBoosterStruct->setAvifMedia($mediaBooster);
  93.                             break;
  94.                         case MediaBoosterTypeEnum::WATERMARK_TYPE_AVIF_THUMB:
  95.                             $mediaBoosterStruct->addAvifThumbnail($mediaBooster);
  96.                             break;
  97.                         case MediaBoosterTypeEnum::WATERMARK_TYPE_WEBP:
  98.                             $mediaBoosterStruct->setWebpMedia($mediaBooster);
  99.                             break;
  100.                         case MediaBoosterTypeEnum::WATERMARK_TYPE_WEBP_THUMB:
  101.                             $mediaBoosterStruct->addWebpThumbail($mediaBooster);
  102.                             break;
  103.                         case MediaBoosterTypeEnum::WATERMARK_TYPE_BASE:
  104.                             $mediaBoosterStruct->setWatermark($mediaBooster);
  105.                             break;
  106.                         case MediaBoosterTypeEnum::WATERMARK_TYPE_BASE_THUMB:
  107.                             $mediaBoosterStruct->addWatermarkThumbail($mediaBooster);
  108.                             break;
  109.                     }
  110.                 }
  111.                 $productMedia->getMedia()->addExtension('mediaBoosters'$mediaBoosterStruct);
  112.             }
  113.         }
  114.         $this->eventDispatcher->dispatch($event'crehler.custom_media.attach_urls');
  115.     }
  116.     public function onProductMediaWritten(EntityWrittenEvent $event): void
  117.     {
  118.         if ($event->getContext()->getScope() === "system") return;
  119.         $ids = [];
  120.         $productMedias = [];
  121.         /** @var EntityWriteResult $writeResult */
  122.         foreach ($event->getWriteResults() as $writeResult)
  123.         {
  124.             if (isset($writeResult->getPayload()['customFields']) && isset($writeResult->getPayload()['customFields']['mediaBoosterProcess'])) {
  125.                 continue;
  126.             }
  127.             $productMedias[] = ['id' => $writeResult->getPrimaryKey(), 'customFields' => ['mediaBoosterProcess' => 1]];
  128.             if (isset($writeResult->getPayload()['mediaId'])) {
  129.                 $ids[] = $writeResult->getPayload()['mediaId'];
  130.             }
  131.         }
  132.         if (empty($ids)) {
  133.             return;
  134.         }
  135.         $this->productMediaRepository->update($productMedias$event->getContext());
  136.         $message = new GenerateWatermarkMessage();
  137.         $message->setMediaIds($ids);
  138.         $message->setSource("product_media");
  139.         $this->messageBus->dispatch($message, [new TransportConfiguration(['topic' => 'crehlerimagebooster'])]);
  140.     }
  141. }