Files
MageSail/Magesail/Model/TunnelRequestDetector.php
T
2026-03-20 23:48:02 -05:00

48 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace MageSail\Magesail\Model;
use Magento\Framework\App\Request\Http;
/**
* Detects MageSail tunnel proxy (header / query / server / hostname vs persisted tunnel URLs) on the request.
*/
class TunnelRequestDetector
{
private const HEADER_STORE_CODE = 'X-MageSail-Store-Code';
private const QUERY_PARAM_STORE = 'magesail_store';
private const SERVER_HEADER = 'HTTP_X_MAGESAIL_STORE_CODE';
public function __construct(
private readonly TunnelHostnameMatcher $tunnelHostnameMatcher
) {
}
public function getTunnelStoreCode(Http $request): ?string
{
$value = $request->getParam(self::QUERY_PARAM_STORE);
if ($value !== null && trim((string) $value) !== '') {
return trim((string) $value);
}
$value = $request->getHeader(self::HEADER_STORE_CODE);
if ($value !== false && $value !== null && trim((string) $value) !== '') {
return trim((string) $value);
}
$value = $request->getServer(self::SERVER_HEADER);
if ($value !== null && $value !== '' && trim((string) $value) !== '') {
return trim((string) $value);
}
$uri = (string) $request->getRequestUri();
if ($uri !== '' && str_contains($uri, self::QUERY_PARAM_STORE . '=')) {
$query = str_contains($uri, '?') ? substr($uri, strpos($uri, '?') + 1) : '';
parse_str($query, $params);
$v = $params[self::QUERY_PARAM_STORE] ?? '';
if ($v !== '' && trim((string) $v) !== '') {
return trim((string) $v);
}
}
return $this->tunnelHostnameMatcher->getStoreCodeIfRequestHostMatchesState($request);
}
}