BannedInCredentialKey   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A remove() 0 2 1
A process() 0 11 4
1
<?php
2
3
namespace kalanis\kw_auth\Methods;
4
5
6
use kalanis\kw_accounts\Interfaces\IAuth;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Interfaces\IKauTranslations;
9
use kalanis\kw_auth\Traits\TLang;
10
use kalanis\kw_bans\Bans;
11
use kalanis\kw_bans\BanException;
12
13
14
/**
15
 * Class BannedInKey
16
 * @package kalanis\kw_auth\AuthMethods
17
 * Throws an exception if incoming user is banned
18
 * Just add it before any other method which try to log user in
19
 */
20
class BannedInCredentialKey extends AMethods
21
{
22
    use TLang;
23
24
    protected Bans\ABan $libBan;
25
    protected string $credentialKey = '';
26
27
    /**
28
     * @param IAuth|null $authenticator
29
     * @param AMethods|null $nextOne
30
     * @param Bans\ABan $ban
31
     * @param string $credentialKey
32
     * @param IKauTranslations|null $kauLang
33
     */
34 3
    public function __construct(
35
        ?IAuth $authenticator,
36
        ?AMethods $nextOne,
37
        Bans\ABan $ban,
38
        string $credentialKey,
39
        ?IKauTranslations $kauLang = null
40
    ) {
41 3
        parent::__construct($authenticator, $nextOne);
42 3
        $this->setAuLang($kauLang);
43 3
        $this->libBan = $ban;
44 3
        $this->credentialKey = $credentialKey;
45 3
    }
46
47 3
    public function process(\ArrayAccess $credentials): void
48
    {
49
        try {
50 3
            if ($credentials->offsetExists($this->credentialKey)) {
51 3
                $this->libBan->setLookedFor(strval($credentials->offsetGet($this->credentialKey)));
52 2
                if ($this->libBan->isBanned()) {
53 2
                    throw new AuthException($this->getAuLang()->kauBanWantedUser(), 401);
54
                }
55
            }
56 2
        } catch (BanException $ex) {
57 1
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
58
        }
59 1
    }
60
61 1
    public function remove(): void
62
    {
63 1
    }
64
}
65