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

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. class LockValidator implements EventSubscriberInterface
  12. {
  13.     public const VIOLATION_LOCKED 'FRAMEWORK__ENTITY_IS_LOCKED';
  14.     /**
  15.      * @var Connection
  16.      */
  17.     private $connection;
  18.     public function __construct(Connection $connection)
  19.     {
  20.         $this->connection $connection;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             PreWriteValidationEvent::class => 'preValidate',
  26.         ];
  27.     }
  28.     /**
  29.      * @throws WriteConstraintViolationException
  30.      */
  31.     public function preValidate(PreWriteValidationEvent $event): void
  32.     {
  33.         $violations = new ConstraintViolationList();
  34.         $writeCommands $event->getCommands();
  35.         $lockedEntities $this->containsLockedEntities($writeCommands);
  36.         if (empty($lockedEntities)) {
  37.             return;
  38.         }
  39.         $message 'The %s entity is locked and can neither be modified nor deleted.';
  40.         foreach ($lockedEntities as $entity => $_isLocked) {
  41.             $violations->add(new ConstraintViolation(
  42.                 sprintf($message$entity),
  43.                 sprintf($message'{{ entity }}'),
  44.                 ['{{ entity }}' => $entity],
  45.                 null,
  46.                 '/',
  47.                 null,
  48.                 null,
  49.                 self::VIOLATION_LOCKED
  50.             ));
  51.         }
  52.         $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  53.     }
  54.     /**
  55.      * @param WriteCommand[] $writeCommands
  56.      */
  57.     private function containsLockedEntities(array $writeCommands): array
  58.     {
  59.         $ids = [];
  60.         $locked = [];
  61.         foreach ($writeCommands as $command) {
  62.             if ($command instanceof InsertCommand) {
  63.                 continue;
  64.             }
  65.             if (!$command->getDefinition()->isLockAware()) {
  66.                 continue;
  67.             }
  68.             $ids[$command->getDefinition()->getEntityName()][] = $command->getPrimaryKey()['id'];
  69.         }
  70.         foreach ($ids as $entityName => $primaryKeys) {
  71.             $locked[$entityName] = $this->connection->createQueryBuilder()
  72.                 ->select('1')
  73.                 ->from(EntityDefinitionQueryHelper::escape($entityName))
  74.                 ->where('`id` IN (:ids) AND `locked` = 1')
  75.                 ->setParameter('ids'$primaryKeysConnection::PARAM_STR_ARRAY)
  76.                 ->execute()
  77.                 ->rowCount() > 0;
  78.         }
  79.         return array_filter($locked);
  80.     }
  81. }