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 — dev-extbase-fluid (#739)
by
unknown
07:45
created

MetadataController   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 155
c 3
b 0
f 0
dl 0
loc 286
rs 2.64
wmc 72

6 Methods

Rating   Name   Duplication   Size   Complexity  
A injectCollectionRepository() 0 3 1
F mainAction() 0 95 28
A injectMetadataRepository() 0 3 1
A pi_getLL() 0 3 1
A parseTS() 0 5 1
D printMetadata() 0 128 40

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

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

128
            $data = $useOriginalIiifManifestMetadata ? $this->doc->/** @scrutinizer ignore-call */ getManifestMetadata($this->doc->toplevelId, $this->settings['pages']) : $this->doc->getTitleData($this->settings['pages']);
Loading history...
129
            $data['_id'] = $this->doc->toplevelId;
130
            array_unshift($metadata, $data);
131
        }
132
        if (empty($metadata)) {
133
            $this->logger->warning('No metadata found for document with UID ' . $this->doc->uid);
1 ignored issue
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

133
            $this->logger->/** @scrutinizer ignore-call */ 
134
                           warning('No metadata found for document with UID ' . $this->doc->uid);

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...
134
            return '';
135
        }
136
        ksort($metadata);
137
        // Get hook objects.
138
        $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...
139
        // Hook for getting a customized title bar (requested by SBB).
140
        foreach ($this->hookObjects as $hookObj) {
141
            if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) {
142
                $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata);
143
            }
144
        }
145
        $this->printMetadata($metadata, $useOriginalIiifManifestMetadata);
146
    }
147
148
    /**
149
     * Prepares the metadata array for output
150
     *
151
     * @access protected
152
     *
153
     * @param array $metadataArray: The metadata array
154
     * @param bool $useOriginalIiifManifestMetadata: Output IIIF metadata as simple key/value pairs?
155
     *
156
     * @return string The metadata array ready for output
157
     */
158
    protected function printMetadata(array $metadataArray, $useOriginalIiifManifestMetadata = false)
159
    {
160
        if ($useOriginalIiifManifestMetadata) {
161
            $iiifData = [];
162
            foreach ($metadataArray as $metadata) {
163
                foreach ($metadata as $key => $group) {
164
                    if ($key == '_id') {
165
                        continue;
166
                    }
167
                    if (!is_array($group)) {
168
                        if (
169
                            IRI::isAbsoluteIri($group)
170
                            && (($scheme = (new IRI($group))->getScheme()) == 'http' || $scheme == 'https')
171
                        ) {
172
                            // Build link
173
                            $iiifData[$key] = [
174
                                'label' => $key,
175
                                'value' => $group,
176
                                'buildUrl' => true
177
                            ];
178
                        } else {
179
                            // Data output
180
                            $iiifData[$key] = [
181
                                'label' => $key,
182
                                'value' => $group,
183
                                'buildUrl' => false
184
                            ];
185
                        }
186
                    } else {
187
                        foreach ($group as $label => $value) {
188
                            if ($label == '_id') {
189
                                continue;
190
                            }
191
                            if (is_array($value)) {
192
                                $value = implode($this->settings['separator'], $value);
193
                            }
194
                            if (IRI::isAbsoluteIri($value) && (($scheme = (new IRI($value))->getScheme()) == 'http' || $scheme == 'https')) {
195
                                $nolabel = $value == $label;
196
                                $iiifData[$key]['data'][] = [
197
                                    'label' => $nolabel ? '' : htmlspecialchars($label),
198
                                    'value' => $value,
199
                                    'buildUrl' => true
200
                                ];
201
                            } else {
202
                                $iiifData[$key]['data'][] = [
203
                                    'label' => htmlspecialchars($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
            $context = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class);
216
            $currentLanguageUid = $context->getPropertyFromAspect('language', 'id');
217
218
            $metadataResult = $this->metadataRepository->getMetadata($this->settings['pages'], $currentLanguageUid);
219
220
            // Get collections to check if they are hidden
221
            $collections = $this->collectionRepository->getCollectionForMetadata($this->settings['pages']);
222
223
            $collList = [];
224
            /** @var Collection $collection */
225
            foreach ($collections as $collection) {
226
                $collList[] = $collection->getIndexName();
227
            }
228
229
            $buildUrl = [];
230
            $i = 0;
231
            foreach ($metadataArray as $metadataSection) {
232
                foreach ($metadataSection as $metadataName => $metadataValue) {
233
                    if ($metadataName == 'title') {
234
                        // Get title of parent document if needed.
235
                        if (empty($metadataValue) && $this->settings['getTitle'] && $this->doc->parentId) {
236
                            $superiorTitle = Document::getTitle($this->doc->parentId, true);
237
                            if (!empty($superiorTitle)) {
238
                                $metadataArray[$i][$metadataName] = ['[' . $superiorTitle . ']'];
239
                            }
240
                        }
241
                        if (!empty($metadataValue)) {
242
                            $metadataArray[$i][$metadataName][0] = htmlspecialchars($metadataArray[$i][$metadataName][0]);
243
                            // Link title to pageview.
244
                            if ($this->settings['linkTitle'] && $metadataSection['_id']) {
245
                                $details = $this->doc->getLogicalStructure($metadataSection['_id']);
246
                                $buildUrl[$i][$metadataName]['buildUrl'] = [
247
                                    'id' => $this->doc->uid,
248
                                    'page' => (!empty($details['points']) ? intval($details['points']) : 1),
249
                                    'targetPid' => (!empty($this->settings['targetPid']) ? $this->settings['targetPid'] : 0)
250
                                ];
251
                            }
252
                        }
253
                    } elseif ($metadataName == 'owner' && !empty($metadataValue)) {
254
                        // Translate name of holding library.
255
                        $metadataArray[$i][$metadataName][0] = htmlspecialchars(Helper::translate($metadataArray[$i][$metadataName][0], 'tx_dlf_libraries', $this->settings['pages']));
256
                    } elseif ($metadataName == 'type' && !empty($metadataValue)) {
257
                        // Translate document type.
258
                        $metadataArray[$i][$metadataName][0] = htmlspecialchars(Helper::translate($metadataArray[$i][$metadataName][0], 'tx_dlf_structures', $this->settings['pages']));
259
                    } elseif ($metadataName == 'collection' && !empty($metadataValue)) {
260
                        // Check if collections isn't hidden.
261
                        $j = 0;
262
                        foreach ($metadataValue as $metadataEntry) {
263
                            if (in_array($metadataEntry, $collList)) {
264
                                // Translate collection.
265
                                $metadataArray[$i][$metadataName][$j] = htmlspecialchars(Helper::translate($metadataArray[$i][$metadataName][$j], 'tx_dlf_collections', $this->settings['pages']));
266
                            } else {
267
                                $metadataArray[$i][$metadataName][$j] = '';
268
                            }
269
                            $j++;
270
                        }
271
                    } elseif ($metadataName == 'language' && !empty($metadataValue)) {
272
                        // Translate ISO 639 language code.
273
                        $metadataArray[$i][$metadataName][0] = htmlspecialchars(Helper::getLanguageName($metadataArray[$i][$metadataName][0]));
274
                    } elseif (!empty($metadataValue)) {
275
                        // Sanitize value for output.
276
                        $metadataArray[$i][$metadataName][0] = htmlspecialchars($metadataArray[$i][$metadataName][0]);
277
                    }
278
                }
279
                $i++;
280
            }
281
282
            $this->view->assign('buildUrl', $buildUrl);
283
            $this->view->assign('documentMetadataSections', $metadataArray);
284
            $this->view->assign('configMetadata', $metadataResult);
285
            $this->view->assign('separator', $this->settings['separator']);
286
287
        }
288
    }
289
290
    // TODO: Needs to be placed in an abstract class (like before in AbstractPlugin)
291
    /**
292
     * Parses a string into a Typoscript array
293
     *
294
     * @access protected
295
     *
296
     * @param string $string: The string to parse
297
     *
298
     * @return array The resulting typoscript array
299
     */
300
    protected function parseTS($string = '')
301
    {
302
        $parser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class);
303
        $parser->parse($string);
304
        return $parser->setup;
305
    }
306
307
    protected function pi_getLL($label)
308
    {
309
        return $GLOBALS['TSFE']->sL('LLL:EXT:dlf/Resources/Private/Language/Metadata.xml:' . $label);
310
    }
311
312
}
313