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

DatabaseTtlResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore\Support;
4
5
use Silviooosilva\CacheerPhp\Helpers\CacheFileHelper;
6
7
/**
8
 * Resolves TTL values for the database store.
9
 */
10
final class DatabaseTtlResolver
11
{
12
    /** @var int|null */
13
    private ?int $defaultTTL;
14
15
    /**
16
     * @param int|null $defaultTTL
17
     */
18
    public function __construct(?int $defaultTTL)
19
    {
20
        $this->defaultTTL = $defaultTTL;
21
    }
22
23
    /**
24
     * @param string|int|null $ttl
25
     * @return int
26
     */
27
    public function resolve(string|int|null $ttl): int
28
    {
29
        $ttlToUse = $ttl;
30
31
        if ($this->defaultTTL !== null && ($ttl === null || (int) $ttl === 3600)) {
32
            $ttlToUse = $this->defaultTTL;
33
        }
34
35
        if (is_string($ttlToUse)) {
36
            $ttlToUse = (int) CacheFileHelper::convertExpirationToSeconds($ttlToUse);
37
        }
38
39
        return (int) $ttlToUse;
40
    }
41
}
42