Base.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Backend 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 Base extends AbstractCurrency
  16. {
  17. /**
  18. * @var \Magento\Directory\Model\CurrencyFactory
  19. */
  20. private $currencyFactory;
  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\Directory\Model\CurrencyFactory $currencyFactory
  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\Directory\Model\CurrencyFactory $currencyFactory,
  37. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  38. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  39. array $data = []
  40. ) {
  41. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  42. $this->currencyFactory = $currencyFactory;
  43. }
  44. /**
  45. * Check base currency is available in installed currencies
  46. *
  47. * @return $this
  48. * @throws \Magento\Framework\Exception\LocalizedException
  49. */
  50. public function afterSave()
  51. {
  52. $value = $this->getValue();
  53. if (!in_array($value, $this->_getInstalledCurrencies())) {
  54. throw new \Magento\Framework\Exception\LocalizedException(
  55. __('Sorry, we haven\'t installed the base currency you selected.')
  56. );
  57. }
  58. $this->currencyFactory->create()->saveRates([$value =>[$value => 1]]);
  59. return parent::afterSave();
  60. }
  61. }