Lines 0.00% 0 / 114
Methods 0.00% 0 / 22
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 0.00% 0 / 3 0.00% 0 / 1 2
 setLocaleList 0.00% 0 / 1 0.00% 0 / 1 2
 __call 0.00% 0 / 13 0.00% 0 / 1 30
 runImport 0.00% 0 / 18 0.00% 0 / 1 30
 clearDatabase 0.00% 0 / 20 0.00% 0 / 1 56
 importSettings 0.00% 0 / 10 0.00% 0 / 1 6
 importTopics 0.00% 0 / 10 0.00% 0 / 1 6
 importAuthorities 0.00% 0 / 10 0.00% 0 / 1 6
 preImport 0.00% 0 / 1 0.00% 0 / 1 2
 postImport 0.00% 0 / 1 0.00% 0 / 1 2
 [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
21abstract class Base implements Options
22{
23    use PDOTrait;
24    use OptionsTrait;
25
26    protected \BO\Zmsdldb\FileAccess $fileAccess;
27
28    protected array $localeList = [
29        'de',
30        'en'
31    ];
32
33    protected mixed $importTypes = [
34        'Services' => 'Service',
35        'Locations' => 'Location',
36        'Authorities' => 'Authority',
37        'Topics' => 'Topic',
38        'Settings' => 'Setting'
39    ];
40
41    public function __construct(PDOAccess $pdoAccess, FileAccess $fileAccess, int $options = 0)
42    {
43        $this->setPDOAccess($pdoAccess);
44        $this->fileAccess = $fileAccess;
45        $this->options = $options;
46    }
47
48    /**
49     * @psalm-api
50     */
51    public function setLocaleList(array $localeList = ['de', 'en']): void
52    {
53        $this->localeList = $localeList;
54    }
55
56    /**
57     * @param string $method
58     * @param array $args
59     * @return \BO\Zmsdldb\Importer\MySQL\Base
60     */
61    public function __call($method, $args = [])
62    {
63        try {
64            if (preg_match('/get(?P<importer>[A-Za-z]+)Importer/', $method, $matches)) {
65                $ImporterClass = static::class . '\\' . $matches['importer'];
66                array_unshift($args, $this->getPDOAccess());
67                if (
68                    !class_exists($ImporterClass)
69                    || !is_a($ImporterClass, \BO\Zmsdldb\Importer\MySQL\Base::class, true)
70                ) {
71                    throw new \InvalidArgumentException(
72                        $ImporterClass . ' must extend \\BO\\Zmsdldb\\Importer\\MySQL\\Base'
73                    );
74                }
75                /** @var class-string<\BO\Zmsdldb\Importer\MySQL\Base> $ImporterClass */
76                /** @psalm-suppress UnsafeInstantiation */
77                $instance = new $ImporterClass(...$args);
78                return $instance;
79            }
80            throw new \BadMethodCallException('Method ' . $method . ' not found!');
81        } catch (\Exception $e) {
82            throw $e;
83        }
84    }
85
86    /**
87     *
88     * @return self
89     */
90    public function runImport()
91    {
92        try {
93            $this->preImport();
94
95            foreach ($this->importTypes as $type => $accessor) {
96                if (method_exists($this, 'import' . $type)) {
97                    call_user_func([$this, 'import' . $type]);
98                    continue;
99                }
100                foreach ($this->localeList as $locale) {
101                    $entitys = call_user_func_array([$this->fileAccess, 'from' . $accessor], [$locale])->getData();
102                    $importer = $this->__call(
103                        'get' . $type . 'Importer',
104                        [$entitys, $locale, $this->getOptions()]
105                    );
106                    $importer->preImport();
107                    $importer->runImport();
108                    $importer->postImport();
109                    #$importer->clearEntity();
110                }
111            }
112
113            $this->postImport();
114            return $this;
115        } catch (\Exception $e) {
116            throw $e;
117        }
118    }
119
120    /**
121     * @psalm-api
122     *
123     * @return void
124     */
125    public function clearDatabase()
126    {
127        try {
128            if (
129                $this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_REFERENCES_TABLES) ||
130                $this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_TABLE)
131            ) {
132                /*
133                $types = [
134                    'Services',
135                    'Locations',
136                    'Authorities',
137                    'Topics',
138                    'Settings'
139                ];
140                */
141                $tablesToClear = [];
142
143                foreach (array_keys($this->importTypes) as $type) {
144                    $importer = $this->__call(
145                        'get' . $type . 'Importer',
146                        [
147                            ['data' => [], 'hash' => ''],
148                            'de',
149                            $this->getOptions()
150                        ]
151                    );
152                    $entity = $importer->createEntity(['meta' => ['locale' => 'de']], false);
153                    $tablesToClear[$entity::getTableName()] = 1;
154
155                    foreach (array_values($entity->getReferenceMapping(true)) as $data) {
156                        $tablesToClear[call_user_func($data['class'] . '::getTableName')] = 1;
157                    }
158                }
159
160                foreach (array_flip($tablesToClear) as $table) {
161                    $this->getPDOAccess()->exec("DELETE FROM " . $table . " WHERE 1=1");
162                }
163            }
164        } catch (\Exception $e) {
165            throw $e;
166        }
167    }
168
169    /**
170     *
171     * @return void
172     * @psalm-api
173     */
174    protected function importSettings()
175    {
176        try {
177            $importer = $this->getSettingsImporter(
178                $this->fileAccess->fromSetting()->getData(),
179                'de',
180                $this->getOptions()
181            );
182            $importer->preImport();
183            $importer->runImport();
184            $importer->postImport();
185        } catch (\Exception $e) {
186            throw $e;
187        }
188    }
189
190    /**
191     *
192     * @return void
193     * @psalm-api
194     */
195    protected function importTopics()
196    {
197        try {
198            $importer = $this->getTopicsImporter(
199                $this->fileAccess->fromTopic('de')->getData(),
200                'de',
201                $this->getOptions()
202            );
203            $importer->preImport();
204            $importer->runImport();
205            $importer->postImport();
206            #$importer->clearEntity();
207        } catch (\Exception $e) {
208            throw $e;
209        }
210    }
211
212    /**
213     *
214     * @return void
215     * @psalm-api
216     */
217    protected function importAuthorities()
218    {
219        try {
220            $importer = $this->getAuthoritiesImporter(
221                $this->fileAccess->fromAuthority('de')->getData(),
222                'de',
223                $this->getOptions()
224            );
225            $importer->preImport();
226            $importer->runImport();
227            $importer->postImport();
228            #$importer->clearEntity();
229        } catch (\Exception $e) {
230            throw $e;
231        }
232    }
233
234    /**
235     * @return void
236     */
237    public function preImport()
238    {
239    }
240
241    /**
242     * @return void
243     */
244    public function postImport()
245    {
246    }
247}

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}