platform/src/Core/System/Currency/CurrencyValidator.php line 27

Open in your IDE?
  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\Uuid\Uuid;
  7. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Validator\ConstraintViolation;
  10. use Symfony\Component\Validator\ConstraintViolationList;
  11. class CurrencyValidator implements EventSubscriberInterface
  12. {
  13.     public const VIOLATION_DELETE_DEFAULT_CURRENCY 'delete_default_currency_violation';
  14.     public const DEFAULT_CURRENCIES = [Defaults::CURRENCY];
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             PreWriteValidationEvent::class => 'preValidate',
  19.         ];
  20.     }
  21.     public function preValidate(PreWriteValidationEvent $event): void
  22.     {
  23.         $commands $event->getCommands();
  24.         $violations = new ConstraintViolationList();
  25.         foreach ($commands as $command) {
  26.             if (!($command instanceof DeleteCommand) || $command->getDefinition()->getClass() !== CurrencyDefinition::class) {
  27.                 continue;
  28.             }
  29.             $pk $command->getPrimaryKey();
  30.             $id mb_strtolower(Uuid::fromBytesToHex($pk['id']));
  31.             if ($id !== Defaults::CURRENCY) {
  32.                 continue;
  33.             }
  34.             $msgTpl 'The default currency {{ id }} cannot be deleted.';
  35.             $parameters = ['{{ id }}' => $id];
  36.             $msg sprintf('The default currency %s cannot be deleted.'$id);
  37.             $violation = new ConstraintViolation(
  38.                 $msg,
  39.                 $msgTpl,
  40.                 $parameters,
  41.                 null,
  42.                 '/' $id,
  43.                 $id,
  44.                 null,
  45.                 self::VIOLATION_DELETE_DEFAULT_CURRENCY
  46.             );
  47.             $violations->add($violation);
  48.         }
  49.         if ($violations->count() > 0) {
  50.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  51.         }
  52.     }
  53. }