Passed
Push — master ( a157fa...3bdbe0 )
by Gabor
02:36
created

AbstractStorage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 26.47%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 135
ccs 9
cts 34
cp 0.2647
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createEntity() 0 10 2
A __construct() 0 10 2
A createEntitySet() 0 3 1
A getEntity() 0 9 2
A getQueryAdapter() 0 3 1
A normalizeLimitAndOffset() 0 4 1
A getEntitySet() 0 17 4
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 3
    public function __construct(
55
        QueryInterface $queryAdapter,
56
        EntitySet $entitySetPrototype,
57
        EntityInterface ...$entityPrototypes
58
    ) {
59 3
        $this->queryAdapter = $queryAdapter;
60 3
        $this->entitySetPrototype = $entitySetPrototype;
61
62 3
        foreach ($entityPrototypes as $entity) {
63 3
            $this->entityPrototypes[get_class($entity)] = $entity;
64
        }
65 3
    }
66
67
    /**
68
     * @return QueryInterface
69
     */
70
    public function getQueryAdapter() : QueryInterface
71
    {
72
        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 1
    public function createEntity(string $entityClass) : EntityInterface
83
    {
84 1
        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 1
        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
    protected function getEntity(string $entityClass, array $data) : ? EntityInterface
103
    {
104
        if (!empty($data)) {
105
            $entity = $this->createEntity($entityClass);
106
            $entity->fromArray($data);
107
            return $entity;
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * Creates an empty entity set.
115
     *
116
     * @return EntitySet
117
     */
118
    public function createEntitySet() : EntitySet
119
    {
120
        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
    protected function getEntitySet(string $entityClass, ? array $data) : EntitySet
132
    {
133
        $entitySet = $this->createEntitySet();
134
135
        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
        foreach ($data as $row) {
140
            $entity = $this->getEntity($entityClass, $row);
141
142
            if (!empty($entity)) {
143
                $entitySet[] = $entity;
144
            }
145
        }
146
147
        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
    protected function normalizeLimitAndOffset(int&$limit, int&$offset) : void
157
    {
158
        $limit = min(QueryInterface::MAX_ROW_LIMIT, abs($limit));
159
        $offset = abs($offset);
160
    }
161
}
162