Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Base
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 10
756
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 setLocaleList
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __call
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 runImport
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
 clearDatabase
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
 importSettings
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 importTopics
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 importAuthorities
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 preImport
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 postImport
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * @package ClientDLDB
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsdldb\Importer;
9
10use BO\Zmsdldb\PDOAccess;
11use BO\Zmsdldb\FileAccess
12;
13
14/**
15 * @method \BO\Zmsdldb\Importer\MySQL\Base getServicesImporter(array $entitys, string $locale, int $options)
16 * @method \BO\Zmsdldb\Importer\MySQL\Base getLocationsImporter(array $entitys, string $locale, int $options)
17 * @method \BO\Zmsdldb\Importer\MySQL\Base getAuthoritiesImporter(array $entitys, string $locale, int $options)
18 * @method \BO\Zmsdldb\Importer\MySQL\Base getTopicsImporter(array $entitys, string $locale, int $options)
19 * @method \BO\Zmsdldb\Importer\MySQL\Base getSettingsImporter(array $entitys, string $locale, int $options)
20 */
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}