platform/src/Core/Content/ImportExport/Event/Subscriber/FileDeletedSubscriber.php line 30

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