vendor/shopware/core/System/Currency/CurrencyValidator.php line 30

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Currency;
  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\Log\Package;
  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.  * @internal
  14.  */
  15. #[Package('core')]
  16. class CurrencyValidator implements EventSubscriberInterface
  17. {
  18.     final public const VIOLATION_DELETE_DEFAULT_CURRENCY 'delete_default_currency_violation';
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             PreWriteValidationEvent::class => 'preValidate',
  23.         ];
  24.     }
  25.     public function preValidate(PreWriteValidationEvent $event): void
  26.     {
  27.         $commands $event->getCommands();
  28.         $violations = new ConstraintViolationList();
  29.         foreach ($commands as $command) {
  30.             if (!($command instanceof DeleteCommand) || $command->getDefinition()->getClass() !== CurrencyDefinition::class) {
  31.                 continue;
  32.             }
  33.             $pk $command->getPrimaryKey();
  34.             $id mb_strtolower((string) Uuid::fromBytesToHex($pk['id']));
  35.             if ($id !== Defaults::CURRENCY) {
  36.                 continue;
  37.             }
  38.             $msgTpl 'The default currency {{ id }} cannot be deleted.';
  39.             $parameters = ['{{ id }}' => $id];
  40.             $msg sprintf('The default currency %s cannot be deleted.'$id);
  41.             $violation = new ConstraintViolation(
  42.                 $msg,
  43.                 $msgTpl,
  44.                 $parameters,
  45.                 null,
  46.                 '/' $id,
  47.                 $id,
  48.                 null,
  49.                 self::VIOLATION_DELETE_DEFAULT_CURRENCY
  50.             );
  51.             $violations->add($violation);
  52.         }
  53.         if ($violations->count() > 0) {
  54.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  55.         }
  56.     }
  57. }