Completed
Push — master ( 5db45e...963acd )
by Timo
02:41
created

RequestHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
dl 0
loc 67
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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