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

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