Passed
Push — master ( 7ed858...861abc )
by Gabor
02:38
created

UserStorage::getUserByCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 14
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Storage;
15
16
use WebHemi\Data\Entity\EntitySet;
17
use WebHemi\Data\Entity\UserEntity;
18
use WebHemi\Data\Entity\UserGroupEntity;
19
use WebHemi\Data\Entity\UserMetaEntity;
20
use WebHemi\Data\Query\QueryInterface;
21
use WebHemi\StringLib;
22
23
/**
24
 * Class UserStorage.
25
 */
26
class UserStorage extends AbstractStorage
27
{
28
    /**
29
     * Returns a set of users.
30
     *
31
     * @param int $limit
32
     * @param int $offset
33
     * @return EntitySet
34
     */
35
    public function getUserList(
36
        int $limit = QueryInterface::MAX_ROW_LIMIT,
37
        int $offset = 0
38
    ) : EntitySet {
39
        $this->normalizeLimitAndOffset($limit, $offset);
40
41
        $data = $this->getQueryAdapter()->fetchData(
42
            'getUserList',
43
            [
44
                ':limit' => $limit,
45
                ':offset' => $offset
46
            ]
47
        );
48
49
        return $this->getEntitySet(UserEntity::class, $data);
50
    }
51
52
    /**
53
     * Returns user information identified by (unique) ID.
54
     *
55
     * @param  int $identifier
56
     * @return null|UserEntity
57
     */
58
    public function getUserById(int $identifier) : ? UserEntity
59
    {
60
        $data = $this->getQueryAdapter()->fetchData(
61
            'getUserById',
62
            [
63
                ':idUser' => $identifier
64
            ]
65
        );
66
67
        /** @var null|UserEntity $entity */
68
        $entity = $this->getEntity(UserEntity::class, $data[0] ?? []);
69
70
        return $entity;
71
    }
72
73
    /**
74
     * Returns user information by user name.
75
     *
76
     * @param  string $username
77
     * @return null|UserEntity
78
     */
79
    public function getUserByUserName(string $username) : ? UserEntity
80
    {
81
        $data = $this->getQueryAdapter()->fetchData(
82
            'getUserByUsername',
83
            [
84
                ':username' => $username
85
            ]
86
        );
87
88
        /** @var null|UserEntity $entity */
89
        $entity = $this->getEntity(UserEntity::class, $data[0] ?? []);
90
91
        return $entity;
92
    }
93
94
    /**
95
     * Returns user information by email.
96
     *
97
     * @param  string $email
98
     * @return null|UserEntity
99
     */
100
    public function getUserByEmail(string $email) : ? UserEntity
101
    {
102
        $data = $this->getQueryAdapter()->fetchData(
103
            'getUserByEmail',
104
            [
105
                ':email' => $email
106
            ]
107
        );
108
109
        /** @var null|UserEntity $entity */
110
        $entity = $this->getEntity(UserEntity::class, $data[0] ?? []);
111
112
        return $entity;
113
    }
114
115
    /**
116
     * Return a user information by credentials.
117
     *
118
     * @param  string $username
119
     * @param  string $password
120
     * @return null|UserEntity
121
     */
122
    public function getUserByCredentials(string $username, string $password) : ? UserEntity
123
    {
124
        $data = $this->getQueryAdapter()->fetchData(
125
            'getUserByCredentials',
126
            [
127
                ':username' => $username,
128
                ':password' => $password
129
            ]
130
        );
131
132
        /** @var null|UserEntity $entity */
133
        $entity = $this->getEntity(UserEntity::class, $data[0] ?? []);
134
135
        return $entity;
136
    }
137
138
    /**
139
     * Returns a user meta list identified by user ID.
140
     *
141
     * @param int $identifier
142
     * @param int $limit
143
     * @param int $offset
144
     * @return EntitySet
145
     */
146
    public function getUserMetaListByUser(
147
        int $identifier,
148
        int $limit = QueryInterface::MAX_ROW_LIMIT,
149
        int $offset = 0
150
    ) : EntitySet {
151
        $this->normalizeLimitAndOffset($limit, $offset);
152
153
        $data = $this->getQueryAdapter()->fetchData(
154
            'getUserMetaListByUser',
155
            [
156
                ':userId' => $identifier,
157
                ':limit' => $limit,
158
                ':offset' => $offset
159
            ]
160
        );
161
162
        return $this->getEntitySet(UserMetaEntity::class, $data);
163
    }
164
165
    /**
166
     * Returns a parsed/simplified form of the user meta list.
167
     *
168
     * @param int $identifier
169
     * @param int $limit
170
     * @param int $offset
171
     * @return array
172
     */
173
    public function getSimpleUserMetaListByUser(
174
        int $identifier,
175
        int $limit = QueryInterface::MAX_ROW_LIMIT,
176
        int $offset = 0
177
    ) : array {
178
        $metaInfo = [];
179
        $entitySet = $this->getUserMetaListByUser($identifier, $limit, $offset);
180
181
        foreach ($entitySet as $userMetaEntity) {
182
            $data = $this->processMetaData($userMetaEntity);
183
            $metaInfo[$data['key']] = $data['value'];
184
        }
185
186
        return $metaInfo;
187
    }
188
189
    /**
190
     * Processes a user meta information.
191
     *
192
     * @param UserMetaEntity $userMetaEntity
193
     * @return array
194
     */
195
    private function processMetaData(UserMetaEntity $userMetaEntity) : array
196
    {
197
        $key = $userMetaEntity->getMetaKey() ?? '';
198
        $value = $userMetaEntity->getMetaData() ?? '';
199
200
        if ($key == 'avatar' && strpos($value, 'gravatar://') === 0) {
201
            $value = str_replace('gravatar://', '', $value);
202
            $value = 'http://www.gravatar.com/avatar/'.md5(strtolower($value)).'?s=256&r=g';
203
        }
204
205
        $jsonDataKeys = ['workplaces', 'instant_messengers', 'phone_numbers', 'social_networks', 'websites'];
206
207
        if (in_array($key, $jsonDataKeys) && !empty($value)) {
208
            $value = json_decode($value, true);
209
        }
210
211
        $data = [
212
            'key' => lcfirst(StringLib::convertUnderscoreToCamelCase($key)),
213
            'value' => $value
214
        ];
215
216
        return $data;
217
    }
218
219
    /**
220
     * Returns a set of user groups.
221
     *
222
     * @param int $limit
223
     * @param int $offset
224
     * @return EntitySet
225
     */
226
    public function getUserGroupList(
227
        int $limit = QueryInterface::MAX_ROW_LIMIT,
228
        int $offset = 0
229
    ) : EntitySet {
230
        $this->normalizeLimitAndOffset($limit, $offset);
231
232
        $data = $this->getQueryAdapter()->fetchData(
233
            'getUserGroupList',
234
            [
235
                ':limit' => $limit,
236
                ':offset' => $offset
237
            ]
238
        );
239
240
        return $this->getEntitySet(UserGroupEntity::class, $data);
241
    }
242
243
    /**
244
     * Returns a set of user groups.
245
     *
246
     * @param int $identifier
247
     * @param int $limit
248
     * @param int $offset
249
     * @return EntitySet
250
     */
251 1
    public function getUserGroupListByUser(
252
        int $identifier,
253
        int $limit = QueryInterface::MAX_ROW_LIMIT,
254
        int $offset = 0
255
    ) : EntitySet {
256 1
        $this->normalizeLimitAndOffset($limit, $offset);
257
258 1
        $data = $this->getQueryAdapter()->fetchData(
259 1
            'getUserGroupListByUser',
260
            [
261 1
                ':userId' => $identifier,
262 1
                ':limit' => $limit,
263 1
                ':offset' => $offset
264
            ]
265
        );
266
267 1
        return $this->getEntitySet(UserGroupEntity::class, $data);
268
    }
269
270
    /**
271
     * Returns user group information identified by (unique) ID.
272
     *
273
     * @param  int $identifier
274
     * @return null|UserGroupEntity
275
     */
276
    public function getUserGroupById(int $identifier) : ? UserGroupEntity
277
    {
278
        $data = $this->getQueryAdapter()->fetchData(
279
            'getUserGroupById',
280
            [
281
                ':idUserGroup' => $identifier
282
            ]
283
        );
284
285
        /** @var null|UserGroupEntity $entity */
286
        $entity = $this->getEntity(UserGroupEntity::class, $data[0] ?? []);
287
288
        return $entity;
289
    }
290
291
    /**
292
     * Returns a user group information by name.
293
     *
294
     * @param  string $name
295
     * @return null|UserGroupEntity
296
     */
297
    public function getUserGroupByName(string $name) : ? UserGroupEntity
298
    {
299
        $data = $this->getQueryAdapter()->fetchData(
300
            'getUserGroupByName',
301
            [
302
                ':name' => $name
303
            ]
304
        );
305
306
        /** @var null|UserGroupEntity $entity */
307
        $entity = $this->getEntity(UserGroupEntity::class, $data[0] ?? []);
308
309
        return $entity;
310
    }
311
}
312