platform/src/Core/System/Salutation/DefaultSalutationValidator.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Salutation;
  3. use Shopware\Core\Defaults;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  6. use Shopware\Core\Framework\Feature;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  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.  * @deprecated tag:v6.5.0 This subscriber will be superfluous once salutations
  14.  * are fully optional and should be removed together with the flag FEATURE_NEXT_7739.
  15.  */
  16. class DefaultSalutationValidator implements EventSubscriberInterface
  17. {
  18.     public const VIOLATION_CODE 'SYSTEM__DEFAULT_SALUTATION_LOCKED';
  19.     private const MESSAGE 'The default salutation entity may not be deleted.';
  20.     public static function getSubscribedEvents()
  21.     {
  22.         if (Feature::isActive('FEATURE_NEXT_7739')) {
  23.             return [];
  24.         }
  25.         return [
  26.             PreWriteValidationEvent::class => 'validate',
  27.         ];
  28.     }
  29.     /**
  30.      * @internal
  31.      */
  32.     public function validate(PreWriteValidationEvent $event): void
  33.     {
  34.         $violations = new ConstraintViolationList();
  35.         foreach ($event->getCommands() as $command) {
  36.             if (!($command instanceof DeleteCommand)) {
  37.                 continue;
  38.             }
  39.             if ($command->getDefinition()->getClass() !== SalutationDefinition::class) {
  40.                 continue;
  41.             }
  42.             if (Uuid::fromBytesToHex($command->getPrimaryKey()['id']) !== Defaults::SALUTATION) {
  43.                 continue;
  44.             }
  45.             $violations->add(new ConstraintViolation(
  46.                 self::MESSAGE,
  47.                 null,
  48.                 [],
  49.                 null,
  50.                 '/',
  51.                 null,
  52.                 null,
  53.                 self::VIOLATION_CODE
  54.             ));
  55.         }
  56.         if ($violations->count() < 1) {
  57.             return;
  58.         }
  59.         $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  60.     }
  61. }