|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\GuzzleAdvancedThrottle\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
6
|
|
|
use GuzzleHttp\Utils; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
|
|
9
|
|
|
class RequestHelper |
|
10
|
|
|
{ |
|
11
|
22 |
|
public static function getHostAndPath(RequestInterface $request): array |
|
12
|
|
|
{ |
|
13
|
22 |
|
$uri = $request->getUri(); |
|
14
|
|
|
|
|
15
|
|
|
return [ |
|
16
|
22 |
|
$uri->getScheme() . '://' . $uri->getHost(), |
|
17
|
22 |
|
$uri->getPath() |
|
18
|
|
|
]; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
24 |
|
public static function getStorageKey(RequestInterface $request): string |
|
22
|
|
|
{ |
|
23
|
24 |
|
$method = $request->getMethod(); |
|
24
|
24 |
|
if ($method !== 'GET') { |
|
25
|
3 |
|
$contentType = $request->getHeader('Content-Type')[0] ?? null; |
|
26
|
3 |
|
$params = $request->getBody()->getContents(); |
|
27
|
|
|
|
|
28
|
3 |
|
return self::_getMethodAndParams( |
|
29
|
3 |
|
$method, |
|
30
|
3 |
|
$contentType === 'application/json' ? self::_decodeJSON($params) : $params |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
21 |
|
return self::_getMethodAndParams($method, $request->getUri()->getQuery()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
24 |
|
private static function _getMethodAndParams(string $method, string $params): string |
|
38
|
|
|
{ |
|
39
|
24 |
|
return $method . '_' . self::_sortParams($params); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
24 |
|
private static function _sortParams(string $params): string |
|
43
|
|
|
{ |
|
44
|
24 |
|
$paramArray = \explode('&', $params); |
|
45
|
24 |
|
\sort($paramArray); |
|
46
|
|
|
|
|
47
|
24 |
|
return \implode('&', $paramArray); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
private static function _decodeJSON(string $json): string |
|
51
|
|
|
{ |
|
52
|
1 |
|
if (empty($json)) { |
|
53
|
|
|
return ''; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return \http_build_query(Utils::jsonDecode($json, true), '', '&'); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
29 |
|
public static function getHostFromRequestAndOptions(RequestInterface $request, array $options = []): string |
|
60
|
|
|
{ |
|
61
|
29 |
|
$requestUri = $request->getUri(); |
|
62
|
|
|
|
|
63
|
29 |
|
if (Uri::isAbsolute($requestUri)) { |
|
64
|
10 |
|
return (string) $requestUri; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
20 |
|
if (isset($options['base_uri'])) { |
|
68
|
20 |
|
return $options['base_uri'] . |
|
69
|
20 |
|
UrlHelper::removeTrailingSlash( |
|
70
|
20 |
|
UrlHelper::prependSlash($requestUri) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return (string) $requestUri; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|