CacheHelper::getCacheKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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