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

AbstractStorage::createEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 3
cts 6
cp 0.5
crap 2.5
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 InvalidArgumentException;
17
use WebHemi\Data\Query\QueryInterface;
18
use WebHemi\Data\Entity\EntityInterface;
19
use WebHemi\Data\Entity\EntitySet;
20
21
/**
22
 * Class AbstractStorage.
23
 * Suppose to hide Data Service Adapter and Data Entity instances from children Storage objects.
24
 */
25
abstract class AbstractStorage implements StorageInterface
26
{
27
    /**
28
     * @var QueryInterface
29
     */
30
    private $queryAdapter;
31
32
    /**
33
     * @var EntitySet
34
     */
35
    private $entitySetPrototype;
36
37
    /**
38
     * @var EntityInterface[]
39
     */
40
    private $entityPrototypes;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $initialized = false;
46
47
    /**
48
     * AbstractStorage constructor.
49
     *
50
     * @param QueryInterface $queryAdapter
51
     * @param EntitySet $entitySetPrototype
52
     * @param EntityInterface[] ...$entityPrototypes
53
     */
54 5
    public function __construct(
55
        QueryInterface $queryAdapter,
56
        EntitySet $entitySetPrototype,
57
        EntityInterface ...$entityPrototypes
58
    ) {
59 5
        $this->queryAdapter = $queryAdapter;
60 5
        $this->entitySetPrototype = $entitySetPrototype;
61
62 5
        foreach ($entityPrototypes as $entity) {
63 5
            $this->entityPrototypes[get_class($entity)] = $entity;
64
        }
65 5
    }
66
67
    /**
68
     * @return QueryInterface
69
     */
70 1
    public function getQueryAdapter() : QueryInterface
71
    {
72 1
        return $this->queryAdapter;
73
    }
74
75
    /**
76
     * Creates a clean instance of the Entity.
77
     *
78
     * @param string $entityClass
79
     * @throws InvalidArgumentException
80
     * @return EntityInterface
81
     */
82 2
    public function createEntity(string $entityClass) : EntityInterface
83
    {
84 2
        if (!isset($this->entityPrototypes[$entityClass])) {
85
            throw new InvalidArgumentException(
86
                sprintf('Entity class reference "%s" is not defined in this class.', $entityClass),
87
                1000
88
            );
89
        }
90
91 2
        return clone $this->entityPrototypes[$entityClass];
92
    }
93
94
    /**
95
     * Get an entity instance with data.
96
     *
97
     * @param string $entityClass
98
     * @param array  $data
99
     * @throws InvalidArgumentException
100
     * @return null|EntityInterface
101
     */
102 1
    protected function getEntity(string $entityClass, array $data) : ? EntityInterface
103
    {
104 1
        if (!empty($data)) {
105 1
            $entity = $this->createEntity($entityClass);
106 1
            $entity->fromArray($data);
107 1
            return $entity;
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * Creates an empty entity set.
115
     *
116
     * @return EntitySet
117
     */
118 1
    public function createEntitySet() : EntitySet
119
    {
120 1
        return clone $this->entitySetPrototype;
121
    }
122
123
    /**
124
     * Creates and fills and EntitySet
125
     *
126
     * @param string $entityClass
127
     * @param array|null $data
128
     * @throws InvalidArgumentException
129
     * @return EntitySet
130
     */
131 1
    protected function getEntitySet(string $entityClass, ? array $data) : EntitySet
132
    {
133 1
        $entitySet = $this->createEntitySet();
134
135 1
        if (is_null($data)) {
0 ignored issues
show
introduced by
The condition is_null($data) is always false.
Loading history...
136
            $data = [];
137
        }
138
139 1
        foreach ($data as $row) {
140 1
            $entity = $this->getEntity($entityClass, $row);
141
142 1
            if (!empty($entity)) {
143 1
                $entitySet[] = $entity;
144
            }
145
        }
146
147 1
        return $entitySet;
148
    }
149
150
    /**
151
     * Checks and corrects values to stay within the limits.
152
     *
153
     * @param int $limit
154
     * @param int $offset
155
     */
156 1
    protected function normalizeLimitAndOffset(int&$limit, int&$offset) : void
157
    {
158 1
        $limit = min(QueryInterface::MAX_ROW_LIMIT, abs($limit));
159 1
        $offset = abs($offset);
160 1
    }
161
}
162