vendor/shopware/core/System/Salutation/DefaultSalutationValidator.php line 42

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 - reason:remove-subscriber - 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.     /**
  21.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  22.      */
  23.     public static function getSubscribedEvents()
  24.     {
  25.         if (Feature::isActive('FEATURE_NEXT_7739')) {
  26.             return [];
  27.         }
  28.         return [
  29.             PreWriteValidationEvent::class => 'validate',
  30.         ];
  31.     }
  32.     /**
  33.      * @internal
  34.      */
  35.     public function validate(PreWriteValidationEvent $event): void
  36.     {
  37.         $violations = new ConstraintViolationList();
  38.         foreach ($event->getCommands() as $command) {
  39.             if (!($command instanceof DeleteCommand)) {
  40.                 continue;
  41.             }
  42.             if ($command->getDefinition()->getClass() !== SalutationDefinition::class) {
  43.                 continue;
  44.             }
  45.             if (Uuid::fromBytesToHex($command->getPrimaryKey()['id']) !== Defaults::SALUTATION) {
  46.                 continue;
  47.             }
  48.             $violations->add(new ConstraintViolation(
  49.                 self::MESSAGE,
  50.                 null,
  51.                 [],
  52.                 null,
  53.                 '/',
  54.                 null,
  55.                 null,
  56.                 self::VIOLATION_CODE
  57.             ));
  58.         }
  59.         if ($violations->count() < 1) {
  60.             return;
  61.         }
  62.         $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  63.     }
  64. }