Passed
Push — master ( 2c97eb...c298b6 )
by Petr
02:20
created

TExpire   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A expiresAt() 0 10 4
A getExpire() 0 3 1
A expiresAfter() 0 11 4
1
<?php
2
3
namespace kalanis\kw_cache_psr\Traits;
4
5
6
use DateInterval;
7
use DateTimeInterface;
8
use Exception;
9
10
11
/**
12
 * Trait TExpire
13
 * @package kalanis\kw_cache_psr\Traits
14
 * Check expiration
15
 */
16
trait TExpire
17
{
18
    protected ?int $expire = null;
19
20
    /**
21
     * @param object|DateTimeInterface|int|null $expiration
22
     * @return $this
23
     */
24 3
    public function expiresAt($expiration)
25
    {
26 3
        $when = null;
27 3
        if (is_object($expiration) && ($expiration instanceof DateTimeInterface)) {
28 1
            $when = $expiration->getTimestamp();
29 3
        } elseif (is_int($expiration)) {
30 3
            $when = $expiration;
31
        }
32 3
        $this->expire = $when;
33 3
        return $this;
34
    }
35
36
    /**
37
     * @param object|DateInterval|int|null $time
38
     * @throws Exception
39
     * @return $this
40
     */
41 1
    public function expiresAfter($time)
42
    {
43 1
        $when = null;
44 1
        if (is_object($time) && ($time instanceof DateInterval)) {
45 1
            $expiration = new \DateTime();
46 1
            $when = $expiration->add($time)->getTimestamp();
47 1
        } elseif (is_int($time)) {
48 1
            $when = time() + $time;
49
        }
50 1
        $this->expire = $when;
51 1
        return $this;
52
    }
53
54 7
    public function getExpire(): ?int
55
    {
56 7
        return $this->expire;
57
    }
58
}
59