Timeout::updateExpire()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls\Security\Timeout;
4
5
6
use ArrayAccess;
7
use kalanis\kw_forms\Interfaces\ITimeout;
8
9
10
/**
11
 * Class Timeout
12
 * @package kalanis\kw_forms\Controls\Security\Timeout
13
 * Remember expiration and for a preset time set it as passing
14
 */
15
class Timeout implements ITimeout
16
{
17
    public const CAPTCHA_TIME = 'captchaTime';
18
19
    protected ArrayAccess $session;
20
    /** @var int When ends time of pass */
21
    protected int $time = 0;
22
    /** @var int How log interval is preset after correct response */
23
    protected int $timeout = 0;
24
25 1
    public function __construct(ArrayAccess &$session, int $timeout = 0)
26
    {
27 1
        $this->session = $session;
28 1
        $this->timeout = $timeout;
29 1
        $this->time = ($this->session->offsetExists(static::CAPTCHA_TIME))
30 1
            ? intval(strval($this->session->offsetGet(static::CAPTCHA_TIME)))
31 1
            : 0 ;
32 1
    }
33
34 1
    public function updateExpire(): void
35
    {
36 1
        $this->time = time() + $this->timeout; // when it begins
37 1
        $this->session->offsetSet(static::CAPTCHA_TIME, $this->time); // set into session
38 1
    }
39
40 1
    public function isRunning(): bool
41
    {
42 1
        return (0 < $this->timeLeft());
43
    }
44
45 1
    public function timeLeft(): int
46
    {
47 1
        return $this->time - time();
48
    }
49
}
50