Passed
Push — master ( 141183...2b56eb )
by Petr
08:03
created

TExpiration::getTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_auth\Sources;
4
5
6
use kalanis\kw_auth\AuthException;
7
use kalanis\kw_auth\Interfaces\IExpire;
8
9
10
/**
11
 * Trait TExpiration
12
 * @package kalanis\kw_auth\Sources
13
 * Expiration of password
14
 */
15
trait TExpiration
16
{
17
    /** @var int */
18
    protected $changeInterval = 31536000; // 60×60×24×365 - one year
19
    /** @var int */
20
    protected $changeNotice = 2592000; // 60×60×24×30 - one month
21
22 1
    protected function initExpiry(int $changeInterval, int $changeNoticeBefore): void
23
    {
24 1
        $this->changeInterval = $changeInterval;
25 1
        $this->changeNotice = $changeNoticeBefore;
26 1
    }
27
28
    /**
29
     * @param object|int|string $class
30
     * @param int $nextChange
31
     * @throws AuthException
32
     */
33 5
    public function setExpirationNotice($class, int $nextChange): void
34
    {
35 5
        if ($class && is_object($class) && $class instanceof IExpire) {
36 1
            $class->setExpireNotice( $this->shallExpire($nextChange));
37
        }
38 5
    }
39
40 1
    protected function shallExpire(int $nextChange): bool
41
    {
42 1
        return $this->getTime() + $this->changeNotice > $nextChange;
43
    }
44
45
    /**
46
     * @param object|int|string $class
47
     * @throws AuthException
48
     */
49 1
    public function updateExpirationTime($class): void
50
    {
51 1
        if ($class && is_object($class) && $class instanceof IExpire) {
52 1
            $class->updateExpireTime($this->whenItExpire());
53
        }
54 1
    }
55
56 4
    protected function whenItExpire(): int
57
    {
58 4
        return $this->getTime() + $this->changeInterval;
59
    }
60
61
    /**
62
     * @return int
63
     * @codeCoverageIgnore too dynamic
64
     */
65 3
    protected function getTime(): int
66
    {
67 3
        return time();
68
    }
69
}
70