Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
created

Database::readGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace kalanis\kw_auth\Sources\Mapper;
4
5
6
use kalanis\kw_auth\AuthException;
7
use kalanis\kw_auth\Interfaces;
8
use kalanis\kw_mapper\Search\Search;
9
10
11
/**
12
 * Class Database
13
 * @package kalanis\kw_auth\Sources\Mapper
14
 * Authenticate via Database
15
 * need kw_mapper!
16
 * @codeCoverageIgnore because access external content
17
 */
18
class Database implements Interfaces\IAuth, Interfaces\IAuthCert, Interfaces\IAccessGroups
19
{
20
    /** @var Interfaces\IMode */
21
    protected $passMode = null;
22
    /** @var Database\UsersRecord */
23
    protected $usersRecord = null;
24
    /** @var Database\GroupsRecord */
25
    protected $groupsRecord = null;
26
27
    public function __construct(Interfaces\IMode $mode)
28
    {
29
        $this->passMode = $mode;
30
        $this->usersRecord = new Database\UsersRecord();
31
        $this->groupsRecord = new Database\GroupsRecord();
32
    }
33
34
    public function authenticate(string $userName, array $params = []): ?Interfaces\IUser
35
    {
36
        $record = $this->getByLogin($userName);
37
        if (empty($record)) {
38
            return null;
39
        }
40
        if (!$this->passMode->check($params['password'] ?: '', $record->pass)) {
41
            return null;
42
        }
43
        return $record;
44
    }
45
46
    public function getDataOnly(string $userName): ?Interfaces\IUser
47
    {
48
        return $this->getByLogin($userName);
49
    }
50
51
    public function updateCertKeys(string $userName, ?string $certKey, ?string $certSalt): void
52
    {
53
        $record = clone $this->usersRecord;
54
        $record->login = $userName;
55
        $record->load();
56
        $record->cert = strval($certKey);
57
        $record->salt = strval($certSalt);
58
        $record->save();
59
    }
60
61
    public function getCertData(string $userName): ?Interfaces\IUserCert
62
    {
63
        return $this->getByLogin($userName);
64
    }
65
66
    protected function getByLogin(string $login): ?Database\UsersRecord
67
    {
68
        $record = clone $this->usersRecord;
69
        $record->login = $login;
70
        if (empty($record->count())) {
71
            return null;
72
        }
73
        $record->load();
74
        return $record;
75
    }
76
77
    public function createAccount(Interfaces\IUser $user, string $password): void
78
    {
79
        $record = clone $this->usersRecord;
80
        $this->checkLogin($user->getAuthName());
81
        $record->login = $user->getAuthName();
82
        $record->groupId = $user->getGroup();
83
        $record->display = $user->getDisplayName();
84
        $record->save(true);
85
    }
86
87
    public function readAccounts(): array
88
    {
89
        $search = new Search(clone $this->usersRecord);
90
        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_auth\Interfac...ccounts::readAccounts() of kalanis\kw_auth\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...
91
    }
92
93
    public function updateAccount(Interfaces\IUser $user): void
94
    {
95
        $this->checkLogin($user->getAuthName(), $user->getAuthId());
96
        $record = clone $this->usersRecord;
97
        $record->id = $user->getAuthId();
98
        $record->load();
99
        $record->login = $user->getAuthName();
100
        $record->groupId = $user->getGroup();
101
        $record->display = $user->getDisplayName();
102
        $record->save();
103
    }
104
105
    public function updatePassword(string $userName, string $passWord): void
106
    {
107
        $record = clone $this->usersRecord;
108
        $record->login = $userName;
109
        $record->load();
110
        $record->pass = $this->passMode->hash($passWord);
111
        $record->save();
112
    }
113
114
    public function deleteAccount(string $userName): void
115
    {
116
        $record = clone $this->usersRecord;
117
        $record->login = $userName;
118
        $record->delete();
119
    }
120
121
    public function createGroup(Interfaces\IGroup $group): void
122
    {
123
        $record = clone $this->groupsRecord;
124
        $record->name = $group->getGroupName();
125
        $record->desc = $group->getGroupDesc();
126
        $record->authorId = $group->getGroupAuthorId();
127
        $record->save(true);
128
    }
129
130
    public function getGroupDataOnly(int $groupId): ?Interfaces\IGroup
131
    {
132
        $record = clone $this->groupsRecord;
133
        $record->id = $groupId;
134
        if (empty($record->count())) {
135
            return null;
136
        }
137
        $record->load();
138
        return $record;
139
    }
140
141
    public function readGroup(): array
142
    {
143
        $search = new Search(clone $this->groupsRecord);
144
        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_auth\Interfac...cessGroups::readGroup() of kalanis\kw_auth\Interfaces\IGroup[].

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...
145
    }
146
147
    public function updateGroup(Interfaces\IGroup $group): void
148
    {
149
        $record = clone $this->groupsRecord;
150
        $record->id = $group->getGroupId();
151
        $record->load();
152
        $record->name = $group->getGroupName();
153
        $record->desc = $group->getGroupDesc();
154
        $record->save();
155
    }
156
157
    public function deleteGroup(int $groupId): void
158
    {
159
        $users = clone $this->usersRecord;
160
        $users->groupId = $groupId;
161
        if (0 >= $users->count()) {
162
            // not empty group
163
            return;
164
        }
165
        $record = clone $this->groupsRecord;
166
        $record->id = $groupId;
167
        $record->delete();
168
    }
169
170
    protected function checkLogin(string $login, int $id = 0): void
171
    {
172
        $user = clone $this->usersRecord;
173
        $user->login = $login;
174
        $amount = $user->count();
175
        if (1 > $amount) {
176
            return;
177
        }
178
        if (1 < $amount) {
179
            throw new AuthException('Too many users with that login!');
180
        }
181
        $user->load();
182
        if ($id && ($user->id != $id)) {
183
            throw new AuthException('Login already used.');
184
        }
185
    }
186
}
187