CacheKeyBuilder::buildKeys()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCacheAdapter\CacheItemPool;
6
7
use RemotelyLiving\PHPCacheAdapter\Assertions;
8
9
final class CacheKeyBuilder
10
{
11
    private string $namespace = '';
12
13
    private int $namespaceLength;
14
15
    private function __construct(?string $namespace = null)
16
    {
17
        if ($namespace) {
18
            $this->namespace = $namespace . ':';
19
        }
20
21
        $this->namespaceLength = \mb_strlen($this->namespace);
22
    }
23
24
    public static function create(?string $namespace = null): self
25
    {
26
        return new self($namespace);
27
    }
28
29
    /**
30
     * @param mixed $key
31
     *
32
     * @return string
33
     */
34
    public function buildKey($key): string
35
    {
36
        Assertions::assertValidKey($key);
37
        return "{$this->namespace}{$key}";
38
    }
39
40
    public function removeNamespace(string $key): string
41
    {
42
        return \mb_substr($key, $this->namespaceLength);
43
    }
44
45
    public function buildKeys(array $keys): array
46
    {
47
        $prefixed = [];
48
        foreach ($keys as $key) {
49
            $prefixed[] = $this->buildKey($key);
50
        }
51
52
        return $prefixed;
53
    }
54
}
55