custom/plugins/PickwareErpStarter/vendor/pickware/dal-bundle/src/EntityDeleteRestrictor.php line 46

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\DalBundle;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  13. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Validator\ConstraintViolation;
  16. use Symfony\Component\Validator\ConstraintViolationList;
  17. class EntityDeleteRestrictor implements EventSubscriberInterface
  18. {
  19.     public const ERROR_CODE_NAMESPACE 'PICKWARE_DAL_BUNDLE__ENTITY_DELETE_RESTRICTOR';
  20.     /**
  21.      * @var string[]
  22.      */
  23.     private array $entityDefinitionClassNames;
  24.     /**
  25.      * @var string[]
  26.      */
  27.     private array $allowedContextScopes;
  28.     public function __construct(array $entityDefinitionClassNames, array $allowedContextScopes)
  29.     {
  30.         $this->entityDefinitionClassNames $entityDefinitionClassNames;
  31.         $this->allowedContextScopes $allowedContextScopes;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [PreWriteValidationEvent::class => 'preValidate'];
  36.     }
  37.     public function preValidate(PreWriteValidationEvent $event): void
  38.     {
  39.         if (in_array($event->getContext()->getScope(), $this->allowedContextScopestrue)) {
  40.             return;
  41.         }
  42.         $commands $event->getCommands();
  43.         $violations = new ConstraintViolationList();
  44.         foreach ($commands as $command) {
  45.             $className $command->getDefinition()->getClass();
  46.             if (!($command instanceof DeleteCommand)
  47.                 || !in_array($className$this->entityDefinitionClassNamestrue)) {
  48.                 continue;
  49.             }
  50.             $entityName $command->getDefinition()->getEntityName();
  51.             $message sprintf('A %s cannot be deleted.'$entityName);
  52.             $violations->add(new ConstraintViolation(
  53.                 $message,
  54.                 $message,
  55.                 [],
  56.                 null,
  57.                 '/',
  58.                 null,
  59.                 null,
  60.                 sprintf(
  61.                     '%s__%s',
  62.                     self::ERROR_CODE_NAMESPACE,
  63.                     mb_strtoupper($entityName),
  64.                 ),
  65.             ));
  66.         }
  67.         if ($violations->count() > 0) {
  68.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  69.         }
  70.     }
  71. }