Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.29% covered (warning)
81.29%
113 / 139
61.54% covered (warning)
61.54%
8 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Provider
81.29% covered (warning)
81.29%
113 / 139
61.54% covered (warning)
61.54%
8 / 13
56.67
0.00% covered (danger)
0.00%
0 / 1
 readEntity
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
5.01
 readEntityById
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 readSourceMapByIds
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
 readCollection
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 readListBySource
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 writeEntity
96.43% covered (success)
96.43%
27 / 28
0.00% covered (danger)
0.00%
0 / 1
6
 writeListBySource
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 writeImportList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 writeImportEntity
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
4
 writeDeleteEntity
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 writeDeleteListBySource
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 testSource
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 removeCache
16.67% covered (danger)
16.67%
2 / 12
0.00% covered (danger)
0.00%
0 / 1
26.83
1<?php
2
3namespace BO\Zmsbackend\Provider\Service;
4
5use BO\Zmsbackend\Application as App;
6use BO\Zmsentities\Provider as Entity;
7use BO\Zmsentities\Collection\ProviderList as Collection;
8
9class Provider extends \BO\Zmsbackend\Base
10{
11    public function readEntity($source, $providerId, $resolveReferences = 0, $disableCache = false)
12    {
13        $cacheKey = "provider-$source-$providerId-$resolveReferences";
14
15        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
16            return App::$cache->get($cacheKey);
17        }
18
19        $this->testSource($source);
20        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::SELECT);
21        $query
22            ->setResolveLevel($resolveReferences)
23            ->addEntityMapping()
24            ->addResolvedReferences($resolveReferences)
25            ->addConditionProviderSource($source)
26            ->addConditionProviderId($providerId);
27        $provider = $this->fetchOne($query, new Entity());
28
29        if (App::$cache) {
30            App::$cache->set($cacheKey, $provider);
31        }
32
33        return $provider;
34    }
35
36    public function readEntityById($providerId, $resolveReferences = 0, $disableCache = false)
37    {
38        $cacheKey = "provider-byid-$providerId-$resolveReferences";
39
40        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
41            return App::$cache->get($cacheKey);
42        }
43
44        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::SELECT);
45        $query
46            ->setResolveLevel($resolveReferences)
47            ->addEntityMapping()
48            ->addResolvedReferences($resolveReferences)
49            ->addConditionProviderId($providerId);
50        $provider = $this->fetchOne($query, new Entity());
51
52        if (App::$cache && $provider->hasId()) {
53            App::$cache->set($cacheKey, $provider);
54        }
55
56        return $provider;
57    }
58
59    /**
60     * @return array<string, string>
61     */
62    public function readSourceMapByIds(array $providerIds): array
63    {
64        if ($providerIds === []) {
65            return [];
66        }
67
68        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::SELECT);
69        $query
70            ->setResolveLevel(0)
71            ->addEntityMapping()
72            ->addConditionProviderIdList($providerIds);
73
74        $map = [];
75        foreach ($this->readCollection($query) as $provider) {
76            $map[(string) $provider->getId()] = (string) $provider->getSource();
77        }
78
79        return $map;
80    }
81
82    /**
83     * @SuppressWarnings(Param)
84     *
85     */
86    protected function readCollection($query)
87    {
88        $providerList = new Collection();
89        $statement = $this->fetchStatement($query);
90        while ($providerData = $statement->fetch(\PDO::FETCH_ASSOC)) {
91            $entity = new Entity($query->postProcessJoins($providerData));
92            $providerList->addEntity($entity);
93        }
94        return $providerList;
95    }
96
97    public function readListBySource($source, $resolveReferences = 0, $isAssigned = null, $requestIdCsv = null)
98    {
99        $this->testSource($source);
100        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::SELECT);
101        $query->setDistinctSelect();
102        $query->setResolveLevel($resolveReferences);
103        $query->addEntityMapping();
104        $query->addConditionProviderSource($source);
105        if (null !== $isAssigned) {
106            $query->addConditionIsAssigned($isAssigned);
107        }
108        if (null !== $requestIdCsv) {
109            $query->addConditionRequestCsv($requestIdCsv, $source);
110        }
111        $providerList = $this->readCollection($query);
112        return ($providerList->count()) ? $providerList->sortById() : $providerList;
113    }
114
115    public function writeEntity(Entity $entity)
116    {
117        $this->writeDeleteEntity($entity->getId(), $entity->getSource());
118        $contact =  $entity->getContact();
119        if (! $contact->count()) {
120            throw new \BO\Zmsbackend\Provider\Exception\ProviderContactMissed();
121        }
122        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::INSERT);
123        $additionalData = $entity->getAdditionalData() ?? [];
124
125        $query->addValues([
126            'source' => $entity->getSource(),
127            'id' => $entity->getId(),
128            'name' => $entity->getName(),
129            'parent_id' => $entity->getParentId(),
130            'display_name' => $additionalData && isset($additionalData['displayName'])
131                ? $additionalData['displayName']
132                : $entity->getName(),
133            'contact__city' => $contact->getProperty('city'),
134            'contact__country' => $contact->getProperty('country'),
135            'contact__lat' => $contact->getProperty('lat', 0),
136            'contact__lon' => $contact->getProperty('lon', 0),
137            'contact__postalCode' => (string) $contact->getProperty('postalCode'),
138            'contact__region' => $contact->getProperty('region'),
139            'contact__street' => $contact->getProperty('street'),
140            'contact__streetNumber' => $contact->getProperty('streetNumber', '-'),
141            'link' =>  ($entity->getLink()) ? $entity->getLink() : '',
142            'data' => ($entity->getAdditionalData()) ? json_encode($entity->getAdditionalData()) : '{}'
143        ]);
144        $this->writeItem($query);
145
146        $this->removeCache($entity);
147
148        return $this->readEntity($entity->getSource(), $entity->getId(), 0, true);
149    }
150
151    public function writeListBySource(\BO\Zmsentities\Source $source)
152    {
153        $this->writeDeleteListBySource($source->getSource());
154        foreach ($source->getProviderList() as $provider) {
155            $this->writeEntity($provider);
156            $this->removeCache($provider);
157        }
158        return $this->readListBySource($source->getSource());
159    }
160
161    public function writeImportList($providerList, $source = 'dldb')
162    {
163        foreach ($providerList as $provider) {
164            $this->writeImportEntity($provider, $source);
165        }
166        return $this->readListBySource($source, 1);
167    }
168
169    public function writeImportEntity($provider, $source = 'dldb')
170    {
171        if ($provider['address']['postal_code']) {
172            $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::REPLACE);
173            $query->addValues([
174                'source' => $source,
175                'id' => $provider['id'],
176                'name' => $provider['name'],
177                'display_name' => $provider['displayName'] ?? $provider['name'],
178                'contact__city' => $provider['address']['city'],
179                'contact__country' => $provider['address']['city'],
180                'contact__lat' => $provider['geo']['lat'],
181                'contact__lon' => $provider['geo']['lon'],
182                'contact__postalCode' => (string) $provider['address']['postal_code'],
183                'contact__region' => $provider['address']['city'],
184                'contact__street' => $provider['address']['street'],
185                'contact__streetNumber' => $provider['address']['house_number'],
186                'link' => ('dldb' == $source)
187                    ? 'https://service.berlin.de/standort/' . $provider['id'] . '/'
188                    : ((isset($provider['link'])) ? $provider['link'] : ''),
189                'data' => json_encode($provider)
190            ]);
191            $this->writeItem($query);
192            $provider = $this->readEntity($source, $provider['id'], 0, true);
193        }
194        return $provider;
195    }
196
197    public function writeDeleteEntity($providerId, $source)
198    {
199        $provider = $this->readEntity($source, $providerId);
200        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::DELETE);
201        $query->addConditionProviderId($providerId);
202        $query->addConditionProviderSource($source);
203        $this->removeCache($provider);
204        return $this->deleteItem($query);
205    }
206
207    public function writeDeleteListBySource($source)
208    {
209        $query = new \BO\Zmsbackend\Provider\Repository\Provider(\BO\Zmsbackend\Query\Base::DELETE);
210        $query->addConditionProviderSource($source);
211        return $this->deleteItem($query);
212    }
213
214    protected function testSource($source)
215    {
216        if (! (new \BO\Zmsbackend\Source\Service\Source())->readEntity($source)) {
217            throw new \BO\Zmsbackend\Source\Exception\UnknownDataSource();
218        }
219    }
220
221    public function removeCache(Entity $provider)
222    {
223        if (!App::$cache || !isset($provider->id)) {
224            return;
225        }
226
227        $source = $provider->getSource();
228        $providerId = $provider->getId();
229
230        foreach ([0, 1, 2] as $resolveReferences) {
231            foreach (
232                [
233                    "provider-$source-$providerId-$resolveReferences",
234                    "provider-byid-$providerId-$resolveReferences",
235                    "request-$source-$providerId-$resolveReferences",
236                ] as $key
237            ) {
238                if (App::$cache->has($key)) {
239                    App::$cache->delete($key);
240                }
241            }
242        }
243    }
244}