Test Failed
Push — master ( d5f951...5b0d59 )
by Gabor
08:43
created

UserStorage   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 4
dl 0
loc 317
rs 9.8
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B getUserList() 0 27 3
A getUserById() 0 11 2
A getUserByUserName() 0 11 2
A getUserByEmail() 0 11 2
A getUserByCredentials() 0 17 2
B getUserMetaListByUser() 0 29 3
A getSimpleUserMetaListByUser() 0 15 2
B processMetaData() 0 23 5
B getUserGroupList() 0 27 3
B getUserGroupListByUser() 0 29 3
A getUserGroupById() 0 11 2
A getUserGroupByName() 0 11 2
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
        $entitySet = $this->createEntitySet();
50
51
        foreach ($data as $row) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
52
            /** @var UserEntity $entity */
53
            $entity = $this->createEntity(UserEntity::class, $row);
54
55
            if (!empty($entity)) {
56
                $entitySet[] = $entity;
57
            }
58
        }
59
60
        return $entitySet;
61
    }
62
63
    /**
64
     * Returns user information identified by (unique) ID.
65
     *
66
     * @param  int $identifier
67
     * @return null|UserEntity
68
     */
69
    public function getUserById(int $identifier) : ? UserEntity
70
    {
71
        $data = $this->getQueryAdapter()->fetchData('getUserById', [':idUser' => $identifier]);
72
73
        if (isset($data[0])) {
74
            /** @var null|UserEntity $entity */
75
            $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
76
        }
77
78
        return $entity ?? null;
79
    }
80
81
    /**
82
     * Returns user information by user name.
83
     *
84
     * @param  string $username
85
     * @return null|UserEntity
86
     */
87
    public function getUserByUserName(string $username) : ? UserEntity
88
    {
89
        $data = $this->getQueryAdapter()->fetchData('getUserByUsername', [':username' => $username]);
90
91
        if (isset($data[0])) {
92
            /** @var null|UserEntity $entity */
93
            $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
94
        }
95
96
        return $entity ?? null;
97
    }
98
99
    /**
100
     * Returns user information by email.
101
     *
102
     * @param  string $email
103
     * @return null|UserEntity
104
     */
105
    public function getUserByEmail(string $email) : ? UserEntity
106
    {
107
        $data = $this->getQueryAdapter()->fetchData('getUserByEmail', [':email' => $email]);
108
109
        if (isset($data[0])) {
110
            /** @var null|UserEntity $entity */
111
            $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
112
        }
113
114
        return $entity ?? null;
115
    }
116
117
    /**
118
     * Return a user information by credentials.
119
     *
120
     * @param  string $username
121
     * @param  string $password
122
     * @return null|UserEntity
123
     */
124
    public function getUserByCredentials(string $username, string $password) : ? UserEntity
125
    {
126
        $data = $this->getQueryAdapter()->fetchData(
127
            'getUserByCredentials',
128
            [
129
                ':username' => $username,
130
                ':password' => $password
131
            ]
132
        );
133
134
        if (isset($data[0])) {
135
            /** @var null|UserEntity $entity */
136
            $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
137
        }
138
139
        return $entity ?? null;
140
    }
141
142
    /**
143
     * Returns a user meta list identified by user ID.
144
     *
145
     * @param int $identifier
146
     * @param int $limit
147
     * @param int $offset
148
     * @return EntitySet
149
     */
150
    public function getUserMetaListByUser(
151
        int $identifier,
152
        int $limit = QueryInterface::MAX_ROW_LIMIT,
153
        int $offset = 0
154
    ) : EntitySet {
155
        $this->normalizeLimitAndOffset($limit, $offset);
156
157
        $data = $this->getQueryAdapter()->fetchData(
158
            'getUserMetaListByUser',
159
            [
160
                ':userId' => $identifier,
161
                ':limit' => $limit,
162
                ':offset' => $offset
163
            ]
164
        );
165
166
        $entitySet = $this->createEntitySet();
167
168
        foreach ($data as $row) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
169
            /** @var UserEntity $entity */
170
            $entity = $this->createEntity(UserMetaEntity::class, $row);
171
172
            if (!empty($entity)) {
173
                $entitySet[] = $entity;
174
            }
175
        }
176
177
        return $entitySet;
178
    }
179
180
    /**
181
     * Returns a parsed/simplified form of the user meta list.
182
     *
183
     * @param int $identifier
184
     * @param int $limit
185
     * @param int $offset
186
     * @return array
187
     */
188
    public function getSimpleUserMetaListByUser(
189
        int $identifier,
190
        int $limit = QueryInterface::MAX_ROW_LIMIT,
191
        int $offset = 0
192
    ) : array {
193
        $metaInfo = [];
194
        $entitySet = $this->getUserMetaListByUser($identifier, $limit, $offset);
195
196
        foreach ($entitySet as $userMetaEntity) {
197
            $data = $this->processMetaData($userMetaEntity);
198
            $metaInfo[$data['key']] = $data['value'];
199
        }
200
201
        return $metaInfo;
202
    }
203
204
    /**
205
     * Processes a user meta information.
206
     *
207
     * @param UserMetaEntity $userMetaEntity
208
     * @return array
209
     */
210
    private function processMetaData(UserMetaEntity $userMetaEntity) : array
211
    {
212
        $key = $userMetaEntity->getMetaKey();
213
        $value = $userMetaEntity->getMetaData();
214
215
        if ($key == 'avatar' && strpos($value, 'gravatar://') === 0) {
216
            $value = str_replace('gravatar://', '', $value);
217
            $value = 'http://www.gravatar.com/avatar/'.md5(strtolower($value)).'?s=256&r=g';
218
        }
219
220
        $jsonDataKeys = ['workplaces', 'instant_messengers', 'phone_numbers', 'social_networks', 'websites'];
221
222
        if (in_array($key, $jsonDataKeys) && !empty($value)) {
223
            $value = json_decode($value, true);
224
        }
225
226
        $data = [
227
            'key' => lcfirst(StringLib::convertUnderscoreToCamelCase($key)),
228
            'value' => $value
229
        ];
230
231
        return $data;
232
    }
233
234
    /**
235
     * Returns a set of user groups.
236
     *
237
     * @param int $limit
238
     * @param int $offset
239
     * @return EntitySet
240
     */
241
    public function getUserGroupList(
242
        int $limit = QueryInterface::MAX_ROW_LIMIT,
243
        int $offset = 0
244
    ) : EntitySet {
245
        $this->normalizeLimitAndOffset($limit, $offset);
246
247
        $data = $this->getQueryAdapter()->fetchData(
248
            'getUserGroupList',
249
            [
250
                ':limit' => $limit,
251
                ':offset' => $offset
252
            ]
253
        );
254
255
        $entitySet = $this->createEntitySet();
256
257
        foreach ($data as $row) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
258
            /** @var UserGroupEntity $entity */
259
            $entity = $this->createEntity(UserGroupEntity::class, $row);
260
261
            if (!empty($entity)) {
262
                $entitySet[] = $entity;
263
            }
264
        }
265
266
        return $entitySet;
267
    }
268
269
    /**
270
     * Returns a set of user groups.
271
     *
272
     * @param int $identifier
273
     * @param int $limit
274
     * @param int $offset
275
     * @return EntitySet
276
     */
277
    public function getUserGroupListByUser(
278
        int $identifier,
279
        int $limit = QueryInterface::MAX_ROW_LIMIT,
280
        int $offset = 0
281
    ) : EntitySet {
282
        $this->normalizeLimitAndOffset($limit, $offset);
283
284
        $data = $this->getQueryAdapter()->fetchData(
285
            'getUserGroupListByUser',
286
            [
287
                ':userId' => $identifier,
288
                ':limit' => $limit,
289
                ':offset' => $offset
290
            ]
291
        );
292
293
        $entitySet = $this->createEntitySet();
294
295
        foreach ($data as $row) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
296
            /** @var UserGroupEntity $entity */
297
            $entity = $this->createEntity(UserGroupEntity::class, $row);
298
299
            if (!empty($entity)) {
300
                $entitySet[] = $entity;
301
            }
302
        }
303
304
        return $entitySet;
305
    }
306
307
    /**
308
     * Returns user group information identified by (unique) ID.
309
     *
310
     * @param  int $identifier
311
     * @return null|UserGroupEntity
312
     */
313
    public function getUserGroupById(int $identifier) : ? UserGroupEntity
314
    {
315
        $data = $this->getQueryAdapter()->fetchData('getUserGroupById', [':idUserGroup' => $identifier]);
316
317
        if (isset($data[0])) {
318
            /** @var null|UserGroupEntity $entity */
319
            $entity = $this->createEntity(UserGroupEntity::class, $data[0] ?? []);
320
        }
321
322
        return $entity ?? null;
323
    }
324
325
    /**
326
     * Returns a user group information by name.
327
     *
328
     * @param  string $name
329
     * @return null|UserGroupEntity
330
     */
331
    public function getUserGroupByName(string $name) : ? UserGroupEntity
332
    {
333
        $data = $this->getQueryAdapter()->fetchData('getUserGroupByName', [':name' => $name]);
334
335
        if (isset($data[0])) {
336
            /** @var null|UserGroupEntity $entity */
337
            $entity = $this->createEntity(UserGroupEntity::class, $data[0] ?? []);
338
        }
339
340
        return $entity ?? null;
341
    }
342
}
343