src/Core/Content/ImportExport/DataAbstractionLayer/SystemDefaultValidator.php line 36

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ImportExport\DataAbstractionLayer;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\FetchMode;
  5. use Shopware\Core\Content\ImportExport\Exception\DeleteDefaultProfileException;
  6. use Shopware\Core\Content\ImportExport\ImportExportProfileDefinition;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * @internal
  13.  */
  14. #[Package('system-settings')]
  15. class SystemDefaultValidator implements EventSubscriberInterface
  16. {
  17.     public function __construct(private readonly Connection $connection)
  18.     {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             PreWriteValidationEvent::class => 'preValidate',
  24.         ];
  25.     }
  26.     /**
  27.      * @internal
  28.      *
  29.      * @throws DeleteDefaultProfileException
  30.      */
  31.     public function preValidate(PreWriteValidationEvent $event): void
  32.     {
  33.         $ids = [];
  34.         $writeCommands $event->getCommands();
  35.         foreach ($writeCommands as $command) {
  36.             if ($command->getDefinition()->getClass() === ImportExportProfileDefinition::class
  37.                 && $command instanceof DeleteCommand
  38.             ) {
  39.                 $ids[] = $command->getPrimaryKey()['id'];
  40.             }
  41.         }
  42.         $filteredIds $this->filterSystemDefaults($ids);
  43.         if (!empty($filteredIds)) {
  44.             $event->getExceptions()->add(new DeleteDefaultProfileException($filteredIds));
  45.         }
  46.     }
  47.     private function filterSystemDefaults(array $ids): array
  48.     {
  49.         if (empty($ids)) {
  50.             return [];
  51.         }
  52.         $result $this->connection->executeQuery(
  53.             'SELECT id FROM import_export_profile WHERE id IN (:idList) AND system_default = 1',
  54.             ['idList' => $ids],
  55.             ['idList' => Connection::PARAM_STR_ARRAY]
  56.         );
  57.         return $result->fetchAll(FetchMode::COLUMN);
  58.     }
  59. }