Passed
Pull Request — master (#103)
by Alexander
03:37
created

MetadataController::printMetadata()   D

Complexity

Conditions 43
Paths 60

Size

Total Lines 133
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 0
Metric Value
cc 43
eloc 85
c 9
b 1
f 0
nc 60
nop 2
dl 0
loc 133
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
4
 *
5
 * This file is part of the Kitodo and TYPO3 projects.
6
 *
7
 * @license GNU General Public License version 3 or later.
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Kitodo\Dlf\Controller;
13
14
use Kitodo\Dlf\Common\Doc;
15
use Kitodo\Dlf\Common\Helper;
16
use Kitodo\Dlf\Common\IiifManifest;
17
use Kitodo\Dlf\Domain\Model\Collection;
18
use Kitodo\Dlf\Domain\Model\Metadata;
19
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
20
use Kitodo\Dlf\Domain\Repository\MetadataRepository;
21
use Kitodo\Dlf\Domain\Repository\StructureRepository;
22
use Ubl\Iiif\Context\IRI;
23
24
/**
25
 * Controller class for the plugin 'Metadata'.
26
 *
27
 * @author Sebastian Meyer <[email protected]>
28
 * @package TYPO3
29
 * @subpackage dlf
30
 * @access public
31
 */
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)
43
    {
44
        $this->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)
69
    {
70
        $this->structureRepository = $structureRepository;
71
    }
72
73
    /**
74
     * @return string|void
75
     */
76
    public function mainAction()
77
    {
78
        $this->cObj = $this->configurationManager->getContentObject();
0 ignored issues
show
Bug Best Practice introduced by
The property cObj does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
80
        // Load current document.
81
        $this->loadDocument($this->requestData);
82
        if (
83
            $this->document === null
84
            || $this->document->getDoc() === null
85
        ) {
86
            // Quit without doing anything if required variables are not set.
87
            return '';
88
        } else {
89
            // Set default values if not set.
90
            if (!isset($this->settings['rootline'])) {
91
                $this->settings['rootline'] = 0;
92
            }
93
            if (!isset($this->settings['originalIiifMetadata'])) {
94
                $this->settings['originalIiifMetadata'] = 0;
95
            }
96
            if (!isset($this->settings['displayIiifDescription'])) {
97
                $this->settings['displayIiifDescription'] = 1;
98
            }
99
            if (!isset($this->settings['displayIiifRights'])) {
100
                $this->settings['displayIiifRights'] = 1;
101
            }
102
            if (!isset($this->settings['displayIiifLinks'])) {
103
                $this->settings['displayIiifLinks'] = 1;
104
            }
105
        }
106
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest;
107
        $metadata = $this->getMetadata();
108
        // Get titledata?
109
        if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->document->getDoc()->toplevelId)) {
110
            $data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitleData($this->settings['storagePid']);
111
            $data['_id'] = $this->document->getDoc()->toplevelId;
112
            array_unshift($metadata, $data);
113
        }
114
        if (empty($metadata)) {
115
            $this->logger->warning('No metadata found for document with UID ' . $this->document->getUid());
0 ignored issues
show
Bug introduced by
The method warning() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
            $this->logger->/** @scrutinizer ignore-call */ 
116
                           warning('No metadata found for document with UID ' . $this->document->getUid());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
            return '';
117
        }
118
        ksort($metadata);
119
        // Get hook objects.
120
        $this->hookObjects = Helper::getHookObjects($this->scriptRelPath);
0 ignored issues
show
Bug Best Practice introduced by
The property scriptRelPath does not exist on Kitodo\Dlf\Controller\MetadataController. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property hookObjects does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
121
        // Hook for getting a customized title bar (requested by SBB).
122
        foreach ($this->hookObjects as $hookObj) {
123
            if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) {
124
                $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata);
125
            }
126
        }
127
        $this->printMetadata($metadata, $useOriginalIiifManifestMetadata);
128
    }
129
130
    /**
131
     * Prepares the metadata array for output
132
     *
133
     * @access protected
134
     *
135
     * @param array $metadataArray: The metadata array
136
     * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs?
137
     *
138
     * @return string The metadata array ready for output
139
     */
140
    protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false)
141
    {
142
        if ($useOriginalIiifManifestMetadata) {
143
            $iiifData = [];
144
            foreach ($metadataArray as $metadata) {
145
                foreach ($metadata as $key => $group) {
146
                    if ($key == '_id') {
147
                        continue;
148
                    }
149
                    if (!is_array($group)) {
150
                        if (
151
                            IRI::isAbsoluteIri($group)
152
                            && (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https')
153
                        ) {
154
                            // Build link
155
                            $iiifData[$key] = [
156
                                'label' => $key,
157
                                'value' => $group,
158
                                'buildUrl' => true,
159
                            ];
160
                        } else {
161
                            // Data output
162
                            $iiifData[$key] = [
163
                                'label' => $key,
164
                                'value' => $group,
165
                                'buildUrl' => false,
166
                            ];
167
                        }
168
                    } else {
169
                        foreach ($group as $label => $value) {
170
                            if ($label == '_id') {
171
                                continue;
172
                            }
173
                            if (is_array($value)) {
174
                                $value = implode($this->settings['separator'], $value);
175
                            }
176
                            // NOTE: Labels are to be escaped in Fluid template
177
                            if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) {
178
                                $nolabel = $value == $label;
179
                                $iiifData[$key]['data'][] = [
180
                                    'label' => $nolabel ? '' : $label,
181
                                    'value' => $value,
182
                                    'buildUrl' => true,
183
                                ];
184
                            } else {
185
                                $iiifData[$key]['data'][] = [
186
                                    'label' => $label,
187
                                    'value' => $value,
188
                                    'buildUrl' => false,
189
                                ];
190
                            }
191
                        }
192
                    }
193
                    $this->view->assign('useIiif', true);
194
                    $this->view->assign('iiifData', $iiifData);
195
                }
196
            }
197
        } else {
198
199
            // findBySettings also sorts entries by the `sorting` field
200
            $metadataResult = $this->metadataRepository->findBySettings([
201
                'is_listed' => !$this->settings['showFull'],
202
            ]);
203
204
            $buildUrl = [];
205
            $i = 0;
206
            foreach ($metadataArray as $metadataSection) {
207
                foreach ($metadataSection as $metadataName => $metadataValue) {
208
                    // NOTE: Labels are to be escaped in Fluid template
209
210
                    if ($metadataName == 'title') {
211
                        // Get title of parent document if needed.
212
                        if (empty($metadataValue) && $this->settings['getTitle'] && $this->document->getDoc()->parentId) {
213
                            $superiorTitle = Doc::getTitle($this->document->getPartof(), true);
214
                            if (!empty($superiorTitle)) {
215
                                $metadataArray[$i][$metadataName] = ['[' . $superiorTitle . ']'];
216
                            }
217
                        }
218
                        if (!empty($metadataValue)) {
219
                            $metadataArray[$i][$metadataName][0] = $metadataArray[$i][$metadataName][0];
220
                            // Link title to pageview.
221
                            if ($this->settings['linkTitle'] && $metadataSection['_id']) {
222
                                $details = $this->document->getDoc()->getLogicalStructure($metadataSection['_id']);
223
                                $buildUrl[$i][$metadataName]['buildUrl'] = [
224
                                    'id' => $this->document->getUid(),
225
                                    'page' => (!empty($details['points']) ? intval($details['points']) : 1),
226
                                    'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0),
227
                                ];
228
                            }
229
                        }
230
                    } elseif ($metadataName == 'owner' && empty($metadataValue)) {
231
                        // no owner is found by metadata records --> take the one associated to the document
232
                        $library = $this->document->getOwner();
233
                        if ($library) {
234
                            $metadataArray[$i][$metadataName][0] = $library->getLabel();
235
                        }
236
                    } elseif ($metadataName == 'type' && !empty($metadataValue)) {
237
                        // Translate document type.
238
                        $structure = $this->structureRepository->findOneByIndexName($metadataArray[$i][$metadataName][0]);
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\StructureRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
                        /** @scrutinizer ignore-call */ 
239
                        $structure = $this->structureRepository->findOneByIndexName($metadataArray[$i][$metadataName][0]);
Loading history...
239
                        if ($structure) {
240
                            $metadataArray[$i][$metadataName][0] = $structure->getLabel();
0 ignored issues
show
Bug introduced by
The method getLabel() does not exist on TYPO3\CMS\Extbase\Persistence\QueryResultInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
                            /** @scrutinizer ignore-call */ 
241
                            $metadataArray[$i][$metadataName][0] = $structure->getLabel();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
                        }
242
                    } elseif ($metadataName == 'collection' && !empty($metadataValue)) {
243
                        // Check if collections isn't hidden.
244
                        $j = 0;
245
                        foreach ($metadataValue as $metadataEntry) {
246
                            $collection = $this->collectionRepository->findOneByIndexName($metadataEntry);
0 ignored issues
show
Bug introduced by
The method findOneByIndexName() does not exist on Kitodo\Dlf\Domain\Repository\CollectionRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
                            /** @scrutinizer ignore-call */ 
247
                            $collection = $this->collectionRepository->findOneByIndexName($metadataEntry);
Loading history...
247
                            if ($collection) {
248
                                $metadataArray[$i][$metadataName][$j] = $collection->getLabel() ? : '';
249
                                $j++;
250
                            }
251
                        }
252
                    } elseif ($metadataName == 'language' && !empty($metadataValue)) {
253
                        // Translate ISO 639 language code.
254
                        $metadataArray[$i][$metadataName][0] = Helper::getLanguageName($metadataArray[$i][$metadataName][0]);
255
                    } elseif (!empty($metadataValue)) {
256
                        $metadataArray[$i][$metadataName][0] = $metadataArray[$i][$metadataName][0];
257
                    }
258
259
                    if (is_array($metadataArray[$i][$metadataName])) {
260
                        $metadataArray[$i][$metadataName] = array_values(array_filter($metadataArray[$i][$metadataName], function($value)
261
                        {
262
                            return !empty($value);
263
                        }));
264
                    }
265
                }
266
                $i++;
267
            }
268
269
            $this->view->assign('buildUrl', $buildUrl);
270
            $this->view->assign('documentMetadataSections', $metadataArray);
271
            $this->view->assign('configMetadata', $metadataResult);
272
            $this->view->assign('separator', $this->settings['separator']);
273
274
        }
275
    }
276
277
    /**
278
     * Get metadata for given id array.
279
     *
280
     * @access private
281
     *
282
     * @return array metadata
283
     */
284
    private function getMetadata() {
285
        $metadata = [];
286
        if ($this->settings['rootline'] < 2) {
287
            // Get current structure's @ID.
288
            $ids = [];
289
            if (!empty($this->document->getDoc()->physicalStructure[$this->requestData['page']]) && !empty($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]])) {
290
                foreach ($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]] as $logId) {
291
                    $count = $this->document->getDoc()->getStructureDepth($logId);
292
                    $ids[$count][] = $logId;
293
                }
294
            }
295
            ksort($ids);
296
            reset($ids);
297
            // Check if we should display all metadata up to the root.
298
            if ($this->settings['rootline'] == 1) {
299
                foreach ($ids as $id) {
300
                    $metadata = $this->getMetadataForIds($id, $metadata);
301
                }
302
            } else {
303
                $id = array_pop($ids);
304
                if (is_array($id)) {
305
                    $metadata = $this->getMetadataForIds($id, $metadata);
306
                }
307
            }
308
        }
309
        return $metadata;
310
    }
311
312
    /**
313
     * Get metadata for given id array.
314
     *
315
     * @access private
316
     *
317
     * @param array $id: An array with ids
318
     * @param array $metadata: An array with metadata
319
     *
320
     * @return array metadata
321
     */
322
    private function getMetadataForIds($id, $metadata) {
323
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest;
324
        foreach ($id as $sid) {
325
            if ($useOriginalIiifManifestMetadata) {
326
                $data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']);
327
            } else {
328
                $data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']);
329
            }
330
            if (!empty($data)) {
331
                $data['_id'] = $sid;
332
                $metadata[] = $data;
333
            }
334
        }
335
        return $metadata;
336
    }
337
}
338