SwitchAction.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Store\Controller\Store;
  8. use Magento\Framework\App\Action\Action;
  9. use Magento\Framework\App\Action\Context as ActionContext;
  10. use Magento\Framework\App\Http\Context as HttpContext;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Store\Api\StoreCookieManagerInterface;
  14. use Magento\Store\Api\StoreRepositoryInterface;
  15. use Magento\Store\Model\StoreIsInactiveException;
  16. use Magento\Store\Model\StoreManagerInterface;
  17. use Magento\Store\Model\StoreSwitcher;
  18. use Magento\Store\Model\StoreSwitcherInterface;
  19. /**
  20. * Handles store switching url and makes redirect.
  21. *
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class SwitchAction extends Action
  25. {
  26. /**
  27. * @var StoreCookieManagerInterface
  28. */
  29. protected $storeCookieManager;
  30. /**
  31. * @var HttpContext
  32. * @deprecated 100.2.5
  33. */
  34. protected $httpContext;
  35. /**
  36. * @var StoreRepositoryInterface
  37. */
  38. protected $storeRepository;
  39. /**
  40. * @var StoreManagerInterface
  41. * @deprecated 100.2.5
  42. */
  43. protected $storeManager;
  44. /**
  45. * @var StoreSwitcherInterface
  46. */
  47. private $storeSwitcher;
  48. /**
  49. * Initialize dependencies.
  50. *
  51. * @param ActionContext $context
  52. * @param StoreCookieManagerInterface $storeCookieManager
  53. * @param HttpContext $httpContext
  54. * @param StoreRepositoryInterface $storeRepository
  55. * @param StoreManagerInterface $storeManager
  56. * @param StoreSwitcherInterface $storeSwitcher
  57. */
  58. public function __construct(
  59. ActionContext $context,
  60. StoreCookieManagerInterface $storeCookieManager,
  61. HttpContext $httpContext,
  62. StoreRepositoryInterface $storeRepository,
  63. StoreManagerInterface $storeManager,
  64. StoreSwitcherInterface $storeSwitcher = null
  65. ) {
  66. parent::__construct($context);
  67. $this->storeCookieManager = $storeCookieManager;
  68. $this->httpContext = $httpContext;
  69. $this->storeRepository = $storeRepository;
  70. $this->storeManager = $storeManager;
  71. $this->messageManager = $context->getMessageManager();
  72. $this->storeSwitcher = $storeSwitcher ?: ObjectManager::getInstance()->get(StoreSwitcherInterface::class);
  73. }
  74. /**
  75. * Execute action
  76. *
  77. * @return void
  78. * @throws StoreSwitcher\CannotSwitchStoreException
  79. */
  80. public function execute()
  81. {
  82. $targetStoreCode = $this->_request->getParam(
  83. \Magento\Store\Model\StoreManagerInterface::PARAM_NAME,
  84. $this->storeCookieManager->getStoreCodeFromCookie()
  85. );
  86. $fromStoreCode = $this->_request->getParam('___from_store');
  87. $requestedUrlToRedirect = $this->_redirect->getRedirectUrl();
  88. $redirectUrl = $requestedUrlToRedirect;
  89. $error = null;
  90. try {
  91. $fromStore = $this->storeRepository->get($fromStoreCode);
  92. $targetStore = $this->storeRepository->getActiveStoreByCode($targetStoreCode);
  93. } catch (StoreIsInactiveException $e) {
  94. $error = __('Requested store is inactive');
  95. } catch (NoSuchEntityException $e) {
  96. $error = __("The store that was requested wasn't found. Verify the store and try again.");
  97. }
  98. if ($error !== null) {
  99. $this->messageManager->addErrorMessage($error);
  100. } else {
  101. $redirectUrl = $this->storeSwitcher->switch($fromStore, $targetStore, $requestedUrlToRedirect);
  102. }
  103. $this->getResponse()->setRedirect($redirectUrl);
  104. }
  105. }