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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Currency;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  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. class CurrencyValidator implements EventSubscriberInterface
  13. {
  14.     public const VIOLATION_DELETE_DEFAULT_CURRENCY 'delete_default_currency_violation';
  15.     public const DEFAULT_CURRENCIES = [Defaults::CURRENCY];
  16.     /**
  17.      * @var Connection
  18.      */
  19.     private $connection;
  20.     public function __construct(Connection $connection)
  21.     {
  22.         $this->connection $connection;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             PreWriteValidationEvent::class => 'preValidate',
  28.         ];
  29.     }
  30.     public function preValidate(PreWriteValidationEvent $event): void
  31.     {
  32.         $commands $event->getCommands();
  33.         $violations = new ConstraintViolationList();
  34.         foreach ($commands as $command) {
  35.             if (!($command instanceof DeleteCommand) || $command->getDefinition()->getClass() !== CurrencyDefinition::class) {
  36.                 continue;
  37.             }
  38.             $pk $command->getPrimaryKey();
  39.             $id = \mb_strtolower(Uuid::fromBytesToHex($pk['id']));
  40.             if ($id !== Defaults::CURRENCY) {
  41.                 continue;
  42.             }
  43.             $msgTpl 'The default currency {{ id }} cannot be deleted.';
  44.             $parameters = ['{{ id }}' => $id];
  45.             $msg sprintf('The default currency %s cannot be deleted.'$id);
  46.             $violation = new ConstraintViolation(
  47.                 $msg,
  48.                 $msgTpl,
  49.                 $parameters,
  50.                 null,
  51.                 '/' $id,
  52.                 $id,
  53.                 null,
  54.                 self::VIOLATION_DELETE_DEFAULT_CURRENCY
  55.             );
  56.             $violations->add($violation);
  57.         }
  58.         if ($violations->count() > 0) {
  59.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  60.         }
  61.     }
  62. }