Passed
Push — master ( ed3164...71916c )
by Petr
02:23
created

TExpiration::initExpiry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_auth_sources\Traits;
4
5
6
use kalanis\kw_auth_sources\AuthSourcesException;
7
use kalanis\kw_auth_sources\Interfaces\IExpire;
8
9
10
/**
11
 * Trait TExpiration
12
 * @package kalanis\kw_auth_sources\Traits
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 AuthSourcesException
32
     */
33 3
    public function setExpirationNotice($class, int $nextChange): void
34
    {
35 3
        if ($class && is_object($class) && $class instanceof IExpire) {
36 1
            $class->setExpireNotice( $this->shallExpire($nextChange));
37
        }
38 3
    }
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 AuthSourcesException
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 3
    protected function whenItExpire(): int
57
    {
58 3
        return $this->getTime() + $this->changeInterval;
59
    }
60
61
    /**
62
     * @return int
63
     * @codeCoverageIgnore too dynamic
64
     */
65 2
    protected function getTime(): int
66
    {
67 2
        return time();
68
    }
69
}
70