platform/src/Core/Content/ImportExport/DataAbstractionLayer/SystemDefaultValidator.php line 40

Open in your IDE?
  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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @internal
  12.  */
  13. class SystemDefaultValidator implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var Connection
  17.      */
  18.     private $connection;
  19.     public function __construct(Connection $connection)
  20.     {
  21.         $this->connection $connection;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             PreWriteValidationEvent::class => 'preValidate',
  27.         ];
  28.     }
  29.     /**
  30.      * @internal
  31.      *
  32.      * @throws DeleteDefaultProfileException
  33.      */
  34.     public function preValidate(PreWriteValidationEvent $event): void
  35.     {
  36.         $ids = [];
  37.         $writeCommands $event->getCommands();
  38.         foreach ($writeCommands as $command) {
  39.             if ($command->getDefinition()->getClass() === ImportExportProfileDefinition::class
  40.                 && $command instanceof DeleteCommand
  41.             ) {
  42.                 $ids[] = $command->getPrimaryKey()['id'];
  43.             }
  44.         }
  45.         $filteredIds $this->filterSystemDefaults($ids);
  46.         if (!empty($filteredIds)) {
  47.             $event->getExceptions()->add(new DeleteDefaultProfileException($filteredIds));
  48.         }
  49.     }
  50.     private function filterSystemDefaults(array $ids): array
  51.     {
  52.         if (empty($ids)) {
  53.             return [];
  54.         }
  55.         $result $this->connection->executeQuery(
  56.             'SELECT id FROM import_export_profile WHERE id IN (:idList) AND system_default = 1',
  57.             [':idList' => $ids],
  58.             [':idList' => Connection::PARAM_STR_ARRAY]
  59.         );
  60.         return $result->fetchAll(FetchMode::COLUMN);
  61.     }
  62. }