/** * Retrieve current url for store * * @param bool|string $fromStore * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getCurrentUrl($fromStore = true) { $sidQueryParam = $this->_sidResolver->getSessionIdQueryParam($this->_getSession()); $requestString = $this->_url->escape(ltrim($this->_request->getRequestString(), '/')); $storeUrl = $this->getUrl('', ['_secure' => $this->_storeManager->getStore()->isCurrentlySecure()]); if (!filter_var($storeUrl, FILTER_VALIDATE_URL)) { return $storeUrl; } $storeParsedUrl = parse_url($storeUrl); $storeParsedQuery = []; if (isset($storeParsedUrl['query'])) { parse_str($storeParsedUrl['query'], $storeParsedQuery); } $currQuery = $this->_request->getQueryValue(); if (isset($currQuery[$sidQueryParam]) && !empty($currQuery[$sidQueryParam]) && $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]) { unset($currQuery[$sidQueryParam]); } foreach ($currQuery as $key => $value) { $storeParsedQuery[$key] = $value; } if (!$this->isUseStoreInUrl()) { $storeParsedQuery['___store'] = $this->getCode(); } if ($fromStore !== false) { $storeParsedQuery['___from_store'] = $fromStore === true ? $this->_storeManager->getStore()->getCode() : $fromStore; } $currentUrl = $storeParsedUrl['scheme'] . '://' . $storeParsedUrl['host'] . (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '') . $storeParsedUrl['path'] . $requestString . ($storeParsedQuery ? '?' . http_build_query($storeParsedQuery, '', '&') : ''); return $currentUrl; }