<?php declare(strict_types=1);
namespace P2Lab\ProductVideo\Subscriber;
use Doctrine\DBAL\Connection;
use P2Lab\ProductVideo\Helper\ProductMediaHelper;
use Shopware\Core\Content\Media\Event\UnusedMediaSearchEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UnusedMediaSubscriber implements EventSubscriberInterface
{
private Connection $connection;
/**
* @internal
*/
public function __construct(
Connection $connection
) {
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
return [
UnusedMediaSearchEvent::class => 'onUnusedMediaSearch'
];
}
public function onUnusedMediaSearch(UnusedMediaSearchEvent $event): void
{
/** @var array $idsToBeDeleted */
$idsToBeDeleted = $event->getUnusedIds();
/** @var array $doNotDeleteTheseIds */
$doNotDeleteTheseIds = ProductMediaHelper::getUsedMediaIds($this->connection, $idsToBeDeleted);
$event->markAsUsed($doNotDeleteTheseIds);
}
}