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
Push — master ( 35f42b...9bfc78 )
by Sebastian
02:52
created

Metadata::main()   D

Complexity

Conditions 20
Paths 113

Size

Total Lines 72
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 46
nc 113
nop 2
dl 0
loc 72
rs 4.0583
c 0
b 0
f 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
namespace Kitodo\Dlf\Plugin;
3
4
/**
5
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
6
 *
7
 * This file is part of the Kitodo and TYPO3 projects.
8
 *
9
 * @license GNU General Public License version 3 or later.
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 */
13
14
use Kitodo\Dlf\Common\Document;
15
use Kitodo\Dlf\Common\Helper;
16
17
/**
18
 * Plugin 'Metadata' for the 'dlf' extension
19
 *
20
 * @author Sebastian Meyer <[email protected]>
21
 * @author Siegfried Schweizer <[email protected]>
22
 * @package TYPO3
23
 * @subpackage dlf
24
 * @access public
25
 */
26
class Metadata extends \Kitodo\Dlf\Common\AbstractPlugin {
27
    public $scriptRelPath = 'Classes/Plugin/Metadata.php';
28
29
    /**
30
     * This holds the hook objects
31
     *
32
     * @var array
33
     * @access protected
34
     */
35
    protected $hookObjects = [];
36
37
    /**
38
     * The main method of the PlugIn
39
     *
40
     * @access public
41
     *
42
     * @param string $content: The PlugIn content
43
     * @param array $conf: The PlugIn configuration
44
     *
45
     * @return string The content that is displayed on the website
46
     */
47
    public function main($content, $conf) {
48
        $this->init($conf);
49
        // Turn cache on.
50
        $this->setCache(TRUE);
51
        // Load current document.
52
        $this->loadDocument();
53
        if ($this->doc === NULL) {
54
            // Quit without doing anything if required variables are not set.
55
            return $content;
56
        } else {
57
            // Set default values if not set.
58
            if (!isset($this->conf['rootline'])) {
59
                $this->conf['rootline'] = 0;
60
            }
61
        }
62
        $metadata = [];
63
        if ($this->conf['rootline'] < 2) {
64
            // Get current structure's @ID.
65
            $ids = [];
66
            if (!empty($this->doc->physicalStructure[$this->piVars['page']]) && !empty($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]])) {
0 ignored issues
show
Bug Best Practice introduced by
The property $smLinks is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
Bug Best Practice introduced by
The property $physicalStructure is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
67
                foreach ($this->doc->smLinks['p2l'][$this->doc->physicalStructure[$this->piVars['page']]] as $logId) {
68
                    $count = count($this->doc->mets->xpath('./mets:structMap[@TYPE="LOGICAL"]//mets:div[@ID="'.$logId.'"]/ancestor::*'));
0 ignored issues
show
Bug Best Practice introduced by
The property $mets is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
69
                    $ids[$count][] = $logId;
70
                }
71
            }
72
            ksort($ids);
73
            reset($ids);
74
            // Check if we should display all metadata up to the root.
75
            if ($this->conf['rootline'] == 1) {
76
                foreach ($ids as $id) {
77
                    foreach ($id as $sid) {
78
                        $data = $this->doc->getMetadata($sid, $this->conf['pages']);
79
                        if (!empty($data)) {
80
                            $data['_id'] = $sid;
81
                            $metadata[] = $data;
82
                        }
83
                    }
84
                }
85
            } else {
86
                $id = array_pop($ids);
87
                if (is_array($id)) {
88
                    foreach ($id as $sid) {
89
                        $data = $this->doc->getMetadata($sid, $this->conf['pages']);
90
                        if (!empty($data)) {
91
                            $data['_id'] = $sid;
92
                            $metadata[] = $data;
93
                        }
94
                    }
95
                }
96
            }
97
        }
98
        // Get titledata?
99
        if (empty($metadata) || ($this->conf['rootline'] == 1 && $metadata[0]['_id'] != $this->doc->toplevelId)) {
0 ignored issues
show
Bug Best Practice introduced by
The property $toplevelId is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
100
            $data = $this->doc->getTitleData($this->conf['pages']);
101
            $data['_id'] = $this->doc->toplevelId;
102
            array_unshift($metadata, $data);
103
        }
104
        if (empty($metadata)) {
105
            Helper::devLog('No metadata found for document with UID '.$this->doc->uid, DEVLOG_SEVERITY_WARNING);
0 ignored issues
show
Bug Best Practice introduced by
The property $uid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
106
            return $content;
107
        }
108
        ksort($metadata);
109
        // Get hook objects.
110
        $this->hookObjects = Helper::getHookObjects($this->scriptRelPath);
111
        // Hook for getting a customized title bar (requested by SBB).
112
        foreach ($this->hookObjects as $hookObj) {
113
            if (method_exists($hookObj, 'main_customizeTitleBarGetCustomTemplate')) {
114
                $hookObj->main_customizeTitleBarGetCustomTemplate($this, $metadata);
115
            }
116
        }
117
        $content .= $this->printMetadata($metadata);
118
        return $this->pi_wrapInBaseClass($content);
119
    }
120
121
    /**
122
     * Prepares the metadata array for output
123
     *
124
     * @access protected
125
     *
126
     * @param array $metadataArray: The metadata array
127
     *
128
     * @return string The metadata array ready for output
129
     */
130
    protected function printMetadata(array $metadataArray) {
131
        // Load template file.
132
        $this->getTemplate();
133
        $output = '';
134
        $subpart['block'] = $this->cObj->getSubpart($this->template, '###BLOCK###');
1 ignored issue
show
Comprehensibility Best Practice introduced by
$subpart was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subpart = array(); before regardless.
Loading history...
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...tRenderer::getSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

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

134
        $subpart['block'] = /** @scrutinizer ignore-deprecated */ $this->cObj->getSubpart($this->template, '###BLOCK###');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
135
        // Get list of metadata to show.
136
        $metaList = [];
137
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
138
            'tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.wrap AS wrap,tx_dlf_metadata.sys_language_uid AS sys_language_uid',
139
            'tx_dlf_metadata',
140
            'tx_dlf_metadata.pid='.intval($this->conf['pages'])
141
                .' AND (sys_language_uid IN (-1,0) OR (sys_language_uid = '.$GLOBALS['TSFE']->sys_language_uid.' AND l18n_parent = 0))'
142
                .Helper::whereClause('tx_dlf_metadata'),
143
            '',
144
            'tx_dlf_metadata.sorting',
145
            ''
146
        );
147
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
148
            if (is_array($resArray) && $resArray['sys_language_uid'] != $GLOBALS['TSFE']->sys_language_content && $GLOBALS['TSFE']->sys_language_contentOL) {
149
                $resArray = $GLOBALS['TSFE']->sys_page->getRecordOverlay('tx_dlf_metadata', $resArray, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
150
            }
151
            if ($resArray) {
152
                if ($this->conf['showFull'] || $resArray['is_listed']) {
153
                    $metaList[$resArray['index_name']] = [
154
                        'wrap' => $resArray['wrap'],
155
                        'label' => Helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages'])
156
                    ];
157
                }
158
            }
159
        }
160
        // Get list of collections to show.
161
        $collList = [];
162
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
163
            'tx_dlf_collections.index_name AS index_name',
164
            'tx_dlf_collections',
165
            'tx_dlf_collections.pid='.intval($this->conf['pages'])
166
                .Helper::whereClause('tx_dlf_collections'),
167
            '',
168
            '',
169
            ''
170
        );
171
        while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
172
            $collList[] = $resArray['index_name'];
173
        }
174
        // Save original data array.
175
        $cObjData = $this->cObj->data;
176
        // Parse the metadata arrays.
177
        foreach ($metadataArray as $metadata) {
178
            $markerArray['###METADATA###'] = '';
179
            // Reset content object's data array.
180
            $this->cObj->data = $cObjData;
181
            // Load all the metadata values into the content object's data array.
182
            foreach ($metadata as $index_name => $value) {
183
                if (is_array($value)) {
184
                    $this->cObj->data[$index_name] = implode($this->conf['separator'], $value);
185
                } else {
186
                    $this->cObj->data[$index_name] = $value;
187
                }
188
            }
189
            // Process each metadate.
190
            foreach ($metaList as $index_name => $metaConf) {
191
                if (!empty($metadata[$index_name])) {
192
                    $parsedValue = '';
193
                    $fieldwrap = $this->parseTS($metaConf['wrap']);
194
                    do {
195
                        $value = @array_shift($metadata[$index_name]);
196
                        if ($index_name == 'title') {
197
                            // Get title of parent document if needed.
198
                            if (empty($value) && $this->conf['getTitle'] && $this->doc->parentId) {
0 ignored issues
show
Bug Best Practice introduced by
The property $parentId is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
199
                                $superiorTitle = Document::getTitle($this->doc->parentId, TRUE);
200
                                if (!empty($superiorTitle)) {
201
                                    $value = '['.$superiorTitle.']';
202
                                }
203
                            }
204
                            if (!empty($value)) {
205
                                $value = htmlspecialchars($value);
206
                                // Link title to pageview.
207
                                if ($this->conf['linkTitle'] && $metadata['_id']) {
208
                                    $details = $this->doc->getLogicalStructure($metadata['_id']);
209
                                    $value = $this->pi_linkTP($value, [$this->prefixId => ['id' => $this->doc->uid, 'page' => (!empty($details['points']) ? intval($details['points']) : 1)]], TRUE, $this->conf['targetPid']);
0 ignored issues
show
Bug Best Practice introduced by
The property $uid is declared protected in Kitodo\Dlf\Common\Document. Since you implement __get, consider adding a @property or @property-read.
Loading history...
210
                                }
211
                            }
212
                        } elseif ($index_name == 'owner' && !empty($value)) {
213
                            // Translate name of holding library.
214
                            $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
215
                        } elseif ($index_name == 'type' && !empty($value)) {
216
                            // Translate document type.
217
                            $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
218
                        } elseif ($index_name == 'collection' && !empty($value)) {
219
                            // Check if collections isn't hidden.
220
                            if (in_array($value, $collList)) {
221
                                // Translate collection.
222
                                $value = htmlspecialchars(Helper::translate($value, 'tx_dlf_collections', $this->conf['pages']));
223
                            } else {
224
                                $value = '';
225
                            }
226
                        } elseif ($index_name == 'language' && !empty($value)) {
227
                            // Translate ISO 639 language code.
228
                            $value = htmlspecialchars(Helper::getLanguageName($value));
229
                        } elseif (!empty($value)) {
230
                            // Sanitize value for output.
231
                            $value = htmlspecialchars($value);
232
                        }
233
                        // Hook for getting a customized value (requested by SBB).
234
                        foreach ($this->hookObjects as $hookObj) {
235
                            if (method_exists($hookObj, 'printMetadata_customizeMetadata')) {
236
                                $hookObj->printMetadata_customizeMetadata($value);
237
                            }
238
                        }
239
                        $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
240
                        if (!empty($value)) {
241
                            $parsedValue .= $value;
242
                        }
243
                    } while (count($metadata[$index_name]));
244
                    if (!empty($parsedValue)) {
245
                        $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
246
                        $field .= $parsedValue;
247
                        $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
248
                    }
249
                }
250
            }
251
            $output .= $this->cObj->substituteMarkerArray($subpart['block'], $markerArray);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...substituteMarkerArray() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

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

251
            $output .= /** @scrutinizer ignore-deprecated */ $this->cObj->substituteMarkerArray($subpart['block'], $markerArray);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
252
        }
253
        return $this->cObj->substituteSubpart($this->template, '###BLOCK###', $output, TRUE);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Frontend\Conte...er::substituteSubpart() has been deprecated: since TYPO3 v8, will be removed in TYPO3 v9, please use the MarkerBasedTemplateService instead. ( Ignorable by Annotation )

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

253
        return /** @scrutinizer ignore-deprecated */ $this->cObj->substituteSubpart($this->template, '###BLOCK###', $output, TRUE);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
254
    }
255
}
256