AbstractBatchAction.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Batch;
  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\BatchProviderInterface;
  14. use Temando\Shipping\Model\ResourceModel\Repository\BatchRepositoryInterface;
  15. /**
  16. * Temando Batch Action
  17. *
  18. * Register a batch as referenced via request parameter
  19. *
  20. * @package Temando\Shipping\Controller
  21. * @author Rhodri Davies <rhodri.davies@temando.com>
  22. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. * @link http://www.temando.com/
  24. */
  25. abstract class AbstractBatchAction extends Action
  26. {
  27. const ADMIN_RESOURCE = 'Temando_Shipping::batches';
  28. /**
  29. * @var BatchRepositoryInterface
  30. */
  31. private $batchRepository;
  32. /**
  33. * @var BatchProviderInterface
  34. */
  35. private $batchProvider;
  36. /**
  37. * AbstractBatchAction constructor.
  38. * @param Context $context
  39. * @param BatchRepositoryInterface $batchRepository
  40. * @param BatchProviderInterface $batchProvider
  41. */
  42. public function __construct(
  43. Context $context,
  44. BatchRepositoryInterface $batchRepository,
  45. BatchProviderInterface $batchProvider
  46. ) {
  47. $this->batchRepository = $batchRepository;
  48. $this->batchProvider = $batchProvider;
  49. parent::__construct($context);
  50. }
  51. /**
  52. * @param RequestInterface $request
  53. * @return ResponseInterface|ResultInterface
  54. */
  55. public function dispatch(RequestInterface $request)
  56. {
  57. $batchId = $request->getParam('batch_id');
  58. try {
  59. $batch = $this->batchRepository->getById($batchId);
  60. // register batch for use in output rendering
  61. $this->batchProvider->setBatch($batch);
  62. } catch (NoSuchEntityException $e) {
  63. /** @var \Magento\Framework\Controller\Result\Forward $resultForward */
  64. $resultForward = $this->resultFactory->create(ResultFactory::TYPE_FORWARD);
  65. return $resultForward->forward('noroute');
  66. }
  67. return parent::dispatch($request);
  68. }
  69. }