Passed
Branch main (6fd149)
by Sílvio
02:57
created

ArrayCacheKeyspace   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isExpired() 0 5 2
A split() 0 7 2
A build() 0 3 2
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore\Support;
4
5
/**
6
 * Handles namespace/key formatting and expiration checks for array cache.
7
 */
8
final class ArrayCacheKeyspace
9
{
10
    /**
11
     * @param string $cacheKey
12
     * @param string $namespace
13
     * @return string
14
     */
15
    public function build(string $cacheKey, string $namespace = ''): string
16
    {
17
        return $namespace !== '' ? ($namespace . ':' . $cacheKey) : $cacheKey;
18
    }
19
20
    /**
21
     * @param string $arrayStoreKey
22
     * @return array{0:string,1:string}
23
     */
24
    public function split(string $arrayStoreKey): array
25
    {
26
        $parts = explode(':', $arrayStoreKey, 2);
27
        if (count($parts) === 2) {
28
            return [$parts[0], $parts[1]];
29
        }
30
        return ['', $arrayStoreKey];
31
    }
32
33
    /**
34
     * @param array $cacheData
35
     * @return bool
36
     */
37
    public function isExpired(array $cacheData): bool
38
    {
39
        $expirationTime = $cacheData['expirationTime'] ?? 0;
40
        $now = time();
41
        return $expirationTime !== 0 && $now >= $expirationTime;
42
    }
43
}
44