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

WithFingerprint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 5
eloc 15
c 4
b 0
f 1
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fingerprint() 0 12 1
A canonicalize() 0 14 4
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
}