Edit.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryAdminUi\Controller\Adminhtml\Stock;
  8. use Magento\Backend\App\Action;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Backend\Model\View\Result\Page;
  11. use Magento\Framework\Controller\Result\Redirect;
  12. use Magento\Framework\Controller\ResultFactory;
  13. use Magento\Framework\Controller\ResultInterface;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15. use Magento\InventoryApi\Api\Data\StockInterface;
  16. use Magento\InventoryApi\Api\StockRepositoryInterface;
  17. use Magento\Framework\App\Action\HttpGetActionInterface;
  18. /**
  19. * Edit Controller
  20. */
  21. class Edit extends Action implements HttpGetActionInterface
  22. {
  23. /**
  24. * @see _isAllowed()
  25. */
  26. const ADMIN_RESOURCE = 'Magento_InventoryApi::stock';
  27. /**
  28. * @var StockRepositoryInterface
  29. */
  30. private $stockRepository;
  31. /**
  32. * @param Context $context
  33. * @param StockRepositoryInterface $stockRepository
  34. */
  35. public function __construct(
  36. Context $context,
  37. StockRepositoryInterface $stockRepository
  38. ) {
  39. parent::__construct($context);
  40. $this->stockRepository = $stockRepository;
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function execute(): ResultInterface
  46. {
  47. $stockId = (int)$this->getRequest()->getParam(StockInterface::STOCK_ID);
  48. try {
  49. $stock = $this->stockRepository->get($stockId);
  50. /** @var Page $result */
  51. $result = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  52. $result->setActiveMenu('Magento_InventoryApi::stock')
  53. ->addBreadcrumb(__('Edit Stock'), __('Edit Stock'));
  54. $result->getConfig()
  55. ->getTitle()
  56. ->prepend(__('Edit Stock: %name', ['name' => $stock->getName()]));
  57. } catch (NoSuchEntityException $e) {
  58. /** @var Redirect $result */
  59. $result = $this->resultRedirectFactory->create();
  60. $this->messageManager->addErrorMessage(
  61. __('Stock with id "%value" does not exist.', ['value' => $stockId])
  62. );
  63. $result->setPath('*/*');
  64. }
  65. return $result;
  66. }
  67. }