FetchRates.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\CurrencySymbol\Controller\Adminhtml\System\Currency;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Controller\ResultFactory;
  12. use Magento\CurrencySymbol\Controller\Adminhtml\System\Currency as CurrencyAction;
  13. class FetchRates extends CurrencyAction implements HttpGetActionInterface, HttpPostActionInterface
  14. {
  15. /**
  16. * Fetch rates action
  17. *
  18. * @return \Magento\Backend\Model\View\Result\Redirect
  19. */
  20. public function execute()
  21. {
  22. /** @var \Magento\Backend\Model\Session $backendSession */
  23. $backendSession = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
  24. try {
  25. $service = $this->getRequest()->getParam('rate_services');
  26. $this->_getSession()->setCurrencyRateService($service);
  27. if (!$service) {
  28. throw new LocalizedException(__('The Import Service is incorrect. Verify the service and try again.'));
  29. }
  30. try {
  31. /** @var \Magento\Directory\Model\Currency\Import\ImportInterface $importModel */
  32. $importModel = $this->_objectManager->get(\Magento\Directory\Model\Currency\Import\Factory::class)
  33. ->create($service);
  34. } catch (\Exception $e) {
  35. throw new LocalizedException(
  36. __("The import model can't be initialized. Verify the model and try again.")
  37. );
  38. }
  39. $rates = $importModel->fetchRates();
  40. $errors = $importModel->getMessages();
  41. if (sizeof($errors) > 0) {
  42. foreach ($errors as $error) {
  43. $this->messageManager->addWarning($error);
  44. }
  45. $this->messageManager->addWarning(
  46. __('Click "Save" to apply the rates we found.')
  47. );
  48. } else {
  49. $this->messageManager->addSuccess(__('Click "Save" to apply the rates we found.'));
  50. }
  51. $backendSession->setRates($rates);
  52. } catch (\Exception $e) {
  53. $this->messageManager->addError($e->getMessage());
  54. }
  55. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  56. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  57. return $resultRedirect->setPath('adminhtml/*/');
  58. }
  59. }