|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Store\ViewModel; |
| 10 | + |
| 11 | +use Magento\Framework\App\ActionInterface; |
| 12 | +use Magento\Framework\Url\EncoderInterface; |
| 13 | +use Magento\Framework\UrlInterface; |
| 14 | +use Magento\Store\Model\Store; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Provides target store redirect url. |
| 19 | + */ |
| 20 | +class SwitcherUrlProvider implements \Magento\Framework\View\Element\Block\ArgumentInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var EncoderInterface |
| 24 | + */ |
| 25 | + private $encoder; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var StoreManagerInterface |
| 29 | + */ |
| 30 | + private $storeManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var UrlInterface |
| 34 | + */ |
| 35 | + private $urlBuilder; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param EncoderInterface $encoder |
| 39 | + * @param StoreManagerInterface $storeManager |
| 40 | + * @param UrlInterface $urlBuilder |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + EncoderInterface $encoder, |
| 44 | + StoreManagerInterface $storeManager, |
| 45 | + UrlInterface $urlBuilder |
| 46 | + ) { |
| 47 | + $this->encoder = $encoder; |
| 48 | + $this->storeManager = $storeManager; |
| 49 | + $this->urlBuilder = $urlBuilder; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns target store redirect url. |
| 54 | + * |
| 55 | + * @param Store $store |
| 56 | + * @return string |
| 57 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 58 | + */ |
| 59 | + public function getTargetStoreRedirectUrl(Store $store): string |
| 60 | + { |
| 61 | + return $this->urlBuilder->getUrl( |
| 62 | + 'stores/store/redirect', |
| 63 | + [ |
| 64 | + '___store' => $store->getCode(), |
| 65 | + '___from_store' => $this->storeManager->getStore()->getCode(), |
| 66 | + ActionInterface::PARAM_NAME_URL_ENCODED => $this->encoder->encode( |
| 67 | + $store->getCurrentUrl(false) |
| 68 | + ), |
| 69 | + ] |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
0 commit comments