Passed
Push — master ( 64ab61...b96172 )
by Gabor
09:36
created

AbstractDataStorage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 0
loc 84
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A init() 0 12 3
A initialized() 0 4 1
A getDataAdapter() 0 4 1
A createEntity() 0 4 1
populateEntity() 0 1 ?
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Data\Storage;
13
14
use WebHemi\Adapter\Data\DataAdapterInterface;
15
use WebHemi\Data\Entity\DataEntityInterface;
16
17
/**
18
 * Class AbstractDataStorage.
19
 * Suppose to hide DataAdapter and DataEntity instances from children Storage objects.
20
 */
21
abstract class AbstractDataStorage implements DataStorageInterface
22
{
23
    /** @var DataAdapterInterface */
24
    private $defaultAdapter;
25
    /** @var DataEntityInterface */
26
    private $entityPrototype;
27
    /** @var string */
28
    protected $dataGroup;
29
    /** @var string */
30
    protected $idKey;
31
    /** @var bool */
32
    protected $initialized = false;
33
34
    /**
35
     * AbstractDataStorage constructor. The DataEntity SHOULD not be used directly unless it is required to represent
36
     * the same instance all the time.
37
     *
38
     * @param DataAdapterInterface $defaultAdapter
39
     * @param DataEntityInterface  $entityPrototype
40
     */
41 7
    final public function __construct(DataAdapterInterface $defaultAdapter, DataEntityInterface $entityPrototype)
42
    {
43
        // Every Storage object MUST have unique adapter instance to avoid override private properties like "dataGroup"
44 7
        $this->defaultAdapter = clone $defaultAdapter;
45 7
        $this->entityPrototype = $entityPrototype;
46 7
        $this->init();
47 7
    }
48
49
    /**
50
     * Special initialization method. The constructor MUST call it.
51
     *
52
     * @return DataStorageInterface
53
     */
54 7
    public function init()
55
    {
56
        // They always walk in pair.
57 7
        if (!empty($this->dataGroup) && !empty($this->idKey)) {
58 7
            $this->defaultAdapter->setDataGroup($this->dataGroup);
59 7
            $this->defaultAdapter->setIdKey($this->idKey);
60
61 7
            $this->initialized = true;
62 7
        }
63
64 7
        return $this;
65
    }
66
67
    /**
68
     * Checks if the storage is initialized.
69
     *
70
     * @return bool
71
     */
72 3
    final public function initialized()
73
    {
74 3
        return $this->initialized;
75
    }
76
77
    /**
78
     * Returns the DataAdapter instance.
79
     *
80
     * @return DataAdapterInterface
81
     */
82 7
    final public function getDataAdapter()
83
    {
84 7
        return $this->defaultAdapter;
85
    }
86
87
    /**
88
     * Creates an empty entity. Should be use by getters.
89
     *
90
     * @return DataEntityInterface
91
     */
92 7
    final public function createEntity()
93
    {
94 7
        return clone $this->entityPrototype;
95
    }
96
97
    /**
98
     * Populates an entity with storage data.
99
     *
100
     * @param DataEntityInterface $entity
101
     * @param array               $data
102
     */
103
    abstract protected function populateEntity(DataEntityInterface &$entity, array $data);
104
}
105