Passed
Push — master ( f03d23...eaba00 )
by Petr
02:26
created

UsersRecord::setUserData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 7
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_auth\Sources\Mapper\Database;
4
5
6
use kalanis\kw_auth\AuthException;
7
use kalanis\kw_auth\Interfaces\IAccessClasses;
8
use kalanis\kw_auth\Interfaces\IUserCert;
9
use kalanis\kw_mapper\Interfaces\IEntryType;
10
use kalanis\kw_mapper\Records\ASimpleRecord;
11
12
13
/**
14
 * Class UsersRecord
15
 * @package kalanis\kw_auth\Sources\Mapper\Database
16
 * @property string $id
17
 * @property string $login
18
 * @property string $pass
19
 * @property string $groupId
20
 * @property string $display
21
 * @property string $cert
22
 * @property string $salt
23
 * @property GroupsRecord[] $groups
24
 * @codeCoverageIgnore remote source
25
 */
26
class UsersRecord extends ASimpleRecord implements IUserCert
27
{
28
    public function addEntries(): void
29
    {
30
        $this->addEntry('id', IEntryType::TYPE_STRING, 2048);
31
        $this->addEntry('login', IEntryType::TYPE_STRING, 512);
32
        $this->addEntry('pass', IEntryType::TYPE_STRING, 512);
33
        $this->addEntry('groupId', IEntryType::TYPE_STRING, 512);
34
        $this->addEntry('display', IEntryType::TYPE_STRING, 512);
35
        $this->addEntry('cert', IEntryType::TYPE_STRING, 8192);
36
        $this->addEntry('salt', IEntryType::TYPE_STRING, 1024);
37
        $this->addEntry('groups', IEntryType::TYPE_ARRAY, []);
38
        $this->setMapper(UsersMapper::class);
39
    }
40
41
    public function setUserData(?string $authId, ?string $authName, ?string $authGroup, ?int $authClass, ?int $authStatus, ?string $displayName, ?string $dir): void
42
    {
43
        if (empty($authId)) {
44
            throw new AuthException('No user ID');
45
        }
46
        $this->id = $authId;
47
        $this->load();
48
        $this->login = $authName ?? $this->login;
49
        $this->groupId = $authGroup ?? $this->groupId;
50
        $this->display = $displayName ?? $this->display;
51
        $this->save();
52
    }
53
54
    public function addCertInfo(?string $key, ?string $salt): void
55
    {
56
        $this->load();
57
        $this->cert = $key ?? $this->cert;
58
        $this->salt = $salt ?? $this->salt;
59
        $this->save();
60
    }
61
62
    public function getAuthId(): string
63
    {
64
        return strval($this->id);
65
    }
66
67
    public function getAuthName(): string
68
    {
69
        return strval($this->login);
70
    }
71
72
    public function getGroup(): string
73
    {
74
        return strval($this->groupId);
75
    }
76
77
    public function getClass(): int
78
    {
79
        return IAccessClasses::CLASS_USER;
80
    }
81
82
    public function getStatus(): ?int
83
    {
84
        return static::USER_STATUS_ENABLED;
85
    }
86
87
    public function getDisplayName(): string
88
    {
89
        return strval($this->display);
90
    }
91
92
    public function getDir(): string
93
    {
94
        return '/';
95
    }
96
97
    public function getPubSalt(): string
98
    {
99
        return strval($this->salt);
100
    }
101
102
    public function getPubKey(): string
103
    {
104
        return strval($this->cert);
105
    }
106
}
107