SwitcherUrlProvider.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Store\ViewModel;
  8. use Magento\Framework\App\ActionInterface;
  9. use Magento\Framework\Url\EncoderInterface;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Store\Model\Store;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. /**
  14. * Provides target store redirect url.
  15. */
  16. class SwitcherUrlProvider implements \Magento\Framework\View\Element\Block\ArgumentInterface
  17. {
  18. /**
  19. * @var EncoderInterface
  20. */
  21. private $encoder;
  22. /**
  23. * @var StoreManagerInterface
  24. */
  25. private $storeManager;
  26. /**
  27. * @var UrlInterface
  28. */
  29. private $urlBuilder;
  30. /**
  31. * @param EncoderInterface $encoder
  32. * @param StoreManagerInterface $storeManager
  33. * @param UrlInterface $urlBuilder
  34. */
  35. public function __construct(
  36. EncoderInterface $encoder,
  37. StoreManagerInterface $storeManager,
  38. UrlInterface $urlBuilder
  39. ) {
  40. $this->encoder = $encoder;
  41. $this->storeManager = $storeManager;
  42. $this->urlBuilder = $urlBuilder;
  43. }
  44. /**
  45. * Returns target store redirect url.
  46. *
  47. * @param Store $store
  48. * @return string
  49. * @throws \Magento\Framework\Exception\NoSuchEntityException
  50. */
  51. public function getTargetStoreRedirectUrl(Store $store): string
  52. {
  53. return $this->urlBuilder->getUrl(
  54. 'stores/store/redirect',
  55. [
  56. '___store' => $store->getCode(),
  57. '___from_store' => $this->storeManager->getStore()->getCode(),
  58. ActionInterface::PARAM_NAME_URL_ENCODED => $this->encoder->encode(
  59. $store->getCurrentUrl(false)
  60. ),
  61. ]
  62. );
  63. }
  64. }