Passed
Push — master ( 6fb631...8b45a0 )
by Petr
08:04
created

BannedInServerKey::process()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
namespace kalanis\kw_auth\Methods;
4
5
6
use ArrayAccess;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Interfaces\IAuth;
9
use kalanis\kw_auth\Interfaces\IKauTranslations;
10
use kalanis\kw_auth\Traits\TLang;
11
use kalanis\kw_bans\Bans;
12
use kalanis\kw_bans\BanException;
13
14
15
/**
16
 * Class BannedInServerKey
17
 * @package kalanis\kw_auth\AuthMethods
18
 * Throws an exception if incoming user is banned
19
 * Just add it before any other method which try to log user in
20
 */
21
class BannedInServerKey extends AMethods
22
{
23
    use TLang;
24
25
    /** @var Bans\ABan */
26
    protected $libBan = null;
27
    /** @var ArrayAccess<string, string|int|float> */
28
    protected $server = null;
29
    /** @var string */
30
    protected $credentialKey = '';
31
32
    /**
33
     * @param IAuth|null $authenticator
34
     * @param AMethods|null $nextOne
35
     * @param ArrayAccess<string, string|int|float> $server
36
     * @param Bans\ABan $ban
37
     * @param string $credentialKey
38
     * @param IKauTranslations|null $kauLang
39
     */
40 3
    public function __construct(
41
        ?IAuth $authenticator,
42
        ?AMethods $nextOne,
43
        ArrayAccess $server,
44
        Bans\ABan $ban,
45
        string $credentialKey,
46
        ?IKauTranslations $kauLang = null
47
    ) {
48 3
        parent::__construct($authenticator, $nextOne);
49 3
        $this->setAuLang($kauLang);
50 3
        $this->server = $server;
51 3
        $this->libBan = $ban;
52 3
        $this->credentialKey = $credentialKey;
53 3
    }
54
55 3
    public function process(\ArrayAccess $credentials): void
56
    {
57
        try {
58 3
            if ($this->server->offsetExists($this->credentialKey)) {
59 3
                $this->libBan->setLookedFor(strval($this->server->offsetGet($this->credentialKey)));
60 2
                if ($this->libBan->isBanned()) {
61 2
                    throw new AuthException($this->getAuLang()->kauBanWantedUser(), 401);
62
                }
63
            }
64 2
        } catch (BanException $ex) {
65 1
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
66
        }
67 1
    }
68
69 1
    public function remove(): void
70
    {
71 1
    }
72
}
73