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. class LockValidator implements EventSubscriberInterface
  12. {
  13.     public const VIOLATION_LOCKED 'FRAMEWORK__ENTITY_IS_LOCKED';
  14.     /**
  15.      * @var Connection
  16.      */
  17.     private $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.     private function containsLockedEntities(array $writeCommands): array
  61.     {
  62.         $ids = [];
  63.         $locked = [];
  64.         foreach ($writeCommands as $command) {
  65.             if ($command instanceof InsertCommand) {
  66.                 continue;
  67.             }
  68.             if (!$command->getDefinition()->isLockAware()) {
  69.                 continue;
  70.             }
  71.             $ids[$command->getDefinition()->getEntityName()][] = $command->getPrimaryKey()['id'];
  72.         }
  73.         foreach ($ids as $entityName => $primaryKeys) {
  74.             $locked[$entityName] = $this->connection->createQueryBuilder()
  75.                 ->select('1')
  76.                 ->from(EntityDefinitionQueryHelper::escape($entityName))
  77.                 ->where('`id` IN (:ids) AND `locked` = 1')
  78.                 ->setParameter('ids'$primaryKeysConnection::PARAM_STR_ARRAY)
  79.                 ->execute()
  80.                 ->rowCount() > 0;
  81.         }
  82.         return array_filter($locked);
  83.     }
  84. }