Fetch.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Paypal\Controller\Adminhtml\Paypal\Reports;
  8. use Magento\Framework\Controller\ResultFactory;
  9. class Fetch extends \Magento\Paypal\Controller\Adminhtml\Paypal\Reports
  10. {
  11. /**
  12. * Authorization level of a basic admin session
  13. *
  14. * @see _isAllowed()
  15. */
  16. const ADMIN_RESOURCE = 'Magento_Paypal::fetch';
  17. /**
  18. * Forced fetch reports action
  19. *
  20. * @return \Magento\Backend\Model\View\Result\Redirect
  21. */
  22. public function execute()
  23. {
  24. try {
  25. $reports = $this->_settlementFactory->create();
  26. /* @var $reports \Magento\Paypal\Model\Report\Settlement */
  27. $credentials = $reports->getSftpCredentials();
  28. if (empty($credentials)) {
  29. throw new \Magento\Framework\Exception\LocalizedException(
  30. __('We found nothing to fetch because of an empty configuration.')
  31. );
  32. }
  33. foreach ($credentials as $config) {
  34. try {
  35. $fetched = $reports->fetchAndSave(
  36. \Magento\Paypal\Model\Report\Settlement::createConnection($config)
  37. );
  38. $this->messageManager->addSuccessMessage(
  39. __(
  40. 'We fetched %1 report rows from "%2@%3."',
  41. $fetched,
  42. $config['username'],
  43. $config['hostname']
  44. )
  45. );
  46. } catch (\Exception $e) {
  47. $this->messageManager->addExceptionMessage(
  48. $e,
  49. __('We can\'t fetch reports from "%1@%2."', $config['username'], $config['hostname'])
  50. );
  51. }
  52. }
  53. } catch (\Exception $e) {
  54. $this->messageManager->addExceptionMessage($e, $e->getMessage());
  55. }
  56. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  57. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  58. return $resultRedirect->setPath('*/*/index');
  59. }
  60. }