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

TExpire::expiresAfter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 4
rs 10
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