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

TExpiration   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 53
ccs 18
cts 18
cp 1
rs 10
wmc 12

6 Methods

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