Timeout   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 33
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A timeLeft() 0 3 1
A updateExpire() 0 4 1
A isRunning() 0 3 1
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