Test Failed
Branch lab/data (a414ec)
by Gabor
07:41
created

UserStorage   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 4
dl 0
loc 305
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B getUserList() 0 27 3
A getUserById() 0 9 1
A getUserByUsername() 0 9 1
A getUserByEmail() 0 9 1
A getUserByCredentials() 0 15 1
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 9 1
A getUserGroupByName() 0 9 1
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
        /** @var null|UserEntity $entity */
74
        $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
75
76
        return $entity;
77
    }
78
79
    /**
80
     * Returns user information by user name.
81
     *
82
     * @param  string $username
83
     * @return null|UserEntity
84
     */
85
    public function getUserByUsername(string $username) : ? UserEntity
86
    {
87
        $data = $this->getQueryAdapter()->fetchData('getUserByUsername', [':username' => $username]);
88
89
        /** @var null|UserEntity $entity */
90
        $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
91
92
        return $entity;
93
    }
94
95
    /**
96
     * Returns user information by email.
97
     *
98
     * @param  string $email
99
     * @return null|UserEntity
100
     */
101
    public function getUserByEmail(string $email) : ? UserEntity
102
    {
103
        $data = $this->getQueryAdapter()->fetchData('getUserByEmail', [':email' => $email]);
104
105
        /** @var null|UserEntity $entity */
106
        $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
107
108
        return $entity;
109
    }
110
111
    /**
112
     * Return a user information by credentials.
113
     *
114
     * @param  string $username
115
     * @param  string $password
116
     * @return null|UserEntity
117
     */
118
    public function getUserByCredentials(string $username, string $password) : ? UserEntity
119
    {
120
        $data = $this->getQueryAdapter()->fetchData(
121
            'getUserByCredentials',
122
            [
123
                ':username' => $username,
124
                ':password' => $password
125
            ]
126
        );
127
128
        /** @var null|UserEntity $entity */
129
        $entity = $this->createEntity(UserEntity::class, $data[0] ?? []);
130
131
        return $entity;
132
    }
133
134
    /**
135
     * Returns a user meta list identified by user ID.
136
     *
137
     * @param int $identifier
138
     * @param int $limit
139
     * @param int $offset
140
     * @return EntitySet
141
     */
142
    public function getUserMetaListByUser(
143
        int $identifier,
144
        int $limit = QueryInterface::MAX_ROW_LIMIT,
145
        int $offset = 0
146
    ) : EntitySet {
147
        $this->normalizeLimitAndOffset($limit, $offset);
148
149
        $data = $this->getQueryAdapter()->fetchData(
150
            'getUserMetaListByUser',
151
            [
152
                ':userId' => $identifier,
153
                ':limit' => $limit,
154
                ':offset' => $offset
155
            ]
156
        );
157
158
        $entitySet = $this->createEntitySet();
159
160
        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...
161
            /** @var UserEntity $entity */
162
            $entity = $this->createEntity(UserMetaEntity::class, $row);
163
164
            if (!empty($entity)) {
165
                $entitySet[] = $entity;
166
            }
167
        }
168
169
        return $entitySet;
170
    }
171
172
    /**
173
     * Returns a parsed/simplified form of the user meta list.
174
     *
175
     * @param int $identifier
176
     * @param int $limit
177
     * @param int $offset
178
     * @return array
179
     */
180
    public function getSimpleUserMetaListByUser(
181
        int $identifier,
182
        int $limit = QueryInterface::MAX_ROW_LIMIT,
183
        int $offset = 0
184
    ) : array {
185
        $metaInfo = [];
186
        $entitySet = $this->getUserMetaListByUser($identifier, $limit, $offset);
187
188
        foreach ($entitySet as $userMetaEntity) {
189
            $data = $this->processMetaData($userMetaEntity);
190
            $metaInfo[$data['key']] = $data['value'];
191
        }
192
193
        return $metaInfo;
194
    }
195
196
    /**
197
     * Processes a user meta information.
198
     *
199
     * @param UserMetaEntity $userMetaEntity
200
     * @return array
201
     */
202
    private function processMetaData(UserMetaEntity $userMetaEntity) : array
203
    {
204
        $key = $userMetaEntity->getMetaKey();
205
        $value = $userMetaEntity->getMetaData();
206
207
        if ($key == 'avatar' && strpos($value, 'gravatar://') === 0) {
208
            $value = str_replace('gravatar://', '', $value);
209
            $value = 'http://www.gravatar.com/avatar/'.md5(strtolower($value)).'?s=256&r=g';
210
        }
211
212
        $jsonDataKeys = ['workplaces', 'instant_messengers', 'phone_numbers', 'social_networks', 'websites'];
213
214
        if (in_array($key, $jsonDataKeys) && !empty($value)) {
215
            $value = json_decode($value, true);
216
        }
217
218
        $data = [
219
            'key' => lcfirst(StringLib::convertUnderscoreToCamelCase($key)),
220
            'value' => $value
221
        ];
222
223
        return $data;
224
    }
225
226
    /**
227
     * Returns a set of user groups.
228
     *
229
     * @param int $limit
230
     * @param int $offset
231
     * @return EntitySet
232
     */
233
    public function getUserGroupList(
234
        int $limit = QueryInterface::MAX_ROW_LIMIT,
235
        int $offset = 0
236
    ) : EntitySet {
237
        $this->normalizeLimitAndOffset($limit, $offset);
238
239
        $data = $this->getQueryAdapter()->fetchData(
240
            'getUserGroupList',
241
            [
242
                ':limit' => $limit,
243
                ':offset' => $offset
244
            ]
245
        );
246
247
        $entitySet = $this->createEntitySet();
248
249
        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...
250
            /** @var UserGroupEntity $entity */
251
            $entity = $this->createEntity(UserGroupEntity::class, $row);
252
253
            if (!empty($entity)) {
254
                $entitySet[] = $entity;
255
            }
256
        }
257
258
        return $entitySet;
259
    }
260
261
    /**
262
     * Returns a set of user groups.
263
     *
264
     * @param int $identifier
265
     * @param int $limit
266
     * @param int $offset
267
     * @return EntitySet
268
     */
269
    public function getUserGroupListByUser(
270
        int $identifier,
271
        int $limit = QueryInterface::MAX_ROW_LIMIT,
272
        int $offset = 0
273
    ) : EntitySet {
274
        $this->normalizeLimitAndOffset($limit, $offset);
275
276
        $data = $this->getQueryAdapter()->fetchData(
277
            'getUserGroupListByUser',
278
            [
279
                ':userId' => $identifier,
280
                ':limit' => $limit,
281
                ':offset' => $offset
282
            ]
283
        );
284
285
        $entitySet = $this->createEntitySet();
286
287
        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...
288
            /** @var UserGroupEntity $entity */
289
            $entity = $this->createEntity(UserGroupEntity::class, $row);
290
291
            if (!empty($entity)) {
292
                $entitySet[] = $entity;
293
            }
294
        }
295
296
        return $entitySet;
297
    }
298
299
    /**
300
     * Returns user group information identified by (unique) ID.
301
     *
302
     * @param  int $identifier
303
     * @return null|UserGroupEntity
304
     */
305
    public function getUserGroupById(int $identifier) : ? UserGroupEntity
306
    {
307
        $data = $this->getQueryAdapter()->fetchData('getUserGroupById', [':idUserGroup' => $identifier]);
308
309
        /** @var null|UserGroupEntity $entity */
310
        $entity = $this->createEntity(UserGroupEntity::class, $data[0] ?? []);
311
312
        return $entity;
313
    }
314
315
    /**
316
     * Returns a user group information by name.
317
     *
318
     * @param  string $name
319
     * @return null|UserGroupEntity
320
     */
321
    public function getUserGroupByName(string $name) : ? UserGroupEntity
322
    {
323
        $data = $this->getQueryAdapter()->fetchData('getUserGroupByName', [':name' => $name]);
324
325
        /** @var null|UserGroupEntity $entity */
326
        $entity = $this->createEntity(UserGroupEntity::class, $data[0] ?? []);
327
328
        return $entity;
329
    }
330
}
331