We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 87 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like MetadataController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetadataController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class MetadataController extends AbstractController |
||
33 | { |
||
34 | /** |
||
35 | * @var CollectionRepository |
||
36 | */ |
||
37 | protected $collectionRepository; |
||
38 | |||
39 | /** |
||
40 | * @param CollectionRepository $collectionRepository |
||
41 | */ |
||
42 | public function injectCollectionRepository(CollectionRepository $collectionRepository) |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @var MetadataRepository |
||
49 | */ |
||
50 | protected $metadataRepository; |
||
51 | |||
52 | /** |
||
53 | * @param MetadataRepository $metadataRepository |
||
54 | */ |
||
55 | public function injectMetadataRepository(MetadataRepository $metadataRepository) |
||
56 | { |
||
57 | $this->metadataRepository = $metadataRepository; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @var StructureRepository |
||
62 | */ |
||
63 | protected $structureRepository; |
||
64 | |||
65 | /** |
||
66 | * @param StructureRepository $structureRepository |
||
67 | */ |
||
68 | public function injectStructureRepository(StructureRepository $structureRepository) |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string|void |
||
75 | */ |
||
76 | public function mainAction() |
||
77 | { |
||
78 | $this->cObj = $this->configurationManager->getContentObject(); |
||
|
|||
79 | |||
80 | // Load current document. |
||
81 | $this->loadDocument($this->requestData); |
||
82 | if ($this->isDocMissing()) { |
||
83 | // Quit without doing anything if required variables are not set. |
||
84 | return ''; |
||
85 | } else { |
||
86 | // Set default values if not set. |
||
87 | if (!isset($this->settings['rootline'])) { |
||
88 | $this->settings['rootline'] = 0; |
||
89 | } |
||
90 | if (!isset($this->settings['originalIiifMetadata'])) { |
||
91 | $this->settings['originalIiifMetadata'] = 0; |
||
92 | } |
||
93 | if (!isset($this->settings['displayIiifDescription'])) { |
||
94 | $this->settings['displayIiifDescription'] = 1; |
||
95 | } |
||
96 | if (!isset($this->settings['displayIiifRights'])) { |
||
97 | $this->settings['displayIiifRights'] = 1; |
||
98 | } |
||
99 | if (!isset($this->settings['displayIiifLinks'])) { |
||
100 | $this->settings['displayIiifLinks'] = 1; |
||
101 | } |
||
102 | } |
||
103 | $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest; |
||
104 | $metadata = $this->getMetadata(); |
||
105 | // Get titledata? |
||
106 | if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->document->getDoc()->toplevelId)) { |
||
107 | $data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitledata($this->settings['storagePid']); |
||
108 | $data['_id'] = $this->document->getDoc()->toplevelId; |
||
109 | $data['_active'] = true; |
||
110 | array_unshift($metadata, $data); |
||
111 | } |
||
112 | if (empty($metadata)) { |
||
113 | $this->logger->warning('No metadata found for document with UID ' . $this->document->getUid()); |
||
114 | return ''; |
||
115 | } |
||
116 | ksort($metadata); |
||
117 | |||
118 | $this->printMetadata($metadata, $useOriginalIiifManifestMetadata); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Prepares the metadata array for output |
||
123 | * |
||
124 | * @access protected |
||
125 | * |
||
126 | * @param array $metadata: The metadata array |
||
127 | * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs? |
||
128 | * |
||
129 | * @return string The metadata array ready for output |
||
130 | */ |
||
131 | protected function printMetadata(array $metadata, $useOriginalIiifManifestMetadata = false) |
||
132 | { |
||
133 | if ($useOriginalIiifManifestMetadata) { |
||
134 | $iiifData = []; |
||
135 | foreach ($metadata as $row) { |
||
136 | foreach ($row as $key => $group) { |
||
137 | if ($key == '_id' || $key === '_active') { |
||
138 | continue; |
||
139 | } |
||
140 | if (!is_array($group)) { |
||
141 | if ( |
||
142 | IRI::isAbsoluteIri($group) |
||
143 | && (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https') |
||
144 | ) { |
||
145 | // Build link |
||
146 | $iiifData[$key] = [ |
||
147 | 'label' => $key, |
||
148 | 'value' => $group, |
||
149 | 'buildUrl' => true, |
||
150 | ]; |
||
151 | } else { |
||
152 | // Data output |
||
153 | $iiifData[$key] = [ |
||
154 | 'label' => $key, |
||
155 | 'value' => $group, |
||
156 | 'buildUrl' => false, |
||
157 | ]; |
||
158 | } |
||
159 | } else { |
||
160 | foreach ($group as $label => $value) { |
||
161 | if ($label === '_id' || $label === '_active') { |
||
162 | continue; |
||
163 | } |
||
164 | if (is_array($value)) { |
||
165 | $value = implode($this->settings['separator'], $value); |
||
166 | } |
||
167 | // NOTE: Labels are to be escaped in Fluid template |
||
168 | if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) { |
||
169 | $nolabel = $value == $label; |
||
170 | $iiifData[$key]['data'][] = [ |
||
171 | 'label' => $nolabel ? '' : $label, |
||
172 | 'value' => $value, |
||
173 | 'buildUrl' => true, |
||
174 | ]; |
||
175 | } else { |
||
176 | $iiifData[$key]['data'][] = [ |
||
177 | 'label' => $label, |
||
178 | 'value' => $value, |
||
179 | 'buildUrl' => false, |
||
180 | ]; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | $this->view->assign('useIiif', true); |
||
185 | $this->view->assign('iiifData', $iiifData); |
||
186 | } |
||
187 | } |
||
188 | } else { |
||
189 | // findBySettings also sorts entries by the `sorting` field |
||
190 | $metadataResult = $this->metadataRepository->findBySettings([ |
||
191 | 'is_listed' => !$this->settings['showFull'], |
||
192 | ]); |
||
193 | |||
194 | // Collect raw metadata into an array that will be passed as data to cObj. |
||
195 | // This lets metadata wraps reference (own or foreign) values via TypoScript "field". |
||
196 | $metaCObjData = []; |
||
197 | |||
198 | $buildUrl = []; |
||
199 | $externalUrl = []; |
||
200 | $i = 0; |
||
201 | foreach ($metadata as $section) { |
||
202 | $metaCObjData[$i] = []; |
||
203 | |||
204 | foreach ($section as $name => $value) { |
||
205 | // NOTE: Labels are to be escaped in Fluid template |
||
206 | |||
207 | $metaCObjData[$i][$name] = is_array($value) |
||
208 | ? implode($this->settings['separator'], $value) |
||
209 | : $value; |
||
210 | |||
211 | if ($name == 'title') { |
||
212 | // Get title of parent document if needed. |
||
213 | if (empty(implode('', $value)) && $this->settings['getTitle'] && $this->document->getPartof()) { |
||
214 | $superiorTitle = Doc::getTitle($this->document->getPartof(), true); |
||
215 | if (!empty($superiorTitle)) { |
||
216 | $metadata[$i][$name] = ['[' . $superiorTitle . ']']; |
||
217 | } |
||
218 | } |
||
219 | if (!empty($value)) { |
||
220 | $metadata[$i][$name][0] = $metadata[$i][$name][0]; |
||
221 | // Link title to pageview. |
||
222 | if ($this->settings['linkTitle'] && $section['_id']) { |
||
223 | $details = $this->document->getDoc()->getLogicalStructure($section['_id']); |
||
224 | $buildUrl[$i][$name]['buildUrl'] = [ |
||
225 | 'id' => $this->document->getUid(), |
||
226 | 'page' => (!empty($details['points']) ? intval($details['points']) : 1), |
||
227 | 'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0), |
||
228 | ]; |
||
229 | } |
||
230 | } |
||
231 | } elseif (($name == 'author' || $name == 'holder') && !empty($value) && !empty($value[0]['url'])) { |
||
232 | $externalUrl[$i][$name]['externalUrl'] = $value[0]; |
||
233 | } elseif (($name == 'geonames' || $name == 'wikidata' || $name == 'wikipedia') && !empty($value)) { |
||
234 | $externalUrl[$i][$name]['externalUrl'] = [ |
||
235 | 'name' => $value[0], |
||
236 | 'url' => $value[0] |
||
237 | ]; |
||
238 | } elseif ($name == 'owner' && empty($value)) { |
||
239 | // no owner is found by metadata records --> take the one associated to the document |
||
240 | $library = $this->document->getOwner(); |
||
241 | if ($library) { |
||
242 | $metadata[$i][$name][0] = $library->getLabel(); |
||
243 | } |
||
244 | } elseif ($name == 'type' && !empty($value)) { |
||
245 | // Translate document type. |
||
246 | $structure = $this->structureRepository->findOneByIndexName($metadata[$i][$name][0]); |
||
247 | if ($structure) { |
||
248 | $metadata[$i][$name][0] = $structure->getLabel(); |
||
249 | } |
||
250 | } elseif ($name == 'collection' && !empty($value)) { |
||
251 | // Check if collections isn't hidden. |
||
252 | $j = 0; |
||
253 | foreach ($value as $entry) { |
||
254 | $collection = $this->collectionRepository->findOneByIndexName($entry); |
||
255 | if ($collection) { |
||
256 | $metadata[$i][$name][$j] = $collection->getLabel() ? : ''; |
||
257 | $j++; |
||
258 | } |
||
259 | } |
||
260 | } elseif ($name == 'language' && !empty($value)) { |
||
261 | // Translate ISO 639 language code. |
||
262 | foreach ($metadata[$i][$name] as &$langValue) { |
||
263 | $langValue = Helper::getLanguageName($langValue); |
||
264 | } |
||
265 | } elseif (!empty($value)) { |
||
266 | $metadata[$i][$name][0] = $metadata[$i][$name][0]; |
||
267 | } |
||
268 | |||
269 | if (is_array($metadata[$i][$name])) { |
||
270 | $metadata[$i][$name] = array_values(array_filter($metadata[$i][$name], function($metadataValue) |
||
271 | { |
||
272 | return !empty($metadataValue); |
||
273 | })); |
||
274 | } |
||
275 | } |
||
276 | $i++; |
||
277 | } |
||
278 | |||
279 | $this->view->assign('buildUrl', $buildUrl); |
||
280 | $this->view->assign('externalUrl', $externalUrl); |
||
281 | $this->view->assign('documentMetadataSections', $metadata); |
||
282 | $this->view->assign('configMetadata', $metadataResult); |
||
283 | $this->view->assign('separator', $this->settings['separator']); |
||
284 | $this->view->assign('metaCObjData', $metaCObjData); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Get metadata for given id array. |
||
290 | * |
||
291 | * @access private |
||
292 | * |
||
293 | * @return array metadata |
||
294 | */ |
||
295 | private function getMetadata() |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Get metadata for given id array. |
||
344 | * |
||
345 | * @access private |
||
346 | * |
||
347 | * @param array $id: An array with ids |
||
348 | * @param array $metadata: An array with metadata |
||
349 | * |
||
350 | * @return array metadata |
||
351 | */ |
||
352 | private function getMetadataForIds($id, $metadata) |
||
368 | } |
||
369 | } |
||
370 |