Lines 0.00% 0 / 118
Methods 0.00% 0 / 32
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 0.00% 0 / 14 0.00% 0 / 1 20
 getPDOAccess 0.00% 0 / 1 0.00% 0 / 1 2
 getCurrentEntitys 0.00% 0 / 1 0.00% 0 / 1 2
 getIterator 0.00% 0 / 2 0.00% 0 / 1 6
 removeEntityFromCurrentList 0.00% 0 / 1 0.00% 0 / 1 2
 setCurrentEntitys 0.00% 0 / 19 0.00% 0 / 1 30
 createMetaObject 0.00% 0 / 14 0.00% 0 / 1 12
 getMetaObject 0.00% 0 / 4 0.00% 0 / 1 6
 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
 getEntityClass 0.00% 0 / 3 0.00% 0 / 1 6
 createEntity 0.00% 0 / 5 0.00% 0 / 1 6
 clearEntity 0.00% 0 / 10 0.00% 0 / 1 42
 preImport 0.00% 0 / 1 0.00% 0 / 1 2
 postImport 0.00% 0 / 1 0.00% 0 / 1 2
 runImport n/a 0 / 0 n/a 0 / 0 0
 [BO\Zmsdldb\Importer\PDOTrait] setPDOAccess 0.00% 0 / 2 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\PDOTrait] getPDOAccess 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\PDOTrait] query 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\PDOTrait] exec 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\PDOTrait] prepare 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\PDOTrait] beginTransaction 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\PDOTrait] commit 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\PDOTrait] rollBack 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\PDOTrait] inTransaction 0.00% 0 / 3 0.00% 0 / 1 6
 [BO\Zmsdldb\Importer\OptionsTrait] checkOptionFlag 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\OptionsTrait] setOptions 0.00% 0 / 1 0.00% 0 / 1 2
 [BO\Zmsdldb\Importer\OptionsTrait] getOptions 0.00% 0 / 1 0.00% 0 / 1 2
12abstract class Base implements Options
13{
14    use PDOTrait;
15    use OptionsTrait;
16
17    /** @var class-string<EntityBase>|null */
18    protected ?string $entityClass = null;
19    protected array $importData = [];
20    protected string $hash = '';
21    protected string $locale = 'de';
22    protected ?MetaEntity $metaObject = null;
23    /** @var array<int, EntityBase> */
24    protected array $entitysToDelete = [];
25    protected bool $getCurrentEntitys = true;
26
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    }
49
50    public function getPDOAccess(): PDOAccess
51    {
52        return $this->pdoAccess;
53    }
54
55    /**
56     * @return array<int, EntityBase>
57     */
58    public function getCurrentEntitys(): array
59    {
60        return $this->entitysToDelete;
61    }
62
63    public function getIterator(): iterable
64    {
65        foreach ($this->importData as $item) {
66            yield $item;
67        }
68    }
69
70    public function removeEntityFromCurrentList(int $entityId): void
71    {
72        unset($this->entitysToDelete[$entityId]);
73    }
74
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    }
107
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    }
128
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    }
137
138    public function saveMetaObject(): self
139    {
140        $this->getMetaObject()->save();
141        return $this;
142    }
143
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    }
153
154    public function setImportData(array $importData = []): self
155    {
156        $this->importData = $importData;
157        return $this;
158    }
159
160    public function setLocale(string $locale): self
161    {
162        $this->locale = $locale;
163        return $this;
164    }
165
166    public function getLocale(): string
167    {
168        return $this->locale;
169    }
170
171    public function setImportHash(string $hash): self
172    {
173        $this->hash = $hash;
174        return $this;
175    }
176
177    public function getImportHash(): string
178    {
179        return $this->hash;
180    }
181
182    /**
183     * @return class-string<EntityBase>
184     */
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    }
192
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    }
203
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    }
224
225    /** @psalm-api */
226    public function preImport(): void
227    {
228    }
229
230    /** @psalm-api */
231    public function postImport(): void
232    {
233    }
234
235    /** @psalm-api */
236    abstract public function runImport(): bool;
237}

From BO\Zmsdldb\Importer\PDOTrait

7trait PDOTrait
8{
9    protected mixed $pdoAccess;
10
11    public function setPDOAccess(PDOAccess $pdoAccess): self
12    {
13        $this->pdoAccess = $pdoAccess;
14        return $this;
15    }
16
17    public function getPDOAccess(): PDOAccess
18    {
19        return $this->pdoAccess;
20    }
21
22    /**
23     * parameters see https://www.php.net/manual/de/pdo.query.php
24     * @psalm-api
25     */
26    public function query(mixed ...$args): mixed
27    {
28        try {
29            return $this->getPDOAccess()->query(...$args);
30        } catch (\Exception $e) {
31            throw $e;
32        }
33    }
34
35    /**
36     * parameters see https://www.php.net/manual/de/pdo.exec.php
37     * @psalm-api
38     */
39
40    public function exec(mixed ...$args): mixed
41    {
42        try {
43            return $this->getPDOAccess()->exec(...$args);
44        } catch (\Exception $e) {
45            throw $e;
46        }
47    }
48
49    /**
50     * parameters see https://www.php.net/manual/de/pdo.prepare.php
51     * @psalm-api
52     */
53    public function prepare(mixed ...$args): mixed
54    {
55        try {
56            return $this->getPDOAccess()->prepare(...$args);
57        } catch (\Exception $e) {
58            throw $e;
59        }
60    }
61
62    public function beginTransaction(): mixed
63    {
64        try {
65            return $this->getPDOAccess()->beginTransaction();
66        } catch (\Exception $e) {
67            throw $e;
68        }
69    }
70
71    public function commit(): mixed
72    {
73        try {
74            return $this->getPDOAccess()->commit();
75        } catch (\Exception $e) {
76            throw $e;
77        }
78    }
79
80    public function rollBack(): mixed
81    {
82        try {
83            return $this->getPDOAccess()->rollBack();
84        } catch (\Exception $e) {
85            throw $e;
86        }
87    }
88
89    /** @psalm-api */
90    public function inTransaction(): mixed
91    {
92        try {
93            return $this->getPDOAccess()->inTransaction();
94        } catch (\Exception $e) {
95            throw $e;
96        }
97    }
98}

From BO\Zmsdldb\Importer\OptionsTrait

5trait OptionsTrait
6{
7    protected int $options = 0;
8
9    protected function checkOptionFlag(int $optionFlag = 0): mixed
10    {
11        return $this->options & $optionFlag;
12    }
13
14    protected function setOptions(int $options = 0): void
15    {
16        $this->options = $options;
17    }
18
19    protected function getOptions(): int
20    {
21        return $this->options;
22    }
23}