CountedSessions::process()   A
last analyzed

Complexity

Conditions 6
Paths 15

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 16
ccs 0
cts 16
cp 0
crap 42
rs 9.2222
1
<?php
2
3
namespace kalanis\kw_auth\Methods;
4
5
6
use ArrayAccess;
7
use kalanis\kw_accounts\Interfaces\IAuth;
8
use kalanis\kw_auth\AuthException;
9
use kalanis\kw_auth\Interfaces\IKauTranslations;
10
use kalanis\kw_auth\Traits\TLang;
11
use SessionHandlerInterface;
12
13
14
/**
15
 * Class CountedSessions
16
 * @package kalanis\kw_auth\AuthMethods
17
 * Authenticate via Session - count tries
18
 * @codeCoverageIgnore external resource, Cannot start session when headers already sent
19
 */
20
class CountedSessions extends AMethods
21
{
22
    use TLang;
23
24
    protected const INPUT_NAME = 'name';
25
    protected const INPUT_COUNTER = 'log_count';
26
27
    protected int $maxTries = 100;
28
    /** @var ArrayAccess<string, string|int> */
29
    protected ArrayAccess $session;
30
    protected ?SessionHandlerInterface $externalHandler = null;
31
32
    /**
33
     * @param IAuth|null $authenticator
34
     * @param AMethods|null $nextOne
35
     * @param ArrayAccess<string, string|int> $session
36
     * @param int $maxTries
37
     * @param IKauTranslations|null $lang
38
     * @param SessionHandlerInterface|null $externalHandler
39
     */
40
    public function __construct(?IAuth $authenticator, ?AMethods $nextOne, ArrayAccess $session, int $maxTries = 100, ?IKauTranslations $lang = null, ?SessionHandlerInterface $externalHandler = null)
41
    {
42
        parent::__construct($authenticator, $nextOne);
43
        $this->setAuLang($lang);
44
        $this->session = $session;
45
        $this->maxTries = $maxTries;
46
        $this->externalHandler = $externalHandler;
47
    }
48
49
    /**
50
     * @param ArrayAccess<string, string|int|float> $credentials
51
     * @throws AuthException
52
     */
53
    public function process(ArrayAccess $credentials): void
54
    {
55
        if (PHP_SESSION_NONE == session_status()) {
56
            if ($this->externalHandler) {
57
                session_set_save_handler($this->externalHandler, true);
58
            }
59
            session_start();
60
        }
61
        if (!empty($credentials->offsetExists(static::INPUT_NAME))) {
62
            if (!$this->session->offsetExists(static::INPUT_COUNTER)) {
63
                $this->session->offsetSet(static::INPUT_COUNTER, 0);
64
            }
65
            if (intval(strval($this->session->offsetGet(static::INPUT_COUNTER))) < $this->maxTries) {
66
                $this->session->offsetSet(static::INPUT_COUNTER, strval(intval(strval($this->session->offsetGet(static::INPUT_COUNTER))) + 1));
67
            } else {
68
                throw new AuthException($this->getAuLang()->kauTooManyTries(), 429);
69
            }
70
        }
71
    }
72
73
    public function remove(): void
74
    {
75
        if (PHP_SESSION_ACTIVE == session_status()) {
76
            session_destroy();
77
        }
78
    }
79
}
80