Completed
Pull Request — master (#42)
by Chad
06:34
created

CacheHelper::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
1
<?php
2
3
namespace TraderInteractive\Api;
4
5
abstract class CacheHelper
6
{
7
    /**
8
     * Returns a valid PSR-11 key.
9
     *
10
     * @param Request $request The request from which the key will be generated.
11
     *
12
     * @return string
13
     */
14
    public static function getCacheKey(Request $request) : string
15
    {
16
        $key = "{$request->getUrl()}|{$request->getBody()}";
17
        $map = [
18
            '{' => '_LBRACE_',
19
            '}'=> '_RBRACE_',
20
            '('=> '_LPAREN_',
21
            ')'=> '_RPAREN_',
22
            '/'=> '_FSLASH_',
23
            '\\'=> '_BSLASH_',
24
            '@'=> '_AT_',
25
            ':'=> '_COLON_',
26
        ];
27
28
        return str_replace(array_keys($map), $map, $key);
29
    }
30
}
31