src/Core/Content/ImportExport/Event/Subscriber/FileDeletedSubscriber.php line 35

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ImportExport\Event\Subscriber;
  3. use Shopware\Core\Content\ImportExport\Aggregate\ImportExportFile\ImportExportFileEntity;
  4. use Shopware\Core\Content\ImportExport\Aggregate\ImportExportFile\ImportExportFileEvents;
  5. use Shopware\Core\Content\ImportExport\Aggregate\ImportExportLog\ImportExportLogEntity;
  6. use Shopware\Core\Content\ImportExport\Message\DeleteFileMessage;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Messenger\MessageBusInterface;
  11. /**
  12.  * @internal
  13.  */
  14. #[Package('system-settings')]
  15. class FileDeletedSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(private readonly MessageBusInterface $messageBus)
  21.     {
  22.     }
  23.     /**
  24.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [ImportExportFileEvents::IMPORT_EXPORT_FILE_DELETED_EVENT => 'onFileDeleted'];
  29.     }
  30.     public function onFileDeleted(EntityDeletedEvent $event): void
  31.     {
  32.         $paths = [];
  33.         $activities = [
  34.             ImportExportLogEntity::ACTIVITY_IMPORT,
  35.             ImportExportLogEntity::ACTIVITY_DRYRUN,
  36.             ImportExportLogEntity::ACTIVITY_EXPORT,
  37.         ];
  38.         foreach ($event->getIds() as $fileId) {
  39.             $path ImportExportFileEntity::buildPath($fileId);
  40.             // since the file could be stored in any one directory of the available activities
  41.             foreach ($activities as $activitiy) {
  42.                 $paths[] = $activitiy '/' $path;
  43.                 // if file is not of an export there might be a log of invalid records
  44.                 if ($activitiy !== ImportExportLogEntity::ACTIVITY_EXPORT) {
  45.                     $paths[] = $activitiy '/' $path '_invalid';
  46.                 }
  47.             }
  48.         }
  49.         $message = new DeleteFileMessage();
  50.         $message->setFiles($paths);
  51.         $this->messageBus->dispatch($message);
  52.     }
  53. }