OrderLoader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\AbstractController;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\Registry;
  10. use Magento\Framework\Controller\Result\ForwardFactory;
  11. use Magento\Framework\Controller\Result\RedirectFactory;
  12. class OrderLoader implements OrderLoaderInterface
  13. {
  14. /**
  15. * @var \Magento\Sales\Model\OrderFactory
  16. */
  17. protected $orderFactory;
  18. /**
  19. * @var \Magento\Framework\Registry
  20. */
  21. protected $registry;
  22. /**
  23. * @var OrderViewAuthorizationInterface
  24. */
  25. protected $orderAuthorization;
  26. /**
  27. * @var \Magento\Framework\UrlInterface
  28. */
  29. protected $url;
  30. /**
  31. * @var ForwardFactory
  32. */
  33. protected $resultForwardFactory;
  34. /**
  35. * @var RedirectFactory
  36. */
  37. protected $redirectFactory;
  38. /**
  39. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  40. * @param OrderViewAuthorizationInterface $orderAuthorization
  41. * @param Registry $registry
  42. * @param \Magento\Framework\UrlInterface $url
  43. * @param ForwardFactory $resultForwardFactory
  44. * @param RedirectFactory $redirectFactory
  45. */
  46. public function __construct(
  47. \Magento\Sales\Model\OrderFactory $orderFactory,
  48. OrderViewAuthorizationInterface $orderAuthorization,
  49. Registry $registry,
  50. \Magento\Framework\UrlInterface $url,
  51. ForwardFactory $resultForwardFactory,
  52. RedirectFactory $redirectFactory
  53. ) {
  54. $this->orderFactory = $orderFactory;
  55. $this->orderAuthorization = $orderAuthorization;
  56. $this->registry = $registry;
  57. $this->url = $url;
  58. $this->resultForwardFactory = $resultForwardFactory;
  59. $this->redirectFactory = $redirectFactory;
  60. }
  61. /**
  62. * @param RequestInterface $request
  63. * @return bool|\Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect
  64. */
  65. public function load(RequestInterface $request)
  66. {
  67. $orderId = (int)$request->getParam('order_id');
  68. if (!$orderId) {
  69. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  70. $resultForward = $this->resultForwardFactory->create();
  71. return $resultForward->forward('noroute');
  72. }
  73. $order = $this->orderFactory->create()->load($orderId);
  74. if ($this->orderAuthorization->canView($order)) {
  75. $this->registry->register('current_order', $order);
  76. return true;
  77. }
  78. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  79. $resultRedirect = $this->redirectFactory->create();
  80. return $resultRedirect->setUrl($this->url->getUrl('*/*/history'));
  81. }
  82. }