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

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\Log\Package;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Validator\ConstraintViolation;
  12. use Symfony\Component\Validator\ConstraintViolationList;
  13. /**
  14.  * @deprecated tag:v6.5.0 - reason:remove-subscriber - This subscriber will be superfluous once salutations
  15.  * are fully optional and should be removed together with the flag FEATURE_NEXT_7739.
  16.  */
  17. #[Package('core')]
  18. class DefaultSalutationValidator implements EventSubscriberInterface
  19. {
  20.     public const VIOLATION_CODE 'SYSTEM__DEFAULT_SALUTATION_LOCKED';
  21.     private const MESSAGE 'The default salutation entity may not be deleted.';
  22.     /**
  23.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  24.      */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         if (Feature::isActive('FEATURE_NEXT_7739')) {
  28.             return [];
  29.         }
  30.         return [
  31.             PreWriteValidationEvent::class => 'validate',
  32.         ];
  33.     }
  34.     /**
  35.      * @internal
  36.      */
  37.     public function validate(PreWriteValidationEvent $event): void
  38.     {
  39.         $violations = new ConstraintViolationList();
  40.         foreach ($event->getCommands() as $command) {
  41.             if (!($command instanceof DeleteCommand)) {
  42.                 continue;
  43.             }
  44.             if ($command->getDefinition()->getClass() !== SalutationDefinition::class) {
  45.                 continue;
  46.             }
  47.             if (Uuid::fromBytesToHex($command->getPrimaryKey()['id']) !== Defaults::SALUTATION) {
  48.                 continue;
  49.             }
  50.             $violations->add(new ConstraintViolation(
  51.                 self::MESSAGE,
  52.                 null,
  53.                 [],
  54.                 null,
  55.                 '/',
  56.                 null,
  57.                 null,
  58.                 self::VIOLATION_CODE
  59.             ));
  60.         }
  61.         if ($violations->count() < 1) {
  62.             return;
  63.         }
  64.         $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  65.     }
  66. }