|
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\Coupler; |
|
13
|
|
|
|
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
use WebHemi\Adapter\Data\DataAdapterInterface; |
|
16
|
|
|
use WebHemi\Data\Entity\DataEntityInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class AbstractDataCoupler. |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractDataCoupler implements DataCouplerInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var DataAdapterInterface */ |
|
24
|
|
|
private $defaultAdapter; |
|
25
|
|
|
/** @var array<DataEntityInterface> */ |
|
26
|
|
|
protected $dataEntityPrototypes = []; |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $connectorIdKey; |
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
protected $connectorDataGroup; |
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
protected $dependentDataGroups; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* DataCouplerInterface constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param DataAdapterInterface $defaultAdapter |
|
38
|
|
|
* @param DataEntityInterface[] ...$dataEntityPrototypes |
|
39
|
|
|
*/ |
|
40
|
|
|
final public function __construct( |
|
41
|
|
|
DataAdapterInterface $defaultAdapter, |
|
42
|
|
|
DataEntityInterface ...$dataEntityPrototypes |
|
43
|
|
|
) { |
|
44
|
|
|
$this->defaultAdapter = $defaultAdapter; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($dataEntityPrototypes as $entityPrototype) { |
|
47
|
|
|
$this->dataEntityPrototypes[get_class($entityPrototype)] = $entityPrototype; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the DataAdapter instance. |
|
53
|
|
|
* |
|
54
|
|
|
* @return DataAdapterInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
final public function getDataAdapter() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->defaultAdapter; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gets all the entities those are depending from the given entity. |
|
63
|
|
|
* |
|
64
|
|
|
* @param DataEntityInterface $entity |
|
65
|
|
|
* @return array<DataEntityInterface> |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract public function getEntityDependencies(DataEntityInterface $entity); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns a new instance of the required entity. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $entityClassName |
|
73
|
|
|
* @throws RuntimeException |
|
74
|
|
|
* @return DataEntityInterface |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
protected function getNewEntityInstance($entityClassName) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
if (!isset($this->dataEntityPrototypes[$entityClassName])) { |
|
79
|
|
|
throw new RuntimeException(sprintf('Class %s is not defined in this Coupler.', $entityClassName)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return clone $this->dataEntityPrototypes[$entityClassName]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Gets raw depending entity data list for the given entity. |
|
87
|
|
|
* |
|
88
|
|
|
* @param DataEntityInterface $entity |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getEntityDataSet(DataEntityInterface $entity) |
|
92
|
|
|
{ |
|
93
|
|
|
$entityClassName = get_class($entity); |
|
94
|
|
|
$entityDataSet = []; |
|
95
|
|
|
$identifiers = []; |
|
96
|
|
|
|
|
97
|
|
|
$this->getDataAdapter()->setDataGroup($this->connectorDataGroup) |
|
98
|
|
|
->setIdKey($this->connectorIdKey); |
|
99
|
|
|
|
|
100
|
|
|
$dataList = $this->getDataAdapter()->getDataSet([ |
|
101
|
|
|
$this->dependentDataGroups[$entityClassName]['source_key'].' = ?' => $entity->getKeyData() |
|
102
|
|
|
]); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($dataList as $rowData) { |
|
105
|
|
|
$identifiers[] = $rowData[$this->dependentDataGroups[$entityClassName]['connector_key']]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!empty($identifiers)) { |
|
109
|
|
|
$this->getDataAdapter()->setDataGroup($this->dependentDataGroups[$entityClassName]['depending_group']) |
|
110
|
|
|
->setIdKey($this->dependentDataGroups[$entityClassName]['depending_id_key']); |
|
111
|
|
|
|
|
112
|
|
|
$entityDataSet = $this->getDataAdapter()->getDataSet([ |
|
113
|
|
|
$this->dependentDataGroups[$entityClassName]['depending_id_key'].' IN (?)' => $identifiers |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $entityDataSet; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.