Lines 0.00% 0 / 104
Methods 0.00% 0 / 21
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 [BO\Zmsdldb\Importer\MySQL\Topics] runImport 0.00% 0 / 13 0.00% 0 / 1 42
 [BO\Zmsdldb\Importer\MySQL\Base] __construct 0.00% 0 / 14 0.00% 0 / 1 20
 [BO\Zmsdldb\Importer\MySQL\Base] getPDOAccess 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] getCurrentEntitys 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] getIterator 0.00% 0 / 2 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\MySQL\Base] removeEntityFromCurrentList 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] setCurrentEntitys 0.00% 0 / 19 0.00% 0 / 1 30
 [BO\Zmsdldb\Importer\MySQL\Base] createMetaObject 0.00% 0 / 14 0.00% 0 / 1 12
 [BO\Zmsdldb\Importer\MySQL\Base] getMetaObject 0.00% 0 / 4 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\MySQL\Base] saveMetaObject 0.00% 0 / 2 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] needsUpdate 0.00% 0 / 5 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\MySQL\Base] setImportData 0.00% 0 / 2 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] setLocale 0.00% 0 / 2 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] getLocale 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] setImportHash 0.00% 0 / 2 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] getImportHash 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] getEntityClass 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\MySQL\Base] createEntity 0.00% 0 / 5 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\MySQL\Base] clearEntity 0.00% 0 / 10 0.00% 0 / 1 42
 [BO\Zmsdldb\Importer\MySQL\Base] preImport 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\MySQL\Base] postImport 0.00% 0 / 1 0.00% 0 / 1 2
8class Topics extends TopicsBase
9{
10    /** @var class-string<\BO\Zmsdldb\Importer\MySQL\Entity\Base>|null */
11    protected ?string $entityClass = \BO\Zmsdldb\Importer\SQLite\Entity\Topic::class;
12}

Inherited from BO\Zmsdldb\Importer\MySQL\Topics

12    public function runImport(): bool
13    {
14        try {
15            if ($this->needsUpdate()) {
16                foreach ($this->getIterator() as $topic) {
17                    if (!is_array($topic)) {
18                        continue;
19                    }
20                    $topic = $this->createEntity($topic);
21                    $this->removeEntityFromCurrentList((int) $topic->get('id'));
22                    $topic->save();
23                }
24            }
25            foreach ($this->getCurrentEntitys() as $entityToDelete) {
26                $entityToDelete->delete();
27            }
28            $this->saveMetaObject();
29        } catch (\Exception $e) {
30            throw $e;
31        }
32
33        return true;
34    }

Inherited from BO\Zmsdldb\Importer\MySQL\Base

27    public function __construct(PDOAccess $mySqlAccess, array $importData = [], string $locale = 'de', int $options = 0)
28    {
29        try {
30            $this->setPDOAccess($mySqlAccess);
31            $data = $importData['data'] ?? [];
32            $hash = $importData['hash'] ?? '';
33            if (!is_array($data)) {
34                throw new \InvalidArgumentException('Import data must be an array');
35            }
36            if (!is_string($hash)) {
37                throw new \InvalidArgumentException('Import hash must be a string');
38            }
39            $this->setImportData($data);
40            $this->setImportHash($hash);
41            $this->setLocale($locale);
42
43            $this->setOptions($options);
44            $this->clearEntity();
45        } catch (\Exception $e) {
46            throw $e;
47        }
48    }
50    public function getPDOAccess(): PDOAccess
51    {
52        return $this->pdoAccess;
53    }
58    public function getCurrentEntitys(): array
59    {
60        return $this->entitysToDelete;
61    }
63    public function getIterator(): iterable
64    {
65        foreach ($this->importData as $item) {
66            yield $item;
67        }
68    }
70    public function removeEntityFromCurrentList(int $entityId): void
71    {
72        unset($this->entitysToDelete[$entityId]);
73    }
75    public function setCurrentEntitys(): void
76    {
77        try {
78            if (false === $this->getCurrentEntitys) {
79                return;
80            }
81            $this->entitysToDelete = [];
82            $entityClass = $this->getEntityClass();
83            $sql = "SELECT 
84            m.object_id AS id, 
85            e.data_json AS data_json 
86            FROM meta AS m
87            JOIN " . $entityClass::getTableName() . " AS e ON e.id = m.object_id AND e.locale = ?
88            WHERE m.locale = ?";
89
90
91            $stm = $this->getPDOAccess()->prepare($sql);
92            $stm->setFetchMode(\PDO::FETCH_OBJ);
93            $stm->execute([$this->getLocale(),$this->getLocale()]);
94            $entitys = $stm->fetchAll();
95            foreach ($entitys as $entity) {
96                $decoded = json_decode($entity->data_json, true);
97                if (!is_array($decoded)) {
98                    continue;
99                }
100                $entityObject = $this->createEntity($decoded);
101                $this->entitysToDelete[(int) $entity->id] = $entityObject;
102            }
103        } catch (\Exception $e) {
104            throw $e;
105        }
106    }
108    public function createMetaObject(): void
109    {
110        try {
111            if (empty($this->metaObject)) {
112                $entityClass = $this->getEntityClass();
113                $metaObject = new MetaEntity(
114                    $this->getPDOAccess(),
115                    [
116                        'object_id' => 0,
117                        'locale' => $this->getLocale(),
118                        'hash' => $this->getImportHash(),
119                        'type' => $entityClass::getTableName()
120                    ]
121                );
122                $this->metaObject = $metaObject;
123            }
124        } catch (\Exception $e) {
125            throw $e;
126        }
127    }
129    public function getMetaObject(): MetaEntity
130    {
131        $this->createMetaObject();
132        if (null === $this->metaObject) {
133            throw new \RuntimeException('Failed to create meta object');
134        }
135        return $this->metaObject;
136    }
138    public function saveMetaObject(): self
139    {
140        $this->getMetaObject()->save();
141        return $this;
142    }
144    public function needsUpdate(): bool
145    {
146        $metaObject = $this->getMetaObject();
147        $needsUpdate = $metaObject->itemNeedsUpdateAlt();
148        if ($needsUpdate) {
149            $this->setCurrentEntitys();
150        }
151        return $needsUpdate;
152    }
154    public function setImportData(array $importData = []): self
155    {
156        $this->importData = $importData;
157        return $this;
158    }
160    public function setLocale(string $locale): self
161    {
162        $this->locale = $locale;
163        return $this;
164    }
166    public function getLocale(): string
167    {
168        return $this->locale;
169    }
171    public function setImportHash(string $hash): self
172    {
173        $this->hash = $hash;
174        return $this;
175    }
177    public function getImportHash(): string
178    {
179        return $this->hash;
180    }
185    protected function getEntityClass(): string
186    {
187        if (null === $this->entityClass) {
188            throw new \InvalidArgumentException(__METHOD__ . " invalid entity class");
189        }
190        return $this->entityClass;
191    }
193    public function createEntity(array $data = array(), bool $setup = true): EntityBase
194    {
195        $entityClass = $this->getEntityClass();
196        /** @psalm-suppress UnsafeInstantiation */
197        $entity = new $entityClass($this->getPDOAccess(), $data, $setup);
198        if (!$entity instanceof EntityBase) {
199            throw new \InvalidArgumentException($entityClass . ' must extend ' . EntityBase::class);
200        }
201        return $entity;
202    }
204    final public function clearEntity(): void
205    {
206        try {
207            if (
208                !$this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_TABLE) &&
209                !$this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_REFERENCES_TABLES)
210            ) {
211                return;
212            }
213            $entity = $this->createEntity(['meta' => ['locale' => $this->getLocale()]], false);
214            if ($this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_TABLE)) {
215                $entity->clearEntity();
216            }
217            if ($this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_REFERENCES_TABLES)) {
218                $entity->clearEntityReferences();
219            }
220        } catch (\Exception $e) {
221            throw $e;
222        }
223    }
226    public function preImport(): void
227    {
228    }
231    public function postImport(): void
232    {
233    }