Test Failed
Push — master ( 3ad9ef...cc0a1d )
by Gabor
05:00
created

AbstractStorage::createEntity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 16
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
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
    public function __construct(
55 33
        QueryInterface $queryAdapter,
56
        EntitySet $entitySetPrototype,
57
        EntityInterface ...$entityPrototypes
58 33
    ) {
59 33
        $this->queryAdapter = $queryAdapter;
60 33
        $this->entitySetPrototype = $entitySetPrototype;
61 33
62
        foreach ($entityPrototypes as $entity) {
63
            $this->entityPrototypes[get_class($entity)] = $entity;
64
        }
65
    }
66
67
    /**
68 33
     * @return QueryInterface
69
     */
70
    public function getQueryAdapter() : QueryInterface
71 33
    {
72 30
        return $this->queryAdapter;
73 30
    }
74
75 30
    /**
76
     * Creates a clean instance of the Entity.
77
     *
78 33
     * @param string $entityClass
79
     * @param array  $data
80
     * @throws InvalidArgumentException
81
     * @return null|EntityInterface
82
     */
83
    protected function getEntity(string $entityClass, array $data = []) : ? EntityInterface
84
    {
85
        if (!isset($this->entityPrototypes[$entityClass])) {
86 7
            throw new InvalidArgumentException(
87
                sprintf('Entity class reference "%s" is not defined in this class.', $entityClass),
88 7
                1000
89
            );
90
        }
91
92
        if (!empty($data)) {
93
            $entity = clone $this->entityPrototypes[$entityClass];
94
            $entity->fromArray($data);
95
            return $entity;
96 30
        }
97
98 30
        return null;
99
    }
100
101
    /**
102
     * Creates and fills and EntitySet
103
     *
104
     * @param string $entityClass
105
     * @param array $data
106 29
     * @throws InvalidArgumentException
107
     * @return EntitySet
108 29
     */
109
    protected function getEntitySet(string $entityClass, array $data) : EntitySet
110
    {
111
        $entitySet = clone $this->entitySetPrototype;
112
113
        foreach ($data as $row) {
114
            $entity = $this->getEntity($entityClass, $row);
115
116
            if (!empty($entity)) {
117 2
                $entitySet[] = $entity;
118
            }
119 2
        }
120 2
121
        return $entitySet;
122 2
    }
123 1
124 1
    /**
125 1
     * Checks and corrects values to stay within the limits.
126 1
     *
127 1
     * @param int $limit
128
     * @param int $offset
129 1
     */
130
    protected function normalizeLimitAndOffset(int&$limit, int&$offset) : void
131
    {
132
        $limit = min(QueryInterface::MAX_ROW_LIMIT, abs($limit));
133 2
        $offset = abs($offset);
134
    }
135
}
136