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 (#878)
by Beatrycze
03:37
created

DocumentController::getUrlTemplate()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 62
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 62
rs 8.4746

How to fix   Long Method   

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
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Controller;
14
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Core\Utility\MathUtility;
17
18
/**
19
 * Provide document JSON for client side access
20
 *
21
 * @package TYPO3
22
 * @subpackage dlf
23
 * @access public
24
 */
25
class DocumentController extends AbstractController
26
{
27
   /**
28
     * The main method of the PlugIn
29
     *
30
     * @access public
31
     *
32
     * @param string $content: The PlugIn content
33
     * @param array $conf: The PlugIn configuration
34
     *
35
     * @return string The content that is displayed on the website
36
     */
37
    public function mainAction()
38
    {
39
        // Load current document.
40
        $this->loadDocument($this->requestData);
41
        if (
42
            $this->document === null
43
            || $this->document->getDoc() === null
44
            || $this->document->getDoc()->numPages < 1
45
        ) {
46
            // Quit without doing anything if required variables are not set.
47
            return;
48
        } else {
49
            if (!empty($this->requestData['logicalPage'])) {
50
                $this->requestData['page'] = $this->document->getDoc()->getPhysicalPage($this->requestData['logicalPage']);
51
                // The logical page parameter should not appear again
52
                unset($this->requestData['logicalPage']);
53
            }
54
            // Set default values if not set.
55
            // $this->requestData['page'] may be integer or string (physical structure @ID)
56
            if ((int) $this->requestData['page'] > 0 || empty($this->requestData['page'])) {
57
                $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getDoc()->numPages, 1);
58
            } else {
59
                $this->requestData['page'] = array_search($this->requestData['page'], $this->document->getDoc()->physicalStructure);
60
            }
61
            $this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'], 0, 1, 0);
62
        }
63
64
        $doc = $this->document->getDoc();
0 ignored issues
show
Unused Code introduced by
The assignment to $doc is dead and can be removed.
Loading history...
65
66
        if (!empty($this->settings['targetPidMetadata'])) {
67
            $metadataUrl = $this->uriBuilder
68
                ->reset()
69
                ->setTargetPageUid((int) $this->settings['targetPidMetadata'])
70
                ->setCreateAbsoluteUri(true)
71
                ->setArguments([
72
                    'tx_dlf' => [
73
                        'id' => $this->requestData['id'],
74
                    ],
75
                ])
76
                ->build();
77
        }
78
79
        $imageFileGroups = array_reverse(GeneralUtility::trimExplode(',', $this->extConf['fileGrpImages']));
80
        $fulltextFileGroups = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
81
        $config = [
82
            'forceAbsoluteUrl' => !empty($this->settings['forceAbsoluteUrl']),
83
            'proxyFileGroups' => !empty($this->settings['useInternalProxy'])
84
                ? array_merge($imageFileGroups, $fulltextFileGroups)
85
                : [],
86
        ];
87
        $tx_dlf_loaded = [
88
            'state' => [
89
                'documentId' => $this->requestData['id'],
90
                'page' => $this->requestData['page'],
91
                'simultaneousPages' => (int) $this->requestData['double'] + 1,
92
            ],
93
            'urlTemplate' => $this->getUrlTemplate(),
94
            'metadataUrl' => $metadataUrl,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $metadataUrl does not seem to be defined for all execution paths leading up to this point.
Loading history...
95
            'fileGroups' => [
96
                'images' => $imageFileGroups,
97
                'fulltext' => $fulltextFileGroups,
98
                'download' => GeneralUtility::trimExplode(',', $this->extConf['fileGrpDownload']),
99
            ],
100
            'document' => $this->document->getDoc()->toArray($this->uriBuilder, $config),
101
        ];
102
103
        $docConfiguration = '
104
            window.addEventListener("DOMContentLoaded", function() {
105
                const tx_dlf_loaded = ' . json_encode($tx_dlf_loaded) . ';
106
                window.dispatchEvent(new CustomEvent("tx-dlf-documentLoaded", {
107
                    detail: {
108
                        docController: new dlfController(tx_dlf_loaded)
109
                    }
110
                }));
111
            });';
112
113
        $this->view->assign('docConfiguration', $docConfiguration);
114
    }
115
116
    /**
117
     * Get URL template with the following placeholders:
118
     *
119
     * * `PAGE_NO` (for value of `tx_dlf[page]`)
120
     * * `DOUBLE_PAGE` (for value of `tx_dlf[double]`)
121
     * * `PAGE_GRID` (for value of `tx_dlf[pagegrid]`)
122
     *
123
     * @return string
124
     */
125
    protected function getUrlTemplate()
126
    {
127
        // Should work for route enhancers like this:
128
        //
129
        //   routeEnhancers:
130
        //     KitodoWorkview:
131
        //     type: Plugin
132
        //     namespace: tx_dlf
133
        //     routePath: '/{page}/{double}'
134
        //     requirements:
135
        //       page: \d+
136
        //       double: 0|1
137
138
        $make = function ($page, $double, $pagegrid) {
139
            $result = $this->uriBuilder->reset()
140
                ->setTargetPageUid($GLOBALS['TSFE']->id)
141
                ->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? true : false)
142
                ->setArguments([
143
                    'tx_dlf' => array_merge($this->requestData, [
144
                        'page' => $page,
145
                        'double' => $double,
146
                        'pagegrid' => $pagegrid
147
                    ]),
148
                ])
149
                ->build();
150
151
            $cHashIdx = strpos($result, '&cHash=');
152
            if ($cHashIdx !== false) {
153
                $result = substr($result, 0, $cHashIdx);
154
            }
155
156
            return $result;
157
        };
158
159
        // Generate two URLs that differ in tx_dlf[page], tx_dlf[double] and tx_dlf[highlight].
160
        // We don't know the order of these parameters, so use the values for matching.
161
        $a = $make(2, 1, 0);
162
        $b = $make(3, 0, 1);
163
164
        $lastIdx = 0;
165
        $result = '';
166
        for ($i = 0, $len = strlen($a); $i < $len; $i++) {
167
            if ($a[$i] === $b[$i]) {
168
                continue;
169
            }
170
171
            $result .= substr($a, $lastIdx, $i - $lastIdx);
172
            $lastIdx = $i + 1;
173
174
            if ($a[$i] === '2') {
175
                $placeholder = 'PAGE_NO';
176
            } else if ($a[$i] === '1') {
177
                $placeholder = 'DOUBLE_PAGE';
178
            } else {
179
                $placeholder = 'PAGE_GRID';
180
            }
181
182
            $result .= $placeholder;
183
        }
184
        $result .= substr($a, $lastIdx);
185
186
        return $result;
187
    }
188
189
}
190