vendor/shopware/core/Content/ImportExport/Event/Subscriber/FileDeletedSubscriber.php line 36

Open in your IDE?
  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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Messenger\MessageBusInterface;
  10. class FileDeletedSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var MessageBusInterface
  14.      */
  15.     private $messageBus;
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(MessageBusInterface $messageBus)
  20.     {
  21.         $this->messageBus $messageBus;
  22.     }
  23.     /**
  24.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  25.      */
  26.     public static function getSubscribedEvents()
  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. }