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

ArrayCacheKeyspace::split()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
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