CurrencyInformationAcquirer.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model;
  7. /**
  8. * Currency information acquirer class
  9. */
  10. class CurrencyInformationAcquirer implements \Magento\Directory\Api\CurrencyInformationAcquirerInterface
  11. {
  12. /**
  13. * @var \Magento\Directory\Model\Data\CurrencyInformationFactory
  14. */
  15. protected $currencyInformationFactory;
  16. /**
  17. * @var \Magento\Directory\Model\Data\ExchangeRateFactory
  18. */
  19. protected $exchangeRateFactory;
  20. /**
  21. * @var \Magento\Store\Model\StoreManagerInterface
  22. */
  23. protected $storeManager;
  24. /**
  25. * @param \Magento\Directory\Model\Data\CurrencyInformationFactory $currencyInformationFactory
  26. * @param \Magento\Directory\Model\Data\ExchangeRateFactory $exchangeRateFactory
  27. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  28. */
  29. public function __construct(
  30. \Magento\Directory\Model\Data\CurrencyInformationFactory $currencyInformationFactory,
  31. \Magento\Directory\Model\Data\ExchangeRateFactory $exchangeRateFactory,
  32. \Magento\Store\Model\StoreManagerInterface $storeManager
  33. ) {
  34. $this->currencyInformationFactory = $currencyInformationFactory;
  35. $this->exchangeRateFactory = $exchangeRateFactory;
  36. $this->storeManager = $storeManager;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getCurrencyInfo()
  42. {
  43. $currencyInfo = $this->currencyInformationFactory->create();
  44. /** @var \Magento\Store\Model\Store $store */
  45. $store = $this->storeManager->getStore();
  46. $currencyInfo->setBaseCurrencyCode($store->getBaseCurrency()->getCode());
  47. $currencyInfo->setBaseCurrencySymbol($store->getBaseCurrency()->getCurrencySymbol());
  48. $currencyInfo->setDefaultDisplayCurrencyCode($store->getDefaultCurrency()->getCode());
  49. $currencyInfo->setDefaultDisplayCurrencySymbol($store->getDefaultCurrency()->getCurrencySymbol());
  50. $currencyInfo->setAvailableCurrencyCodes($store->getAvailableCurrencyCodes(true));
  51. $exchangeRates = [];
  52. foreach ($store->getAvailableCurrencyCodes(true) as $currencyCode) {
  53. $exchangeRate = $this->exchangeRateFactory->create();
  54. $exchangeRate->setRate($store->getBaseCurrency()->getRate($currencyCode));
  55. $exchangeRate->setCurrencyTo($currencyCode);
  56. $exchangeRates[] = $exchangeRate;
  57. }
  58. $currencyInfo->setExchangeRates($exchangeRates);
  59. return $currencyInfo;
  60. }
  61. }