Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#715)
by Alexander
03:36
created

MetadataController   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 271
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 153
dl 0
loc 271
rs 2.64
c 6
b 1
f 0
wmc 72

4 Methods

Rating   Name   Duplication   Size   Complexity  
A injectCollectionRepository() 0 3 1
F mainAction() 0 95 29
A injectMetadataRepository() 0 3 1
D printMetadata() 0 137 41

How to fix   Complexity   

Complex Class

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
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 Ubl\Iiif\Context\IRI;
22
23
class MetadataController extends AbstractController
24
{
25
    public $prefixId = 'tx_dlf';
26
27
    protected $metadataRepository;
28
29
    protected $collectionRepository;
30
31
    /**
32
     * @param CollectionRepository $collectionRepository
33
     */
34
    public function injectCollectionRepository(CollectionRepository $collectionRepository)
35
    {
36
        $this->collectionRepository = $collectionRepository;
37
    }
38
39
    /**
40
     * @param MetadataRepository $metadataRepository
41
     */
42
    public function injectMetadataRepository(MetadataRepository $metadataRepository)
43
    {
44
        $this->metadataRepository = $metadataRepository;
45
    }
46
47
    /**
48
     * @return string|void
49
     */
50
    public function mainAction()
51
    {
52
        $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...
53
54
        // Load current document.
55
        $this->loadDocument($this->requestData);
56
        if (
57
            $this->document === null
58
            || $this->document->getDoc() === null
59
        ) {
60
            // Quit without doing anything if required variables are not set.
61
            return '';
62
        } else {
63
            // Set default values if not set.
64
            if (!isset($this->settings['rootline'])) {
65
                $this->settings['rootline'] = 0;
66
            }
67
            if (!isset($this->settings['originalIiifMetadata'])) {
68
                $this->settings['originalIiifMetadata'] = 0;
69
            }
70
            if (!isset($this->settings['displayIiifDescription'])) {
71
                $this->settings['displayIiifDescription'] = 1;
72
            }
73
            if (!isset($this->settings['displayIiifRights'])) {
74
                $this->settings['displayIiifRights'] = 1;
75
            }
76
            if (!isset($this->settings['displayIiifLinks'])) {
77
                $this->settings['displayIiifLinks'] = 1;
78
            }
79
        }
80
        $useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->document->getDoc() instanceof IiifManifest;
81
        $metadata = [];
82
        if ($this->settings['rootline'] < 2) {
83
            // Get current structure's @ID.
84
            $ids = [];
85
            if (!empty($this->document->getDoc()->physicalStructure[$this->requestData['page']]) && !empty($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]])) {
86
                foreach ($this->document->getDoc()->smLinks['p2l'][$this->document->getDoc()->physicalStructure[$this->requestData['page']]] as $logId) {
87
                    $count = $this->document->getDoc()->getStructureDepth($logId);
88
                    $ids[$count][] = $logId;
89
                }
90
            }
91
            ksort($ids);
92
            reset($ids);
93
            // Check if we should display all metadata up to the root.
94
            if ($this->settings['rootline'] == 1) {
95
                foreach ($ids as $id) {
96
                    foreach ($id as $sid) {
97
                        if ($useOriginalIiifManifestMetadata) {
98
                            $data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']);
99
                        } else {
100
                            $data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']);
101
                        }
102
                        if (!empty($data)) {
103
                            $data['_id'] = $sid;
104
                            $metadata[] = $data;
105
                        }
106
                    }
107
                }
108
            } else {
109
                $id = array_pop($ids);
110
                if (is_array($id)) {
111
                    foreach ($id as $sid) {
112
                        if ($useOriginalIiifManifestMetadata) {
113
                            $data = $this->document->getDoc()->getManifestMetadata($sid, $this->settings['storagePid']);
114
                        } else {
115
                            $data = $this->document->getDoc()->getMetadata($sid, $this->settings['storagePid']);
116
                        }
117
                        if (!empty($data)) {
118
                            $data['_id'] = $sid;
119
                            $metadata[] = $data;
120
                        }
121
                    }
122
                }
123
            }
124
        }
125
        // Get titledata?
126
        if (empty($metadata) || ($this->settings['rootline'] == 1 && $metadata[0]['_id'] != $this->document->getDoc()->toplevelId)) {
127
            $data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitleData($this->settings['storagePid']);
0 ignored issues
show
Bug introduced by
The method getManifestMetadata() does not exist on Kitodo\Dlf\Common\Doc. ( Ignorable by Annotation )

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

127
            $data = $useOriginalIiifManifestMetadata ? $this->document->getDoc()->/** @scrutinizer ignore-call */ getManifestMetadata($this->document->getDoc()->toplevelId, $this->settings['storagePid']) : $this->document->getDoc()->getTitleData($this->settings['storagePid']);

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...
128
            $data['_id'] = $this->document->getDoc()->toplevelId;
129
            array_unshift($metadata, $data);
130
        }
131
        if (empty($metadata)) {
132
            $this->logger->warning('No metadata found for document with UID ' . $this->document->getUid());
133
            return '';
134
        }
135
        ksort($metadata);
136
        // Get hook objects.
137
        $this->hookObjects = Helper::getHookObjects($this->scriptRelPath);
0 ignored issues
show
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...
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...
138
        // Hook for getting a customized title bar (requested by SBB).
139
        foreach ($this->hookObjects as $hookObj) {
140
            if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) {
141
                $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata);
142
            }
143
        }
144
        $this->printMetadata($metadata, $useOriginalIiifManifestMetadata);
145
    }
146
147
    /**
148
     * Prepares the metadata array for output
149
     *
150
     * @access protected
151
     *
152
     * @param array $metadataArray: The metadata array
153
     * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs?
154
     *
155
     * @return string The metadata array ready for output
156
     */
157
    protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false)
158
    {
159
        if ($useOriginalIiifManifestMetadata) {
160
            $iiifData = [];
161
            foreach ($metadataArray as $metadata) {
162
                foreach ($metadata as $key => $group) {
163
                    if ($key == '_id') {
164
                        continue;
165
                    }
166
                    if (!is_array($group)) {
167
                        if (
168
                            IRI::isAbsoluteIri($group)
169
                            && (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https')
170
                        ) {
171
                            // Build link
172
                            $iiifData[$key] = [
173
                                'label' => $key,
174
                                'value' => $group,
175
                                'buildUrl' => true,
176
                            ];
177
                        } else {
178
                            // Data output
179
                            $iiifData[$key] = [
180
                                'label' => $key,
181
                                'value' => $group,
182
                                'buildUrl' => false,
183
                            ];
184
                        }
185
                    } else {
186
                        foreach ($group as $label => $value) {
187
                            if ($label == '_id') {
188
                                continue;
189
                            }
190
                            if (is_array($value)) {
191
                                $value = implode($this->settings['separator'], $value);
192
                            }
193
                            // NOTE: Labels are to be escaped in Fluid template
194
                            if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) {
195
                                $nolabel = $value == $label;
196
                                $iiifData[$key]['data'][] = [
197
                                    'label' => $nolabel ? '' : $label,
198
                                    'value' => $value,
199
                                    'buildUrl' => true,
200
                                ];
201
                            } else {
202
                                $iiifData[$key]['data'][] = [
203
                                    'label' => $label,
204
                                    'value' => $value,
205
                                    'buildUrl' => false,
206
                                ];
207
                            }
208
                        }
209
                    }
210
                    $this->view->assign('useIiif', true);
211
                    $this->view->assign('iiifData', $iiifData);
212
                }
213
            }
214
        } else {
215
216
            // findBySettings also sorts entries by the `sorting` field
217
            $metadataResult = $this->metadataRepository->findBySettings([
218
                'is_listed' => !$this->settings['showFull'],
219
            ]);
220
221
            // Get collections to check if they are hidden
222
            $collections = $this->collectionRepository->getCollectionForMetadata($this->settings['storagePid']);
223
224
            $collList = [];
225
            /** @var Collection $collection */
226
            foreach ($collections as $collection) {
227
                $collList[] = $collection->getIndexName();
228
            }
229
230
            $buildUrl = [];
231
            $i = 0;
232
            foreach ($metadataArray as $metadataSection) {
233
                foreach ($metadataSection as $metadataName => $metadataValue) {
234
                    // NOTE: Labels are to be escaped in Fluid template
235
236
                    if ($metadataName == 'title') {
237
                        // Get title of parent document if needed.
238
                        if (empty($metadataValue) && $this->settings['getTitle'] && $this->document->getDoc()->parentId) {
239
                            $superiorTitle = Doc::getTitle($this->document->getPartof(), true);
240
                            if (!empty($superiorTitle)) {
241
                                $metadataArray[$i][$metadataName] = ['[' . $superiorTitle . ']'];
242
                            }
243
                        }
244
                        if (!empty($metadataValue)) {
245
                            $metadataArray[$i][$metadataName][0] = $metadataArray[$i][$metadataName][0];
246
                            // Link title to pageview.
247
                            if ($this->settings['linkTitle'] && $metadataSection['_id']) {
248
                                $details = $this->document->getDoc()->getLogicalStructure($metadataSection['_id']);
249
                                $buildUrl[$i][$metadataName]['buildUrl'] = [
250
                                    'id' => $this->document->getUid(),
251
                                    'page' => (!empty($details['points']) ? intval($details['points']) : 1),
252
                                    'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0),
253
                                ];
254
                            }
255
                        }
256
                    } elseif ($metadataName == 'owner' && !empty($metadataValue)) {
257
                        // Translate name of holding library.
258
                        $metadataArray[$i][$metadataName][0] = Helper::translate($metadataArray[$i][$metadataName][0], 'tx_dlf_libraries', $this->settings['storagePid']);
259
                    } elseif ($metadataName == 'type' && !empty($metadataValue)) {
260
                        // Translate document type.
261
                        $metadataArray[$i][$metadataName][0] = Helper::translate($metadataArray[$i][$metadataName][0], 'tx_dlf_structures', $this->settings['storagePid']);
262
                    } elseif ($metadataName == 'collection' && !empty($metadataValue)) {
263
                        // Check if collections isn't hidden.
264
                        $j = 0;
265
                        foreach ($metadataValue as $metadataEntry) {
266
                            if (in_array($metadataEntry, $collList)) {
267
                                // Translate collection.
268
                                $metadataArray[$i][$metadataName][$j] = Helper::translate($metadataArray[$i][$metadataName][$j], 'tx_dlf_collections', $this->settings['storagePid']);
269
                            } else {
270
                                $metadataArray[$i][$metadataName][$j] = '';
271
                            }
272
                            $j++;
273
                        }
274
                    } elseif ($metadataName == 'language' && !empty($metadataValue)) {
275
                        // Translate ISO 639 language code.
276
                        $metadataArray[$i][$metadataName][0] = Helper::getLanguageName($metadataArray[$i][$metadataName][0]);
277
                    } elseif (!empty($metadataValue)) {
278
                        $metadataArray[$i][$metadataName][0] = $metadataArray[$i][$metadataName][0];
279
                    }
280
281
                    if (is_array($metadataArray[$i][$metadataName])) {
282
                        $metadataArray[$i][$metadataName] = array_values(array_filter($metadataArray[$i][$metadataName], function ($value) {
283
                            return !empty($value);
284
                        }));
285
                    }
286
                }
287
                $i++;
288
            }
289
290
            $this->view->assign('buildUrl', $buildUrl);
291
            $this->view->assign('documentMetadataSections', $metadataArray);
292
            $this->view->assign('configMetadata', $metadataResult);
293
            $this->view->assign('separator', $this->settings['separator']);
294
295
        }
296
    }
297
}
298