View.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Shipment;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Controller\ResultFactory;
  9. use Magento\Framework\Escaper;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Temando\Shipping\Model\ResourceModel\Repository\ShipmentReferenceRepositoryInterface;
  12. /**
  13. * Temando Redirect Shipment Page
  14. *
  15. * Query a Shipment ID based on given Platform ID and redirect to native shipment page.
  16. *
  17. * @package Temando\Shipping\Controller
  18. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  19. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  20. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link http://www.temando.com/
  22. */
  23. class View extends Action
  24. {
  25. const ADMIN_RESOURCE = 'Temando_Shipping::shipping';
  26. /**
  27. * @var ShipmentReferenceRepositoryInterface
  28. */
  29. private $shipmentReferenceRepository;
  30. /**
  31. * @var Escaper
  32. */
  33. private $escaper;
  34. /**
  35. * View constructor.
  36. *
  37. * @param Context $context
  38. * @param ShipmentReferenceRepositoryInterface $shipmentReferenceRepository
  39. * @param Escaper $escaper
  40. */
  41. public function __construct(
  42. Context $context,
  43. ShipmentReferenceRepositoryInterface $shipmentReferenceRepository,
  44. Escaper $escaper
  45. ) {
  46. $this->shipmentReferenceRepository = $shipmentReferenceRepository;
  47. $this->escaper = $escaper;
  48. parent::__construct($context);
  49. }
  50. /**
  51. * @return \Magento\Framework\Controller\AbstractResult
  52. */
  53. public function execute()
  54. {
  55. $extShipmentId = $this->escaper->escapeHtml($this->getRequest()->getParam('shipment_id'));
  56. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  57. $resultRedirect = $this->resultRedirectFactory->create();
  58. try {
  59. $shipmentReference = $this->shipmentReferenceRepository->getByExtShipmentId($extShipmentId);
  60. $resultRedirect->setPath('sales/shipment/view', ['shipment_id' => $shipmentReference->getShipmentId()]);
  61. } catch (LocalizedException $exception) {
  62. $message = "Shipment '$extShipmentId' not found.";
  63. $this->messageManager->addExceptionMessage($exception, __($message));
  64. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  65. $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
  66. $resultForward->forward('noroute');
  67. return $resultForward;
  68. }
  69. return $resultRedirect;
  70. }
  71. }