AbstractDispatchAction.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Dispatch;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Framework\App\ResponseInterface;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Framework\Controller\ResultInterface;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Temando\Shipping\Model\DispatchProviderInterface;
  14. use Temando\Shipping\Model\ResourceModel\Repository\DispatchRepositoryInterface;
  15. /**
  16. * Temando List Dispatches Action
  17. *
  18. * @package Temando\Shipping\Controller
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.temando.com/
  23. */
  24. abstract class AbstractDispatchAction extends Action
  25. {
  26. const ADMIN_RESOURCE = 'Temando_Shipping::dispatches';
  27. /**
  28. * @var DispatchRepositoryInterface
  29. */
  30. private $dispatchRepository;
  31. /**
  32. * @var DispatchProviderInterface
  33. */
  34. private $dispatchProvider;
  35. /**
  36. * View constructor.
  37. * @param Context $context
  38. * @param DispatchRepositoryInterface $dispatchRepository
  39. * @param DispatchProviderInterface $dispatchProvider
  40. */
  41. public function __construct(
  42. Context $context,
  43. DispatchRepositoryInterface $dispatchRepository,
  44. DispatchProviderInterface $dispatchProvider
  45. ) {
  46. $this->dispatchRepository = $dispatchRepository;
  47. $this->dispatchProvider = $dispatchProvider;
  48. parent::__construct($context);
  49. }
  50. /**
  51. * @param RequestInterface $request
  52. * @return ResponseInterface|ResultInterface
  53. */
  54. public function dispatch(RequestInterface $request)
  55. {
  56. $dispatchId = $this->getRequest()->getParam('dispatch_id');
  57. try {
  58. $dispatch = $this->dispatchRepository->getById($dispatchId);
  59. // register dispatch for use in output rendering
  60. $this->dispatchProvider->setDispatch($dispatch);
  61. } catch (NoSuchEntityException $e) {
  62. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  63. $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
  64. return $resultForward->forward('noroute');
  65. }
  66. return parent::dispatch($request);
  67. }
  68. }