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

ClientTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 43
ccs 16
cts 17
cp 0.9412
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A secondsWithGlobalTtl() 0 9 3
A timestampWithGlobalTtl() 0 14 4
A getTtl() 0 3 1
A client() 0 3 1
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
}