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