custom/plugins/P2LabProductVideo/src/Subscriber/UnusedMediaSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace P2Lab\ProductVideo\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use P2Lab\ProductVideo\Helper\ProductMediaHelper;
  5. use Shopware\Core\Content\Media\Event\UnusedMediaSearchEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class UnusedMediaSubscriber implements EventSubscriberInterface
  8. {
  9.     private Connection $connection;
  10.     /**
  11.      * @internal
  12.      */
  13.     public function __construct(
  14.         Connection $connection
  15.     ) {
  16.         $this->connection $connection;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             UnusedMediaSearchEvent::class => 'onUnusedMediaSearch'
  22.         ];
  23.     }
  24.     public function onUnusedMediaSearch(UnusedMediaSearchEvent $event): void
  25.     {
  26.         /** @var array $idsToBeDeleted */
  27.         $idsToBeDeleted $event->getUnusedIds();
  28.         /** @var array $doNotDeleteTheseIds */
  29.         $doNotDeleteTheseIds ProductMediaHelper::getUsedMediaIds($this->connection$idsToBeDeleted);
  30.         $event->markAsUsed($doNotDeleteTheseIds);
  31.     }
  32. }