vendor/shopware/core/Framework/DataAbstractionLayer/Write/Validation/LockValidator.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Write\Validation;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityDefinitionQueryHelper;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  7. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Validator\ConstraintViolation;
  10. use Symfony\Component\Validator\ConstraintViolationList;
  11. /**
  12.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  13.  */
  14. class LockValidator implements EventSubscriberInterface
  15. {
  16.     public const VIOLATION_LOCKED 'FRAMEWORK__ENTITY_IS_LOCKED';
  17.     private Connection $connection;
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(Connection $connection)
  22.     {
  23.         $this->connection $connection;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             PreWriteValidationEvent::class => 'preValidate',
  29.         ];
  30.     }
  31.     /**
  32.      * @throws WriteConstraintViolationException
  33.      */
  34.     public function preValidate(PreWriteValidationEvent $event): void
  35.     {
  36.         $violations = new ConstraintViolationList();
  37.         $writeCommands $event->getCommands();
  38.         $lockedEntities $this->containsLockedEntities($writeCommands);
  39.         if (empty($lockedEntities)) {
  40.             return;
  41.         }
  42.         $message 'The %s entity is locked and can neither be modified nor deleted.';
  43.         foreach ($lockedEntities as $entity => $_isLocked) {
  44.             $violations->add(new ConstraintViolation(
  45.                 sprintf($message$entity),
  46.                 sprintf($message'{{ entity }}'),
  47.                 ['{{ entity }}' => $entity],
  48.                 null,
  49.                 '/',
  50.                 null,
  51.                 null,
  52.                 self::VIOLATION_LOCKED
  53.             ));
  54.         }
  55.         $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  56.     }
  57.     /**
  58.      * @param WriteCommand[] $writeCommands
  59.      *
  60.      * @return array<string, bool>
  61.      */
  62.     private function containsLockedEntities(array $writeCommands): array
  63.     {
  64.         $ids = [];
  65.         $locked = [];
  66.         foreach ($writeCommands as $command) {
  67.             if ($command instanceof InsertCommand) {
  68.                 continue;
  69.             }
  70.             if (!$command->getDefinition()->isLockAware()) {
  71.                 continue;
  72.             }
  73.             $ids[$command->getDefinition()->getEntityName()][] = $command->getPrimaryKey()['id'];
  74.         }
  75.         /** @var string $entityName */
  76.         foreach ($ids as $entityName => $primaryKeys) {
  77.             $locked[$entityName] = $this->connection->createQueryBuilder()
  78.                 ->select('1')
  79.                 ->from(EntityDefinitionQueryHelper::escape($entityName))
  80.                 ->where('`id` IN (:ids) AND `locked` = 1')
  81.                 ->setParameter('ids'$primaryKeysConnection::PARAM_STR_ARRAY)
  82.                 ->execute()
  83.                 ->rowCount() > 0;
  84.         }
  85.         return array_filter($locked);
  86.     }
  87. }