Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.39% |
101 / 107 |
|
81.82% |
9 / 11 |
CRAP | |
0.00% |
0 / 1 |
Provider | |
94.39% |
101 / 107 |
|
81.82% |
9 / 11 |
30.16 | |
0.00% |
0 / 1 |
readEntity | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
readCollection | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
readListBySource | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
writeEntity | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
6 | |||
writeListBySource | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
writeImportList | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
writeImportEntity | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
4 | |||
writeDeleteEntity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
deleteEntitySafe | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
writeDeleteListBySource | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
testSource | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Provider as Entity; |
6 | use BO\Zmsentities\Collection\ProviderList as Collection; |
7 | use BO\Zmsdb\Scope as Scope; |
8 | |
9 | class Provider extends Base |
10 | { |
11 | public function readEntity($source, $providerId, $resolveReferences = 0) |
12 | { |
13 | $this->testSource($source); |
14 | $query = new Query\Provider(Query\Base::SELECT); |
15 | $query |
16 | ->setResolveLevel($resolveReferences) |
17 | ->addEntityMapping() |
18 | ->addResolvedReferences($resolveReferences) |
19 | ->addConditionProviderSource($source) |
20 | ->addConditionProviderId($providerId); |
21 | $provider = $this->fetchOne($query, new Entity()); |
22 | $inUseByScope = (new Scope())->countByInfoDienstleister($providerId, $source) > 0; |
23 | $inUseByRel = (new RequestRelation())->countByProviderId($providerId, $source) > 0; |
24 | $provider['canDelete'] = ! ($inUseByScope || $inUseByRel); |
25 | return $provider; |
26 | } |
27 | |
28 | /** |
29 | * @SuppressWarnings(Param) |
30 | * |
31 | */ |
32 | protected function readCollection($query) |
33 | { |
34 | $providerList = new Collection(); |
35 | $statement = $this->fetchStatement($query); |
36 | while ($providerData = $statement->fetch(\PDO::FETCH_ASSOC)) { |
37 | $entity = new Entity($query->postProcessJoins($providerData)); |
38 | $source = $entity->getSource(); |
39 | $id = $entity->getId(); |
40 | $inUseByScope = (new Scope())->countByInfoDienstleister($id, $source) > 0; |
41 | $inUseByRel = (new RequestRelation())->countByProviderId($id, $source) > 0; |
42 | $entity['canDelete'] = ! ($inUseByScope || $inUseByRel); |
43 | $providerList->addEntity($entity); |
44 | } |
45 | return $providerList; |
46 | } |
47 | |
48 | public function readListBySource($source, $resolveReferences = 0, $isAssigned = null, $requestIdCsv = null) |
49 | { |
50 | $this->testSource($source); |
51 | $query = new Query\Provider(Query\Base::SELECT); |
52 | $query->setDistinctSelect(); |
53 | $query->setResolveLevel($resolveReferences); |
54 | $query->addEntityMapping(); |
55 | $query->addConditionProviderSource($source); |
56 | if (null !== $isAssigned) { |
57 | $query->addConditionIsAssigned($isAssigned); |
58 | } |
59 | if (null !== $requestIdCsv) { |
60 | $query->addConditionRequestCsv($requestIdCsv, $source); |
61 | } |
62 | $providerList = $this->readCollection($query); |
63 | return ($providerList->count()) ? $providerList->sortById() : $providerList; |
64 | } |
65 | |
66 | public function writeEntity(Entity $entity) |
67 | { |
68 | $this->writeDeleteEntity($entity->getId(), $entity->getSource()); |
69 | $contact = $entity->getContact(); |
70 | if (! $contact->count()) { |
71 | throw new Exception\Provider\ProviderContactMissed(); |
72 | } |
73 | $query = new Query\Provider(Query\Base::INSERT); |
74 | $additionalData = $entity->getAdditionalData() ?? []; |
75 | |
76 | $query->addValues([ |
77 | 'source' => $entity->getSource(), |
78 | 'id' => $entity->getId(), |
79 | 'name' => $entity->getName(), |
80 | 'parent_id' => $entity->getParentId(), |
81 | 'display_name' => $additionalData && isset($additionalData['displayName']) |
82 | ? $additionalData['displayName'] |
83 | : $entity->getName(), |
84 | 'contact__city' => $contact->getProperty('city'), |
85 | 'contact__country' => $contact->getProperty('country'), |
86 | 'contact__lat' => $contact->getProperty('lat', 0), |
87 | 'contact__lon' => $contact->getProperty('lon', 0), |
88 | 'contact__postalCode' => intval($contact->getProperty('postalCode')), |
89 | 'contact__region' => $contact->getProperty('region'), |
90 | 'contact__street' => $contact->getProperty('street'), |
91 | 'contact__streetNumber' => $contact->getProperty('streetNumber', '-'), |
92 | 'link' => ($entity->getLink()) ? $entity->getLink() : '', |
93 | 'data' => ($entity->getAdditionalData()) ? json_encode($entity->getAdditionalData()) : '{}' |
94 | ]); |
95 | $this->writeItem($query); |
96 | return $this->readEntity($entity->getSource(), $entity->getId()); |
97 | } |
98 | |
99 | public function writeListBySource(\BO\Zmsentities\Source $source) |
100 | { |
101 | $this->writeDeleteListBySource($source->getSource()); |
102 | foreach ($source->getProviderList() as $provider) { |
103 | $this->writeEntity($provider); |
104 | } |
105 | return $this->readListBySource($source->getSource()); |
106 | } |
107 | |
108 | public function writeImportList($providerList, $source = 'dldb') |
109 | { |
110 | foreach ($providerList as $provider) { |
111 | $this->writeImportEntity($provider, $source); |
112 | } |
113 | return $this->readListBySource($source, 1); |
114 | } |
115 | |
116 | public function writeImportEntity($provider, $source = 'dldb') |
117 | { |
118 | if ($provider['address']['postal_code']) { |
119 | $query = new Query\Provider(Query\Base::REPLACE); |
120 | $query->addValues([ |
121 | 'source' => $source, |
122 | 'id' => $provider['id'], |
123 | 'name' => $provider['name'], |
124 | 'display_name' => $provider['displayName'] ?? $provider['name'], |
125 | 'contact__city' => $provider['address']['city'], |
126 | 'contact__country' => $provider['address']['city'], |
127 | 'contact__lat' => $provider['geo']['lat'], |
128 | 'contact__lon' => $provider['geo']['lon'], |
129 | 'contact__postalCode' => intval($provider['address']['postal_code']), |
130 | 'contact__region' => $provider['address']['city'], |
131 | 'contact__street' => $provider['address']['street'], |
132 | 'contact__streetNumber' => $provider['address']['house_number'], |
133 | 'link' => ('dldb' == $source) |
134 | ? 'https://service.berlin.de/standort/' . $provider['id'] . '/' |
135 | : ((isset($provider['link'])) ? $provider['link'] : ''), |
136 | 'data' => json_encode($provider) |
137 | ]); |
138 | $this->writeItem($query); |
139 | $provider = $this->readEntity($source, $provider['id']); |
140 | } |
141 | return $provider; |
142 | } |
143 | |
144 | public function writeDeleteEntity($providerId, $source) |
145 | { |
146 | $query = new Query\Provider(Query\Base::DELETE); |
147 | $query->addConditionProviderId($providerId); |
148 | $query->addConditionProviderSource($source); |
149 | return $this->deleteItem($query); |
150 | } |
151 | |
152 | public function deleteEntitySafe(string $source, string $providerId): void |
153 | { |
154 | $scopeCount = (new Scope())->countByInfoDienstleister($providerId, $source); |
155 | $rrCount = (new RequestRelation())->countByProviderId($providerId, $source); |
156 | |
157 | if ($scopeCount > 0 || $rrCount > 0) { |
158 | throw new Exception\Provider\ProviderInUse(); |
159 | } |
160 | $this->writeDeleteEntity($providerId, $source); |
161 | } |
162 | |
163 | public function writeDeleteListBySource($source) |
164 | { |
165 | $query = new Query\Provider(Query\Base::DELETE); |
166 | $query->addConditionProviderSource($source); |
167 | return $this->deleteItem($query); |
168 | } |
169 | |
170 | protected function testSource($source) |
171 | { |
172 | if (! (new Source())->readEntity($source)) { |
173 | throw new Exception\Source\UnknownDataSource(); |
174 | } |
175 | } |
176 | } |