Allow.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Config Directory currency backend model
  8. * Allows dispatching before and after events for each controller action
  9. */
  10. namespace Magento\Config\Model\Config\Backend\Currency;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Allow extends AbstractCurrency
  16. {
  17. /**
  18. * @var \Magento\Framework\Locale\CurrencyInterface
  19. */
  20. protected $_localeCurrency;
  21. /**
  22. * @param \Magento\Framework\Model\Context $context
  23. * @param \Magento\Framework\Registry $registry
  24. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  25. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  26. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  27. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  28. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  29. * @param array $data
  30. */
  31. public function __construct(
  32. \Magento\Framework\Model\Context $context,
  33. \Magento\Framework\Registry $registry,
  34. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  35. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  36. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  37. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  38. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  39. array $data = []
  40. ) {
  41. $this->_localeCurrency = $localeCurrency;
  42. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  43. }
  44. /**
  45. * Check is isset default display currency in allowed currencies
  46. * Check allowed currencies is available in installed currencies
  47. *
  48. * @return $this
  49. * @throws \Magento\Framework\Exception\LocalizedException
  50. */
  51. public function afterSave()
  52. {
  53. $exceptions = [];
  54. $allowedCurrencies = $this->_getAllowedCurrencies();
  55. foreach ($allowedCurrencies as $currencyCode) {
  56. if (!in_array($currencyCode, $this->_getInstalledCurrencies())) {
  57. $exceptions[] = __(
  58. 'Selected allowed currency "%1" is not available in installed currencies.',
  59. $this->_localeCurrency->getCurrency($currencyCode)->getName()
  60. );
  61. }
  62. }
  63. if (!in_array($this->_getCurrencyDefault(), $allowedCurrencies)) {
  64. $exceptions[] = __(
  65. 'Default display currency "%1" is not available in allowed currencies.',
  66. $this->_localeCurrency->getCurrency($this->_getCurrencyDefault())->getName()
  67. );
  68. }
  69. if ($exceptions) {
  70. throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $exceptions)));
  71. }
  72. return parent::afterSave();
  73. }
  74. /**
  75. * @inheritdoc
  76. * @since 101.0.0
  77. */
  78. protected function _getAllowedCurrencies()
  79. {
  80. $value = $this->getValue();
  81. $isFormData = $this->getData('groups/options/fields') !== null;
  82. if ($isFormData && $this->getData('groups/options/fields/allow/inherit')) {
  83. $value = (string)$this->_config->getValue(
  84. \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW,
  85. $this->getScope(),
  86. $this->getScopeId()
  87. );
  88. }
  89. return explode(',', $value);
  90. }
  91. }