TExpiration::getTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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