Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FetchRates.php
Go to the documentation of this file.
1 <?php
9 
11 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
15 
16 class FetchRates extends CurrencyAction implements HttpGetActionInterface, HttpPostActionInterface
17 {
23  public function execute()
24  {
26  $backendSession = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
27  try {
28  $service = $this->getRequest()->getParam('rate_services');
29  $this->_getSession()->setCurrencyRateService($service);
30  if (!$service) {
31  throw new LocalizedException(__('The Import Service is incorrect. Verify the service and try again.'));
32  }
33  try {
35  $importModel = $this->_objectManager->get(\Magento\Directory\Model\Currency\Import\Factory::class)
36  ->create($service);
37  } catch (\Exception $e) {
38  throw new LocalizedException(
39  __("The import model can't be initialized. Verify the model and try again.")
40  );
41  }
42  $rates = $importModel->fetchRates();
43  $errors = $importModel->getMessages();
44  if (sizeof($errors) > 0) {
45  foreach ($errors as $error) {
46  $this->messageManager->addWarning($error);
47  }
48  $this->messageManager->addWarning(
49  __('Click "Save" to apply the rates we found.')
50  );
51  } else {
52  $this->messageManager->addSuccess(__('Click "Save" to apply the rates we found.'));
53  }
54 
55  $backendSession->setRates($rates);
56  } catch (\Exception $e) {
57  $this->messageManager->addError($e->getMessage());
58  }
59 
61  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
62  return $resultRedirect->setPath('adminhtml/*/');
63  }
64 }
__()
Definition: __.php:13
$rates
Definition: tax.phtml:35
$errors
Definition: overview.phtml:9