CacheFileHelper::convertExpirationToSeconds()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 17
rs 9.8666
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Silviooosilva\CacheerPhp\Helpers\CacheerHelper;
6
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException;
7
8
/**
9
 * Class CacheFileHelper
10
 * @author Sílvio Silva <https://github.com/silviooosilva>
11
 * @package Silviooosilva\CacheerPhp
12
 */
13
class CacheFileHelper
14
{
15
16
    /**
17
     * Converts a string expiration format to seconds.
18
     *
19
     * @param string $expiration
20
     * @return float|int
21
     * @throws CacheFileException
22
     */
23
    public static function convertExpirationToSeconds(string $expiration): float|int
24
    {
25
        $units = [
26
            'second' => 1,
27
            'minute' => 60,
28
            'hour'   => 3600,
29
            'day'    => 86400,
30
            'week'   => 604800,
31
            'month'  => 2592000,
32
            'year'   => 31536000,
33
        ];
34
        foreach ($units as $unit => $value) {
35
            if (str_contains($expiration, $unit)) {
36
                return (int)$expiration * $value;
37
            }
38
        }
39
        throw CacheFileException::create("Invalid expiration format");
40
    }
41
42
    /**
43
     * Merges cache data with existing data.
44
     *
45
     * @param $cacheData
46
     * @return array
47
     */
48
    public static function mergeCacheData($cacheData): array
49
    {
50
        return CacheerHelper::mergeCacheData($cacheData);
51
    }
52
53
    /**
54
     * Validates a cache item.
55
     * 
56
     * @param array $item
57
     * @return void
58
     */
59
    public static function validateCacheItem(array $item): void
60
    {
61
        CacheerHelper::validateCacheItem(
62
            $item,
63
            fn($msg) => CacheFileException::create($msg)
64
        );
65
    }
66
67
    /**
68
     * Calculates the TTL (Time To Live) for cache items.
69
     *
70
     * @param null $ttl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ttl is correct as it would always require null to be passed?
Loading history...
71
     * @param int|null $defaultTTL
72
     * @return mixed
73
     * @throws CacheFileException
74
     */
75
    public static function ttl($ttl = null, ?int $defaultTTL = null): mixed
76
    {
77
        if ($ttl) {
0 ignored issues
show
introduced by
$ttl is of type null, thus it always evaluated to false.
Loading history...
78
            $ttl = is_string($ttl) ? self::convertExpirationToSeconds($ttl) : $ttl;
79
        } else {
80
            $ttl = $defaultTTL;
81
        }
82
        return $ttl;
83
    }
84
85
  /**
86
  * Generates an array identifier for cache data.
87
  * 
88
  * @param mixed $currentCacheData
89
  * @param mixed $cacheData
90
  * @return array
91
  */
92
  public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData): array
93
  {
94
    return CacheerHelper::arrayIdentifier($currentCacheData, $cacheData);
95
  }
96
}
97