Completed
Push — master ( 216ad2...692e73 )
by Gabor
02:41
created

AbstractDataStorage::initialized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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\DataStorage;
13
14
use WebHemi\Adapter\Data\DataAdapterInterface;
15
use WebHemi\DataEntity\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
     * UserMetaStorage 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 1
    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 1
        $this->defaultAdapter = clone $defaultAdapter;
45 1
        $this->entityPrototype = $entityPrototype;
46 1
        $this->init();
47 1
    }
48
49
    /**
50
     * Special initialization method. The constructor MUST call it.
51
     *
52
     * @return DataStorageInterface
53
     */
54 1
    public function init()
55
    {
56
        // They always walk in pair.
57 1
        if (!empty($this->dataGroup) && !empty($this->idKey)) {
58 1
            $this->defaultAdapter->setDataGroup($this->dataGroup);
59 1
            $this->defaultAdapter->setIdKey($this->idKey);
60
61 1
            $this->initialized = true;
62 1
        }
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * Checks if the storage is initialized.
69
     *
70
     * @return bool
71
     */
72 1
    final public function initialized()
73
    {
74 1
        return $this->initialized;
75
    }
76
77
    /**
78
     * Returns the DataAdapter instance.
79
     *
80
     * @return DataAdapterInterface
81
     */
82 1
    final public function getDataAdapter()
83
    {
84 1
        return $this->defaultAdapter;
85
    }
86
87
    /**
88
     * Creates an empty entity. Should be use by getters.
89
     *
90
     * @return DataEntityInterface
91
     */
92 1
    final public function createEntity()
93
    {
94 1
        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