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

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.     public function __construct(MessageBusInterface $messageBus)
  17.     {
  18.         $this->messageBus $messageBus;
  19.     }
  20.     /**
  21.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  22.      */
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [ImportExportFileEvents::IMPORT_EXPORT_FILE_DELETED_EVENT => 'onFileDeleted'];
  26.     }
  27.     public function onFileDeleted(EntityDeletedEvent $event): void
  28.     {
  29.         $paths = [];
  30.         $activities = [
  31.             ImportExportLogEntity::ACTIVITY_IMPORT,
  32.             ImportExportLogEntity::ACTIVITY_DRYRUN,
  33.             ImportExportLogEntity::ACTIVITY_EXPORT,
  34.         ];
  35.         foreach ($event->getIds() as $fileId) {
  36.             $path ImportExportFileEntity::buildPath($fileId);
  37.             // since the file could be stored in any one directory of the available activities
  38.             foreach ($activities as $activitiy) {
  39.                 $paths[] = $activitiy '/' $path;
  40.                 // if file is not of an export there might be a log of invalid records
  41.                 if ($activitiy !== ImportExportLogEntity::ACTIVITY_EXPORT) {
  42.                     $paths[] = $activitiy '/' $path '_invalid';
  43.                 }
  44.             }
  45.         }
  46.         $message = new DeleteFileMessage();
  47.         $message->setFiles($paths);
  48.         $this->messageBus->dispatch($message);
  49.     }
  50. }