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:15
created

MetadataController::mainAction()   F

Complexity

Conditions 29
Paths 5377

Size

Total Lines 95
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 29
eloc 61
c 2
b 0
f 0
nc 5377
nop 0
dl 0
loc 95
rs 0

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

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