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

PolicyStorage::getPolicyListByUserGroup()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 3
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\PolicyEntity;
18
use WebHemi\Data\Query\QueryInterface;
19
20
/**
21
 * Class PolicyStorage.
22
 */
23
class PolicyStorage extends AbstractStorage
24
{
25
    /**
26
     * Returns a full set of policy data.
27
     *
28
     * @param int $limit
29
     * @param int $offset
30
     * @return EntitySet
31
     */
32
    public function getPolicyList(
33
        int $limit = QueryInterface::MAX_ROW_LIMIT,
34
        int $offset = 0
35
    ) : EntitySet {
36
        $this->normalizeLimitAndOffset($limit, $offset);
37
38
        $data = $this->getQueryAdapter()->fetchData(
39
            'getPolicyList',
40
            [
41
                ':limit' => $limit,
42
                ':offset' => $offset
43
            ]
44
        );
45
46
        $entitySet = $this->createEntitySet();
47
48
        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...
49
            /** @var PolicyEntity $entity */
50
            $entity = $this->createEntity(PolicyEntity::class, $row);
51
52
            if (!empty($entity)) {
53
                $entitySet[] = $entity;
54
            }
55
        }
56
57
        return $entitySet;
58
    }
59
60
    /**
61
     * Returns a set of policy data identified by resource ID.
62
     *
63
     * @param  int $resourceId
64
     * @param int $limit
65
     * @param int $offset
66
     * @return EntitySet
67
     */
68
    public function getPolicyListByResource(
69
        int $resourceId,
70
        int $limit = QueryInterface::MAX_ROW_LIMIT,
71
        int $offset = 0
72
    ) : EntitySet {
73
        $this->normalizeLimitAndOffset($limit, $offset);
74
75
        $data = $this->getQueryAdapter()->fetchData(
76
            'getPolicyListByResource',
77
            [
78
                ':idResource' => $resourceId,
79
                ':limit' => $limit,
80
                ':offset' => $offset
81
            ]
82
        );
83
84
        $entitySet = $this->createEntitySet();
85
86
        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...
87
            /** @var PolicyEntity $entity */
88
            $entity = $this->createEntity(PolicyEntity::class, $row);
89
90
            if (!empty($entity)) {
91
                $entitySet[] = $entity;
92
            }
93
        }
94
95
        return $entitySet;
96
    }
97
98
    /**
99
     * Returns a set of policy data identified by application ID.
100
     *
101
     * @param int $applicationId
102
     * @param int $limit
103
     * @param int $offset
104
     * @return EntitySet
105
     */
106
    public function getPolicyListByApplication(
107
        int $applicationId,
108
        int $limit = QueryInterface::MAX_ROW_LIMIT,
109
        int $offset = 0
110
    ) : EntitySet {
111
        $this->normalizeLimitAndOffset($limit, $offset);
112
113
        $data = $this->getQueryAdapter()->fetchData(
114
            'getPolicyListByApplication',
115
            [
116
                ':idApplication' => $applicationId,
117
                ':limit' => $limit,
118
                ':offset' => $offset
119
            ]
120
        );
121
122
        $entitySet = $this->createEntitySet();
123
124
        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...
125
            /** @var PolicyEntity $entity */
126
            $entity = $this->createEntity(PolicyEntity::class, $row);
127
128
            if (!empty($entity)) {
129
                $entitySet[] = $entity;
130
            }
131
        }
132
133
        return $entitySet;
134
    }
135
136
    /**
137
     * Returns a set of policy data identified by user ID.
138
     *
139
     * @param int $userId
140
     * @param int $limit
141
     * @param int $offset
142
     * @return EntitySet
143
     */
144
    public function getPolicyListByUser(
145
        int $userId,
146
        int $limit = QueryInterface::MAX_ROW_LIMIT,
147
        int $offset = 0
148
    ) : EntitySet {
149
        $this->normalizeLimitAndOffset($limit, $offset);
150
151
        $data = $this->getQueryAdapter()->fetchData(
152
            'getPolicyListByUser',
153
            [
154
                ':idUser' => $userId,
155
                ':limit' => $limit,
156
                ':offset' => $offset
157
            ]
158
        );
159
160
        $entitySet = $this->createEntitySet();
161
162
        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...
163
            /** @var PolicyEntity $entity */
164
            $entity = $this->createEntity(PolicyEntity::class, $row);
165
166
            if (!empty($entity)) {
167
                $entitySet[] = $entity;
168
            }
169
        }
170
171
        return $entitySet;
172
    }
173
174
    /**
175
     * Returns a set of policy data identified by user group ID.
176
     *
177
     * @param int $userGroupId
178
     * @param int $limit
179
     * @param int $offset
180
     * @return EntitySet
181
     */
182
    public function getPolicyListByUserGroup(
183
        int $userGroupId,
184
        int $limit = QueryInterface::MAX_ROW_LIMIT,
185
        int $offset = 0
186
    ) : EntitySet {
187
        $this->normalizeLimitAndOffset($limit, $offset);
188
189
        $data = $this->getQueryAdapter()->fetchData(
190
            'getPolicyListByUserGroup',
191
            [
192
                ':idUserGroup' => $userGroupId,
193
                ':limit' => $limit,
194
                ':offset' => $offset
195
            ]
196
        );
197
198
        $entitySet = $this->createEntitySet();
199
200
        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...
201
            /** @var PolicyEntity $entity */
202
            $entity = $this->createEntity(PolicyEntity::class, $row);
203
204
            if (!empty($entity)) {
205
                $entitySet[] = $entity;
206
            }
207
        }
208
209
        return $entitySet;
210
    }
211
212
    /**
213
     * Returns a set of policy data identified by both resource and application IDs.
214
     *
215
     * @param int $resourceId
216
     * @param int $applicationId
217
     * @param int $limit
218
     * @param int $offset
219
     * @return EntitySet
220
     */
221
    public function getPolicyListByResourceAndApplication(
222
        int $resourceId,
223
        int $applicationId,
224
        int $limit = QueryInterface::MAX_ROW_LIMIT,
225
        int $offset = 0
226
    ) : EntitySet {
227
        $this->normalizeLimitAndOffset($limit, $offset);
228
229
        $data = $this->getQueryAdapter()->fetchData(
230
            'getPolicyListByResourceAndApplication',
231
            [
232
                ':idResource' => $resourceId,
233
                ':idApplication' => $applicationId,
234
                ':limit' => $limit,
235
                ':offset' => $offset
236
            ]
237
        );
238
239
        $entitySet = $this->createEntitySet();
240
241
        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...
242
            /** @var PolicyEntity $entity */
243
            $entity = $this->createEntity(PolicyEntity::class, $row);
244
245
            if (!empty($entity)) {
246
                $entitySet[] = $entity;
247
            }
248
        }
249
250
        return $entitySet;
251
    }
252
253
    /**
254
     * Returns policy data identified by (unique) ID.
255
     *
256
     * @param  int $identifier
257
     * @return null|PolicyEntity
258
     */
259
    public function getPolicyById(int $identifier) : ? PolicyEntity
260
    {
261
        $data = $this->getQueryAdapter()->fetchData('getPolicyById', [':idPolicy' => $identifier]);
262
263
        /** @var null|PolicyEntity $entity */
264
        $entity = $this->createEntity(PolicyEntity::class, $data[0] ?? []);
265
266
        return $entity;
267
    }
268
269
    /**
270
     * Returns policy data by name.
271
     *
272
     * @param  string $name
273
     * @return null|PolicyEntity
274
     */
275
    public function getPolicyByName(string $name) : ? PolicyEntity
276
    {
277
        $data = $this->getQueryAdapter()->fetchData('getPolicyByName', [':name' => $name]);
278
279
        /** @var null|PolicyEntity $entity */
280
        $entity = $this->createEntity(PolicyEntity::class, $data[0] ?? []);
281
282
        return $entity;
283
    }
284
}
285