TExpiration   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 12
c 2
b 0
f 0
dl 0
loc 51
ccs 16
cts 18
cp 0.8889
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initExpiry() 0 4 1
A whenItExpire() 0 3 1
A updateExpirationTime() 0 4 4
A setExpirationNotice() 0 4 4
A getTime() 0 3 1
A shallExpire() 0 3 1
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