Forward.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Pickup;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Controller\ResultInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Temando\Shipping\Model\Pickup\PickupLoader;
  11. use Temando\Shipping\Model\PickupInterface;
  12. use Temando\Shipping\Model\ResourceModel\Order\OrderRepository;
  13. use Temando\Shipping\ViewModel\DataProvider\PickupUrl;
  14. /**
  15. * Temando Pickup Forward Action
  16. *
  17. * Redirect to pickup details view based on given request parameters
  18. *
  19. * @package Temando\Shipping\Controller
  20. * @author Nathan Wilson <nathan.wilson@temando.com>
  21. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link https://www.temando.com/
  23. */
  24. class Forward extends Action
  25. {
  26. const ADMIN_RESOURCE = 'Temando_Shipping::pickups';
  27. /**
  28. * @var PickupLoader
  29. */
  30. private $pickupLoader;
  31. /**
  32. * @var OrderRepository
  33. */
  34. private $orderRepository;
  35. /**
  36. * @var PickupUrl
  37. */
  38. private $pickupUrl;
  39. /**
  40. * Forward constructor.
  41. *
  42. * @param Context $context
  43. * @param PickupLoader $pickupLoader
  44. * @param OrderRepository $orderRepository
  45. * @param PickupUrl $pickupUrl
  46. */
  47. public function __construct(
  48. Context $context,
  49. PickupLoader $pickupLoader,
  50. OrderRepository $orderRepository,
  51. PickupUrl $pickupUrl
  52. ) {
  53. $this->pickupLoader = $pickupLoader;
  54. $this->orderRepository = $orderRepository;
  55. $this->pickupUrl = $pickupUrl;
  56. parent::__construct($context);
  57. }
  58. /**
  59. * Redirect to the Pickup view page
  60. *
  61. * @return ResultInterface
  62. */
  63. public function execute()
  64. {
  65. $redirect = $this->resultRedirectFactory->create();
  66. $forwardId = $this->getRequest()->getParam('pickup_id', '');
  67. $pickupId = preg_filter('/^PID([0-9]*)/', '$1', $forwardId);
  68. $pickups = $this->pickupLoader->load(0, (int) $pickupId);
  69. if (!isset($pickups[$pickupId])) {
  70. // redirect to pickups listing if pickup does not exist at the platform.
  71. $this->messageManager->addErrorMessage(__('Pickup "%1" does not exist.', $pickupId));
  72. $redirect->setPath('*/*/');
  73. return $redirect;
  74. }
  75. try {
  76. $pickup = $pickups[$pickupId];
  77. $order = $this->orderRepository->getReferenceByExtOrderId($pickup->getOrderId());
  78. } catch (NoSuchEntityException $e) {
  79. // redirect to pickups listing if associated order does not exist locally.
  80. $this->messageManager->addErrorMessage($e->getMessage());
  81. $redirect->setPath('*/*/');
  82. return $redirect;
  83. }
  84. $urlParams = [
  85. PickupInterface::PICKUP_ID => $pickupId,
  86. PickupInterface::SALES_ORDER_ID => $order->getOrderId(),
  87. ];
  88. if ($pickup->getState() === PickupInterface::STATE_REQUESTED) {
  89. $url = $this->pickupUrl->getEditActionUrl($urlParams);
  90. } else {
  91. $url = $this->pickupUrl->getViewActionUrl($urlParams);
  92. }
  93. $redirect->setUrl($url);
  94. return $redirect;
  95. }
  96. }