Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
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 / 86
0.00% covered (danger)
0.00%
0 / 10
702
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 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 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 $fileAccess;
27
28    protected $localeList = [
29        'de',
30        'en'
31    ];
32
33    protected $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                /** @psalm-suppress UnsafeInstantiation */
68                $instance = new $ImporterClass(...$args);
69                if (!$instance instanceof \BO\Zmsdldb\Importer\MySQL\Base) {
70                    throw new \InvalidArgumentException(
71                        $ImporterClass . ' must extend \\BO\\Zmsdldb\\Importer\\MySQL\\Base'
72                    );
73                }
74                return $instance;
75            }
76            throw new \BadMethodCallException('Method ' . $method . ' not found!');
77        } catch (\Exception $e) {
78            throw $e;
79        }
80    }
81
82    /**
83     *
84     * @return self
85     */
86    public function runImport()
87    {
88        try {
89            $this->preImport();
90
91            foreach ($this->importTypes as $type => $accessor) {
92                if (method_exists($this, 'import' . $type)) {
93                    call_user_func([$this, 'import' . $type]);
94                    continue;
95                }
96                foreach ($this->localeList as $locale) {
97                    $entitys = call_user_func_array([$this->fileAccess, 'from' . $accessor], [$locale])->getData();
98                    $importer = $this->__call(
99                        'get' . $type . 'Importer',
100                        [$entitys, $locale, $this->getOptions()]
101                    );
102                    $importer->preImport();
103                    $importer->runImport();
104                    $importer->postImport();
105                    #$importer->clearEntity();
106                }
107            }
108
109            $this->postImport();
110            return $this;
111        } catch (\Exception $e) {
112            throw $e;
113        }
114    }
115
116    /**
117     * @psalm-api
118     *
119     * @return void
120     */
121    public function clearDatabase()
122    {
123        try {
124            if (
125                $this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_REFERENCES_TABLES) ||
126                $this->checkOptionFlag(static::OPTION_CLEAR_ENTITIY_TABLE)
127            ) {
128                /*
129                $types = [
130                    'Services',
131                    'Locations',
132                    'Authorities',
133                    'Topics',
134                    'Settings'
135                ];
136                */
137                $tablesToClear = [];
138
139                foreach (array_keys($this->importTypes) as $type) {
140                    $importer = $this->__call(
141                        'get' . $type . 'Importer',
142                        [
143                            ['data' => [], 'hash' => ''],
144                            'de',
145                            $this->getOptions()
146                        ]
147                    );
148                    $entity = $importer->createEntity(['meta' => ['locale' => 'de']], false);
149                    $tablesToClear[$entity::getTableName()] = 1;
150
151                    foreach (array_values($entity->getReferenceMapping(true)) as $data) {
152                        $tablesToClear[call_user_func($data['class'] . '::getTableName')] = 1;
153                    }
154                }
155
156                foreach (array_flip($tablesToClear) as $table) {
157                    $this->getPDOAccess()->exec("DELETE FROM " . $table . " WHERE 1=1");
158                }
159            }
160        } catch (\Exception $e) {
161            throw $e;
162        }
163    }
164
165    /**
166     *
167     * @return self
168     * @psalm-api
169     */
170    protected function importSettings()
171    {
172        try {
173            $importer = $this->getSettingsImporter(
174                $this->fileAccess->fromSetting('de')->getData(),
175                'de',
176                $this->getOptions()
177            );
178            $importer->preImport();
179            $importer->runImport();
180            $importer->postImport();
181        } catch (\Exception $e) {
182            throw $e;
183        }
184    }
185
186    /**
187     *
188     * @return self
189     * @psalm-api
190     */
191    protected function importTopics()
192    {
193        try {
194            $importer = $this->getTopicsImporter(
195                $this->fileAccess->fromTopic('de')->getData(),
196                'de',
197                $this->getOptions()
198            );
199            $importer->preImport();
200            $importer->runImport();
201            $importer->postImport();
202            #$importer->clearEntity();
203        } catch (\Exception $e) {
204            throw $e;
205        }
206    }
207
208    /**
209     *
210     * @return self
211     * @psalm-api
212     */
213    protected function importAuthorities()
214    {
215        try {
216            $importer = $this->getAuthoritiesImporter(
217                $this->fileAccess->fromAuthority('de')->getData(),
218                'de',
219                $this->getOptions()
220            );
221            $importer->preImport();
222            $importer->runImport();
223            $importer->postImport();
224            #$importer->clearEntity();
225        } catch (\Exception $e) {
226            throw $e;
227        }
228    }
229
230    /**
231     * @return void
232     */
233    public function preImport()
234    {
235    }
236
237    /**
238     * @return void
239     */
240    public function postImport()
241    {
242    }
243}