Passed
Push — master ( be2d4f...7625ce )
by frey
01:09 queued 11s
created

WithFingerprint::canonicalize()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 4
nc 5
nop 2
1
<?php
2
3
namespace Freyo\ApiGateway\Kernel\Traits;
4
5
trait WithFingerprint
6
{
7
    /**
8
     * @param $method
9
     * @param $url
10
     * @param array $params
11
     *
12
     * @return string
13
     */
14
    protected function fingerprint($method, $url, array $params = [])
15
    {
16
        ksort($params);
17
18
        $srcStr = sprintf('%s%s%s?%s',
19
            $method,
20
            parse_url($url, PHP_URL_HOST),
21
            parse_url($url, PHP_URL_PATH),
22
            $this->canonicalize($params)
23
        );
24
25
        return hash('sha256', $srcStr);
26
    }
27
28
    /**
29
     * @param array $input
30
     * @param string $keyPrefix
31
     *
32
     * @return string
33
     */
34
    protected function canonicalize(array $input, $keyPrefix = '')
35
    {
36
        $resource = [];
37
38
        foreach ($input as $key => $value) {
39
40
            $key = $keyPrefix ? $keyPrefix . '.' . $key : $key;
41
42
            $resource[] = is_array($value)
43
                ? $this->canonicalize($value, $key)
44
                : $key . '=' . $value;
45
        }
46
47
        return implode('&', $resource);
48
    }
49
}