We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 74 |
Total Lines | 556 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractController 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 AbstractController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class AbstractController extends ActionController implements LoggerAwareInterface |
||
39 | { |
||
40 | use LoggerAwareTrait; |
||
41 | |||
42 | /** |
||
43 | * @access protected |
||
44 | * @var DocumentRepository |
||
45 | */ |
||
46 | protected DocumentRepository $documentRepository; |
||
47 | |||
48 | /** |
||
49 | * @access public |
||
50 | * |
||
51 | * @param DocumentRepository $documentRepository |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function injectDocumentRepository(DocumentRepository $documentRepository): void |
||
56 | { |
||
57 | $this->documentRepository = $documentRepository; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @access protected |
||
62 | * @var Document|null This holds the current document |
||
63 | */ |
||
64 | protected ?Document $document = null; |
||
65 | |||
66 | /** |
||
67 | * @access protected |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $documentArray; |
||
71 | |||
72 | /** |
||
73 | * @access protected |
||
74 | * @var array |
||
75 | */ |
||
76 | protected array $extConf; |
||
77 | |||
78 | /** |
||
79 | * @access protected |
||
80 | * @var array This holds the request parameter |
||
81 | */ |
||
82 | protected array $requestData; |
||
83 | |||
84 | /** |
||
85 | * @access protected |
||
86 | * @var array This holds some common data for the fluid view |
||
87 | */ |
||
88 | protected array $viewData; |
||
89 | |||
90 | /** |
||
91 | * @access protected |
||
92 | * @var int |
||
93 | */ |
||
94 | protected int $pageUid; |
||
95 | |||
96 | /** |
||
97 | * Initialize the plugin controller |
||
98 | * |
||
99 | * @access protected |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | protected function initialize(): void |
||
104 | { |
||
105 | $this->requestData = GeneralUtility::_GPmerged('tx_dlf'); |
||
106 | $this->pageUid = (int) GeneralUtility::_GET('id'); |
||
107 | |||
108 | // Sanitize user input to prevent XSS attacks. |
||
109 | $this->sanitizeRequestData(); |
||
110 | |||
111 | // Get extension configuration. |
||
112 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
||
113 | |||
114 | $this->viewData = [ |
||
115 | 'pageUid' => $this->pageUid, |
||
116 | 'uniqueId' => uniqid(), |
||
117 | 'requestData' => $this->requestData |
||
118 | ]; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Loads the current document into $this->document |
||
123 | * |
||
124 | * @access protected |
||
125 | * |
||
126 | * @param int $documentId The document's UID (fallback: $this->requestData[id]) |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | protected function loadDocument(int $documentId = 0): void |
||
131 | { |
||
132 | // Get document ID from request data if not passed as parameter. |
||
133 | if ($documentId === 0 && !empty($this->requestData['id'])) { |
||
134 | $documentId = $this->requestData['id']; |
||
135 | } |
||
136 | |||
137 | // Try to get document format from database |
||
138 | if (!empty($documentId)) { |
||
139 | |||
140 | $doc = null; |
||
141 | |||
142 | if (MathUtility::canBeInterpretedAsInteger($documentId)) { |
||
143 | $doc = $this->getDocumentByUid($documentId); |
||
144 | } elseif (GeneralUtility::isValidUrl($documentId)) { |
||
145 | $doc = $this->getDocumentByUrl($documentId); |
||
146 | } |
||
147 | |||
148 | if ($this->document !== null && $doc !== null) { |
||
149 | $this->document->setCurrentDocument($doc); |
||
150 | } |
||
151 | |||
152 | } elseif (!empty($this->requestData['recordId'])) { |
||
153 | |||
154 | $this->document = $this->documentRepository->findOneByRecordId($this->requestData['recordId']); |
||
|
|||
155 | |||
156 | if ($this->document !== null) { |
||
157 | $doc = AbstractDocument::getInstance($this->document->getLocation(), $this->settings, true); |
||
158 | if ($doc !== null) { |
||
159 | $this->document->setCurrentDocument($doc); |
||
160 | } else { |
||
161 | $this->logger->error('Failed to load document with record ID "' . $this->requestData['recordId'] . '"'); |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | $this->logger->error('Invalid ID "' . $documentId . '" or PID "' . $this->settings['storagePid'] . '" for document loading'); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Configure URL for proxy. |
||
171 | * |
||
172 | * @access protected |
||
173 | * |
||
174 | * @param string $url URL for proxy configuration |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | protected function configureProxyUrl(string &$url): void |
||
179 | { |
||
180 | $this->uriBuilder->reset() |
||
181 | ->setTargetPageUid($this->pageUid) |
||
182 | ->setCreateAbsoluteUri(!empty($this->extConf['general']['forceAbsoluteUrl'])) |
||
183 | ->setArguments( |
||
184 | [ |
||
185 | 'eID' => 'tx_dlf_pageview_proxy', |
||
186 | 'url' => $url, |
||
187 | 'uHash' => GeneralUtility::hmac($url, 'PageViewProxy') |
||
188 | ] |
||
189 | ) |
||
190 | ->build(); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Checks if doc is missing or is empty (no pages) |
||
195 | * |
||
196 | * @access protected |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | protected function isDocMissingOrEmpty(): bool |
||
201 | { |
||
202 | $multiViewType = $this->settings['multiViewType'] ?? ''; |
||
203 | return $this->isDocMissing() || ($this->document->getCurrentDocument()->numPages < 1 && $this->document->getCurrentDocument()->tableOfContents[0]['type'] !== $multiViewType); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Checks if doc is missing |
||
208 | * |
||
209 | * @access protected |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | protected function isDocMissing(): bool |
||
214 | { |
||
215 | return $this->document === null || $this->document->getCurrentDocument() === null; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Returns the LanguageService |
||
220 | * |
||
221 | * @access protected |
||
222 | * |
||
223 | * @return LanguageService |
||
224 | */ |
||
225 | protected function getLanguageService(): LanguageService |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Safely gets Parameters from request if they exist |
||
232 | * |
||
233 | * @access protected |
||
234 | * |
||
235 | * @param string $parameterName |
||
236 | * |
||
237 | * @return null|string|array |
||
238 | */ |
||
239 | protected function getParametersSafely(string $parameterName) |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Sanitize input variables. |
||
249 | * |
||
250 | * @access protected |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | protected function sanitizeRequestData(): void |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Sanitize settings from FlexForm. |
||
281 | * |
||
282 | * @access protected |
||
283 | * |
||
284 | * @return void |
||
285 | */ |
||
286 | protected function sanitizeSettings(): void |
||
287 | { |
||
288 | $this->setDefaultIntSetting('storagePid', 0); |
||
289 | |||
290 | if ($this instanceof MetadataController) { |
||
291 | $this->setDefaultIntSetting('rootline', 0); |
||
292 | $this->setDefaultIntSetting('originalIiifMetadata', 0); |
||
293 | $this->setDefaultIntSetting('displayIiifDescription', 1); |
||
294 | $this->setDefaultIntSetting('displayIiifRights', 1); |
||
295 | $this->setDefaultIntSetting('displayIiifLinks', 1); |
||
296 | } |
||
297 | |||
298 | if ($this instanceof OaiPmhController) { |
||
299 | $this->setDefaultIntSetting('limit', 5); |
||
300 | $this->setDefaultIntSetting('solr_limit', 50000); |
||
301 | } |
||
302 | |||
303 | if ($this instanceof PageViewController) { |
||
304 | $this->setDefaultIntSetting('useInternalProxy', 0); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Sets default value for setting if not yet set. |
||
310 | * |
||
311 | * @access protected |
||
312 | * |
||
313 | * @param string $setting name of setting |
||
314 | * @param int $value for being set if empty |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | protected function setDefaultIntSetting(string $setting, int $value): void |
||
319 | { |
||
320 | if (empty($this->settings[$setting])) { |
||
321 | $this->settings[$setting] = $value; |
||
322 | $this->logger->warning('Setting "' . $setting . '" not set, using default value "' . $value . '". Probably FlexForm for controller "' . get_class($this) . '" is not read.'); |
||
323 | } else { |
||
324 | $this->settings[$setting] = (int) $this->settings[$setting]; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Sets page value. |
||
330 | * |
||
331 | * @access protected |
||
332 | * |
||
333 | * @return void |
||
334 | */ |
||
335 | protected function setPage(): void |
||
336 | { |
||
337 | if (!empty($this->requestData['logicalPage'])) { |
||
338 | $this->requestData['page'] = $this->document->getCurrentDocument()->getPhysicalPage($this->requestData['logicalPage']); |
||
339 | // The logical page parameter should not appear again |
||
340 | unset($this->requestData['logicalPage']); |
||
341 | } |
||
342 | |||
343 | $this->setDefaultPage(); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Sets default page value. |
||
348 | * |
||
349 | * @access protected |
||
350 | * |
||
351 | * @return void |
||
352 | */ |
||
353 | protected function setDefaultPage(): void |
||
354 | { |
||
355 | // Set default values if not set. |
||
356 | // $this->requestData['page'] may be integer or string (physical structure @ID) |
||
357 | if ( |
||
358 | (int) $this->requestData['page'] > 0 |
||
359 | || empty($this->requestData['page']) |
||
360 | || is_array($this->requestData['docPage']) |
||
361 | ) { |
||
362 | if (isset($this->settings['multiViewType']) && $this->document->getCurrentDocument()->tableOfContents[0]['type'] === $this->settings['multiViewType']) { |
||
363 | $i = 0; |
||
364 | foreach ($this->documentArray as $document) { |
||
365 | if ($document !== null) { |
||
366 | $this->requestData['docPage'][$i] = MathUtility::forceIntegerInRange((int) $this->requestData['docPage'][$i], 1, $document->numPages, 1); |
||
367 | $i++; |
||
368 | } |
||
369 | } |
||
370 | } else { |
||
371 | $this->requestData['page'] = MathUtility::forceIntegerInRange((int) $this->requestData['page'], 1, $this->document->getCurrentDocument()->numPages, 1); |
||
372 | } |
||
373 | } |
||
374 | // reassign viewData to get correct page |
||
375 | $this->viewData['requestData'] = $this->requestData; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * This is the constructor |
||
380 | * |
||
381 | * @access public |
||
382 | * |
||
383 | * @return void |
||
384 | */ |
||
385 | public function __construct() |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * build simple pagination |
||
392 | * |
||
393 | * @param PaginationInterface $pagination |
||
394 | * @param PaginatorInterface $paginator |
||
395 | * @return array |
||
396 | */ |
||
397 | //TODO: clean this function |
||
398 | protected function buildSimplePagination(PaginationInterface $pagination, PaginatorInterface $paginator): array |
||
508 | ]; |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Get document from repository by uid. |
||
513 | * |
||
514 | * @access private |
||
515 | * |
||
516 | * @param int $documentId The document's UID |
||
517 | * |
||
518 | * @return AbstractDocument |
||
519 | */ |
||
520 | private function getDocumentByUid(int $documentId) |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * Get document by URL. |
||
536 | * |
||
537 | * @access protected |
||
538 | * |
||
539 | * @param string $documentId The document's URL |
||
540 | * |
||
541 | * @return AbstractDocument |
||
542 | */ |
||
543 | protected function getDocumentByUrl(string $documentId) |
||
596 |