Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 74 |
|
0.00% |
0 / 21 |
CRAP | |
0.00% |
0 / 1 |
Base | |
0.00% |
0 / 74 |
|
0.00% |
0 / 21 |
1056 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
getPDOAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getImportData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIterator | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getCurrentEntitys | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
removeEntityFromCurrentList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setCurrentEntitys | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
createMetaObject | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getMetaObject | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
saveMetaObject | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
needsUpdate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
setImportData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setLocale | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getLocale | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setImportHash | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getImportHash | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createEntity | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
clearEntity | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
preImport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
postImport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
runImport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdldb\Importer\MySQL; |
4 | |
5 | use BO\Zmsdldb\PDOAccess; |
6 | use BO\Zmsdldb\Importer\OptionsTrait; |
7 | use BO\Zmsdldb\Importer\PDOTrait; |
8 | use BO\Zmsdldb\Importer\ItemNeedsUpdateTrait; |
9 | use BO\Zmsdldb\Importer\Options; |
10 | use BO\Zmsdldb\Importer\MySQL\Entity\Meta as MetaEntity |
11 | ; |
12 | |
13 | abstract class Base implements Options |
14 | { |
15 | use PDOTrait; |
16 | use OptionsTrait; |
17 | |
18 | protected $entityClass = null; |
19 | protected $importData = []; |
20 | protected $hash = null; |
21 | protected $locale = 'de'; |
22 | protected $metaObject = null; |
23 | protected $entitysToDelete = []; |
24 | protected $getCurrentEntitys = true; |
25 | |
26 | public function __construct(PDOAccess $mySqlAccess, array $importData = [], string $locale = 'de', $options = 0) |
27 | { |
28 | try { |
29 | $this->setPDOAccess($mySqlAccess); |
30 | $this->setImportData($importData['data']); |
31 | $this->setImportHash($importData['hash']); |
32 | $this->setLocale($locale); |
33 | |
34 | $this->setOptions($options); |
35 | $this->clearEntity(); |
36 | } catch (\Exception $e) { |
37 | throw $e; |
38 | } |
39 | } |
40 | |
41 | public function getPDOAccess(): PDOAccess |
42 | { |
43 | return $this->pdoAccess; |
44 | } |
45 | |
46 | public function getImportData(): array |
47 | { |
48 | return $this->importData; |
49 | } |
50 | |
51 | public function getIterator(): iterable |
52 | { |
53 | foreach ($this->importData as $item) { |
54 | yield $item; |
55 | } |
56 | } |
57 | |
58 | public function getCurrentEntitys(): array |
59 | { |
60 | return $this->entitysToDelete; |
61 | } |
62 | |
63 | public function removeEntityFromCurrentList(int $entityId) |
64 | { |
65 | unset($this->entitysToDelete[$entityId]); |
66 | } |
67 | |
68 | public function setCurrentEntitys() |
69 | { |
70 | try { |
71 | if (false === $this->getCurrentEntitys) { |
72 | return true; |
73 | } |
74 | $this->entitysToDelete = []; |
75 | $sql = "SELECT |
76 | m.object_id AS id, |
77 | e.data_json AS data_json |
78 | FROM meta AS m |
79 | JOIN " . $this->entityClass::getTableName() . " AS e ON e.id = m.object_id AND e.locale = ? |
80 | WHERE m.locale = ?"; |
81 | |
82 | |
83 | $stm = $this->getPDOAccess()->prepare($sql); |
84 | $stm->setFetchMode(\PDO::FETCH_OBJ); |
85 | $stm->execute([$this->getLocale(),$this->getLocale()]); |
86 | $entitys = $stm->fetchAll(); |
87 | foreach ($entitys as $entity) { |
88 | $entityObject = $this->createEntity(json_decode($entity->data_json, true)); |
89 | $this->entitysToDelete[$entity->id] = $entityObject; |
90 | } |
91 | /* |
92 | error_log( |
93 | print_r([ |
94 | 'current', |
95 | $this->entityClass::getTableName(), |
96 | $this->getLocale(), |
97 | count($this->entitysToDelete) |
98 | ],1 |
99 | )); |
100 | */ |
101 | } catch (\Exception $e) { |
102 | throw $e; |
103 | } |
104 | } |
105 | |
106 | public function createMetaObject() |
107 | { |
108 | try { |
109 | if (empty($this->metaObject)) { |
110 | $metaObject = new MetaEntity( |
111 | $this->getPDOAccess(), |
112 | [ |
113 | 'object_id' => 0, |
114 | 'locale' => $this->getLocale(), |
115 | 'hash' => $this->getImportHash(), |
116 | 'type' => call_user_func($this->entityClass . '::getTableName') |
117 | ] |
118 | ); |
119 | $this->metaObject = $metaObject; |
120 | } |
121 | } catch (\Exception $e) { |
122 | throw $e; |
123 | } |
124 | } |
125 | |
126 | public function getMetaObject(): MetaEntity |
127 | { |
128 | $this->createMetaObject(); |
129 | return $this->metaObject; |
130 | } |
131 | |
132 | public function saveMetaObject(): self |
133 | { |
134 | $this->getMetaObject()->save(); |
135 | return $this; |
136 | } |
137 | |
138 | public function needsUpdate() |
139 | { |
140 | $metaObject = $this->getMetaObject(); |
141 | $needsUpdate = $metaObject->itemNeedsUpdateAlt(); |
142 | if ($needsUpdate) { |
143 | $this->setCurrentEntitys(); |
144 | } |
145 | return $needsUpdate; |
146 | } |
147 | |
148 | public function setImportData(array $importData = []): self |
149 | { |
150 | $this->importData = $importData; |
151 | return $this; |
152 | } |
153 | |
154 | public function setLocale(string $locale): self |
155 | { |
156 | $this->locale = $locale; |
157 | return $this; |
158 | } |
159 | |
160 | public function getLocale(): string |
161 | { |
162 | return $this->locale; |
163 | } |
164 | |
165 | public function setImportHash(string $hash): self |
166 | { |
167 | $this->hash = $hash; |
168 | return $this; |
169 | } |
170 | |
171 | public function getImportHash(): string |
172 | { |
173 | return $this->hash; |
174 | } |
175 | |
176 | public function createEntity(array $data = array(), bool $setup = true) |
177 | { |
178 | if (null === $this->entityClass) { |
179 | throw new \InvalidArgumentException(__METHOD__ . " invalid entity class"); |
180 | } |
181 | return new $this->entityClass($this->getPDOAccess(), $data, $setup); |
182 | } |
183 | |
184 | final public function clearEntity() |
185 | { |
186 | try { |
187 | $entity = null; |
188 | if ($this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_TABLE)) { |
189 | $entity = ($entity ?? $this->createEntity(['meta' => ['locale' => $this->getLocale()]], false)); |
190 | $entity->clearEntity(); |
191 | } |
192 | if ($this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_REFERENCES_TABLES)) { |
193 | $entity = ($entity ?? $this->createEntity(['meta' => ['locale' => $this->getLocale()]], false)); |
194 | $entity->clearEntityReferences(); |
195 | } |
196 | } catch (\Exception $e) { |
197 | throw $e; |
198 | } |
199 | } |
200 | |
201 | public function preImport() |
202 | { |
203 | } |
204 | |
205 | public function postImport() |
206 | { |
207 | } |
208 | |
209 | abstract public function runImport(); |
210 | } |