AccountsDatabase::getCertData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace kalanis\kw_auth_sources\Sources\Mapper;
4
5
6
use kalanis\kw_accounts\AccountsException;
7
use kalanis\kw_accounts\Interfaces as acc_interfaces;
8
use kalanis\kw_auth_sources\AuthSourcesException;
9
use kalanis\kw_auth_sources\Interfaces;
10
use kalanis\kw_auth_sources\Traits\TLang;
11
use kalanis\kw_auth_sources\Traits\TSeparated;
12
use kalanis\kw_mapper\MapperException;
13
use kalanis\kw_mapper\Search\Search;
14
15
16
/**
17
 * Class Database
18
 * @package kalanis\kw_auth_sources\Sources\Mapper
19
 * Authenticate via Database
20
 * need kw_mapper!
21
 * @codeCoverageIgnore because access external content
22
 */
23
class AccountsDatabase implements acc_interfaces\IAuthCert, acc_interfaces\IProcessAccounts
24
{
25
    use TLang;
26
    use TSeparated;
27
28
    protected Interfaces\IHashes $passMode;
29
    protected Database\UsersRecord $usersRecord;
30
31 1
    public function __construct(Interfaces\IHashes $mode, ?Interfaces\IKAusTranslations $lang = null)
32
    {
33 1
        $this->setAusLang($lang);
34 1
        $this->passMode = $mode;
35 1
        $this->usersRecord = new Database\UsersRecord();
36 1
    }
37
38
    public function authenticate(string $userName, array $params = []): ?acc_interfaces\IUser
39
    {
40
        if (!isset($params['password'])) {
41
            throw new AccountsException($this->getAusLang()->kauPassMustBeSet());
42
        }
43
        try {
44
            $record = $this->getByLogin($userName);
45
            if (empty($record)) {
46
                return null;
47
            }
48
49
            if (!$this->passMode->checkHash(isset($params['password']) ? strval($params['password']): '', $record->pass)) {
50
                return null;
51
            }
52
            return $record;
53
54
        } catch (AuthSourcesException | MapperException $ex) {
55
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
56
        }
57
    }
58
59
    public function getDataOnly(string $userName): ?acc_interfaces\IUser
60
    {
61
        try {
62
            return $this->getByLogin($userName);
63
64
        } catch (MapperException $ex) {
65
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
66
        }
67
    }
68
69
    public function updateCertData(string $userName, ?string $certKey, ?string $certSalt): bool
70
    {
71
        try {
72
            $record = clone $this->usersRecord;
73
            $record->login = $userName;
74
            $record->load();
75
            $record->cert = strval($certKey);
76
            $record->salt = strval($certSalt);
77
            return $record->save();
78
79
        } catch (MapperException $ex) {
80
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
81
        }
82
    }
83
84
    public function getCertData(string $userName): ?acc_interfaces\ICert
85
    {
86
        try {
87
            return $this->getByLogin($userName);
88
89
        } catch (MapperException $ex) {
90
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
91
        }
92
    }
93
94
    /**
95
     * @param string $login
96
     * @throws MapperException
97
     * @return Database\UsersRecord|null
98
     */
99
    protected function getByLogin(string $login): ?Database\UsersRecord
100
    {
101
        $record = clone $this->usersRecord;
102
        $record->login = $login;
103
        if (empty($record->count())) {
104
            return null;
105
        }
106
        $record->load();
107
        return $record;
108
    }
109
110
    public function createAccount(acc_interfaces\IUser $user, string $password): bool
111
    {
112
        try {
113
            $record = clone $this->usersRecord;
114
            $this->checkLogin($user->getAuthName());
115
            $record->login = $user->getAuthName();
116
            $record->groupId = $user->getGroup();
117
            $record->display = $user->getDisplayName();
118
            return $record->save(true);
119
120
        } catch (MapperException $ex) {
121
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
122
        }
123
    }
124
125
    /**
126
     * @throws AccountsException
127
     * @return Database\UsersRecord[]
128
     */
129
    public function readAccounts(): array
130
    {
131
        try {
132
            $search = new Search(clone $this->usersRecord);
133
            return $search->getResults();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $search->getResults() returns the type kalanis\kw_mapper\Records\ARecord[] which is incompatible with the return type mandated by kalanis\kw_accounts\Inte...ccounts::readAccounts() of kalanis\kw_accounts\Interfaces\IUser[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
134
135
        } catch (MapperException $ex) {
136
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
137
        }
138
    }
139
140
    public function updateAccount(acc_interfaces\IUser $user): bool
141
    {
142
        try {
143
            $this->checkLogin($user->getAuthName(), $user->getAuthId());
144
            $record = clone $this->usersRecord;
145
            $record->id = $user->getAuthId();
146
            $record->load();
147
            $record->login = $user->getAuthName();
148
            $record->groupId = $user->getGroup();
149
            $record->display = $user->getDisplayName();
150
            return $record->save();
151
152
        } catch (MapperException $ex) {
153
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
154
        }
155
    }
156
157
    public function updatePassword(string $userName, string $passWord): bool
158
    {
159
        try {
160
            $record = clone $this->usersRecord;
161
            $record->login = $userName;
162
            $record->load();
163
            $record->pass = $this->passMode->createHash($passWord);
164
            return $record->save();
165
166
        } catch (AuthSourcesException | MapperException $ex) {
167
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
168
        }
169
    }
170
171
    public function deleteAccount(string $userName): bool
172
    {
173
        try {
174
            $record = clone $this->usersRecord;
175
            $record->login = $userName;
176
            return $record->delete();
177
178
        } catch (MapperException $ex) {
179
            throw new AccountsException($ex->getMessage(), $ex->getCode(), $ex);
180
        }
181
    }
182
183
    /**
184
     * @param string $login
185
     * @param string $id
186
     * @throws AccountsException
187
     * @throws MapperException
188
     */
189
    protected function checkLogin(string $login, string $id = '0'): void
190
    {
191
        $user = clone $this->usersRecord;
192
        $user->login = $login;
193
        $amount = $user->count();
194
        if (1 > $amount) {
195
            return;
196
        }
197
        if (1 < $amount) {
198
            throw new AccountsException('Too many users with that login!');
199
        }
200
        $user->load();
201
        if ($id && ($user->id != $id)) {
202
            throw new AccountsException('Login already used.');
203
        }
204
    }
205
}
206