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

ResourceStorage::getResourceById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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\ResourceEntity;
18
use WebHemi\Data\Query\QueryInterface;
19
20
/**
21
 * Class ResourceStorage.
22
 */
23
class ResourceStorage extends AbstractStorage
24
{
25
    /**
26
     * Returns a full set of resources data.
27
     *
28
     * @param int $limit
29
     * @param int $offset
30
     * @return null|EntitySet
31
     */
32
    public function getResourceList(
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
            'getResourceList',
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 ResourceEntity $entity */
50
            $entity = $this->createEntity(ResourceEntity::class, $row);
51
52
            if (!empty($entity)) {
53
                $entitySet[] = $entity;
54
            }
55
        }
56
57
        return $entitySet;
58
    }
59
60
    /**
61
     * Returns resource information identified by (unique) ID.
62
     *
63
     * @param  int $identifier
64
     * @return null|ResourceEntity
65
     */
66
    public function getResourceById(int $identifier) : ? ResourceEntity
67
    {
68
        $data = $this->getQueryAdapter()->fetchData('getResourceById', [':idResource' => $identifier]);
69
70
        /** @var null|ResourceEntity $entity */
71
        $entity = $this->createEntity(ResourceEntity::class, $data[0] ?? []);
72
73
        return $entity;
74
    }
75
76
    /**
77
     * Returns resource information by name.
78
     *
79
     * @param  string $name
80
     * @return null|ResourceEntity
81
     */
82
    public function getResourceByName(string $name) : ? ResourceEntity
83
    {
84
        $data = $this->getQueryAdapter()->fetchData('getResourceByName', [':name' => $name]);
85
86
        /** @var null|ResourceEntity $entity */
87
        $entity = $this->createEntity(ResourceEntity::class, $data[0] ?? []);
88
89
        return $entity;
90
    }
91
}
92