Passed
Push — master ( 5a87d9...5af04f )
by Mihail
03:04
created

ClientTrait::secondsWithGlobalTtl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Caching\Client;
14
15
use function Koded\Caching\normalize_ttl;
16
use function Koded\Stdlib\now;
17
18
trait ClientTrait
19
{
20
    /** @var int|null Global TTL for caching, used as default expiration time in cache clients */
21
    private $ttl;
22
23
    /** @var \Memcached | \Redis | \Predis\Client | \Koded\Caching\Client\FileClient | \Koded\Caching\Client\MemoryClient | \Koded\Caching\Client\ShmopClient */
24
    private $client;
25
26 2
    public function getTtl(): ?int
27
    {
28 2
        return $this->ttl;
29
    }
30
31 11
    public function client()
32
    {
33 11
        return $this->client ?? $this;
34
    }
35
36 175
    private function timestampWithGlobalTtl($ttl, int $default = 0): int
37
    {
38 175
        $explicit = normalize_ttl($ttl);
39 145
        $now = now()->getTimestamp();
40
41 145
        if (null === $explicit && $this->ttl > 0) {
0 ignored issues
show
introduced by
The condition null === $explicit is always false.
Loading history...
42 2
            return $now + $this->ttl;
43
        }
44
45 144
        if ($explicit > 0) {
46 8
            return $now + $explicit;
47
        }
48
49 136
        return $explicit ?? $default;
50
    }
51
52 280
    private function secondsWithGlobalTtl($ttl): int
53
    {
54 280
        $seconds = normalize_ttl($ttl);
55
56 230
        if (null === $seconds && $this->ttl > 0) {
0 ignored issues
show
introduced by
The condition null === $seconds is always false.
Loading history...
57
            return (int)$this->ttl;
58
        }
59
60 230
        return (int)$seconds;
61
    }
62
}