src/Core/Framework/DataAbstractionLayer/Write/Validation/LockValidator.php line 40

  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\Log\Package;
  8. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Validator\ConstraintViolation;
  11. use Symfony\Component\Validator\ConstraintViolationList;
  12. /**
  13.  * @internal
  14.  */
  15. #[Package('core')]
  16. class LockValidator implements EventSubscriberInterface
  17. {
  18.     final public const VIOLATION_LOCKED 'FRAMEWORK__ENTITY_IS_LOCKED';
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(private readonly Connection $connection)
  23.     {
  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. }