| Total Complexity | 50 |
| Total Lines | 343 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ViewModuleController 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 ViewModuleController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class ViewModuleController |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * ModuleTemplate object |
||
| 51 | * |
||
| 52 | * @var ModuleTemplate |
||
| 53 | */ |
||
| 54 | protected $moduleTemplate; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * View |
||
| 58 | * |
||
| 59 | * @var ViewInterface |
||
| 60 | */ |
||
| 61 | protected $view; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Initialize module template and language service |
||
| 65 | */ |
||
| 66 | public function __construct() |
||
| 67 | { |
||
| 68 | $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class); |
||
| 69 | $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf'); |
||
| 70 | $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class); |
||
| 71 | $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf'); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Initialize view |
||
| 76 | * |
||
| 77 | * @param string $templateName |
||
| 78 | */ |
||
| 79 | protected function initializeView(string $templateName) |
||
| 80 | { |
||
| 81 | $this->view = GeneralUtility::makeInstance(StandaloneView::class); |
||
| 82 | $this->view->getRequest()->setControllerExtensionName('Viewpage'); |
||
| 83 | $this->view->setTemplate($templateName); |
||
| 84 | $this->view->setTemplateRootPaths(['EXT:viewpage/Resources/Private/Templates/ViewModule']); |
||
| 85 | $this->view->setPartialRootPaths(['EXT:viewpage/Resources/Private/Partials']); |
||
| 86 | $this->view->setLayoutRootPaths(['EXT:viewpage/Resources/Private/Layouts']); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Registers the docheader |
||
| 91 | * |
||
| 92 | * @param int $pageId |
||
| 93 | * @param int $languageId |
||
| 94 | * @param string $targetUrl |
||
| 95 | */ |
||
| 96 | protected function registerDocHeader(int $pageId, int $languageId, string $targetUrl) |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Show selected page from pagetree in iframe |
||
| 151 | * |
||
| 152 | * @param ServerRequestInterface $request |
||
| 153 | * @return ResponseInterface |
||
| 154 | * @throws \TYPO3\CMS\Core\Exception |
||
| 155 | */ |
||
| 156 | public function showAction(ServerRequestInterface $request): ResponseInterface |
||
| 157 | { |
||
| 158 | $pageId = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0); |
||
| 159 | |||
| 160 | $this->initializeView('show'); |
||
| 161 | $this->moduleTemplate->setBodyTag('<body class="typo3-module-viewpage">'); |
||
| 162 | $this->moduleTemplate->setModuleName('typo3-module-viewpage'); |
||
| 163 | $this->moduleTemplate->setModuleId('typo3-module-viewpage'); |
||
| 164 | |||
| 165 | if (!$this->isValidDoktype($pageId)) { |
||
| 166 | $flashMessage = GeneralUtility::makeInstance( |
||
| 167 | FlashMessage::class, |
||
| 168 | $this->getLanguageService()->getLL('noValidPageSelected'), |
||
| 169 | '', |
||
| 170 | FlashMessage::INFO |
||
| 171 | ); |
||
| 172 | return $this->renderFlashMessage($flashMessage); |
||
| 173 | } |
||
| 174 | |||
| 175 | $languageId = $this->getCurrentLanguage($pageId, $request->getParsedBody()['language'] ?? $request->getQueryParams()['language'] ?? null); |
||
| 176 | try { |
||
| 177 | $targetUrl = BackendUtility::getPreviewUrl( |
||
| 178 | $pageId, |
||
| 179 | '', |
||
| 180 | null, |
||
| 181 | '', |
||
| 182 | '', |
||
| 183 | $this->getTypeParameterIfSet($pageId) . '&L=' . $languageId |
||
| 184 | ); |
||
| 185 | } catch (UnableToLinkToPageException $e) { |
||
| 186 | $flashMessage = GeneralUtility::makeInstance( |
||
| 187 | FlashMessage::class, |
||
| 188 | $this->getLanguageService()->getLL('noSiteConfiguration'), |
||
| 189 | '', |
||
| 190 | FlashMessage::WARNING |
||
| 191 | ); |
||
| 192 | return $this->renderFlashMessage($flashMessage); |
||
| 193 | } |
||
| 194 | |||
| 195 | $this->registerDocHeader($pageId, $languageId, $targetUrl); |
||
| 196 | |||
| 197 | $iconFactory = GeneralUtility::makeInstance(IconFactory::class); |
||
| 198 | $icons = []; |
||
| 199 | $icons['orientation'] = $iconFactory->getIcon('actions-device-orientation-change', Icon::SIZE_SMALL)->render('inline'); |
||
| 200 | $icons['fullscreen'] = $iconFactory->getIcon('actions-fullscreen', Icon::SIZE_SMALL)->render('inline'); |
||
| 201 | $icons['expand'] = $iconFactory->getIcon('actions-expand', Icon::SIZE_SMALL)->render('inline'); |
||
| 202 | $icons['desktop'] = $iconFactory->getIcon('actions-device-desktop', Icon::SIZE_SMALL)->render('inline'); |
||
| 203 | $icons['tablet'] = $iconFactory->getIcon('actions-device-tablet', Icon::SIZE_SMALL)->render('inline'); |
||
| 204 | $icons['mobile'] = $iconFactory->getIcon('actions-device-mobile', Icon::SIZE_SMALL)->render('inline'); |
||
| 205 | $icons['unidentified'] = $iconFactory->getIcon('actions-device-unidentified', Icon::SIZE_SMALL)->render('inline'); |
||
| 206 | |||
| 207 | $current = ($this->getBackendUser()->uc['moduleData']['web_view']['States']['current'] ?: []); |
||
| 208 | $current['label'] = ($current['label'] ?? $this->getLanguageService()->sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:custom')); |
||
| 209 | $current['width'] = (isset($current['width']) && (int)$current['width'] >= 300 ? (int)$current['width'] : 320); |
||
| 210 | $current['height'] = (isset($current['height']) && (int)$current['height'] >= 300 ? (int)$current['height'] : 480); |
||
| 211 | |||
| 212 | $custom = ($this->getBackendUser()->uc['moduleData']['web_view']['States']['custom'] ?: []); |
||
| 213 | $custom['width'] = (isset($current['custom']) && (int)$current['custom'] >= 300 ? (int)$current['custom'] : 320); |
||
| 214 | $custom['height'] = (isset($current['custom']) && (int)$current['custom'] >= 300 ? (int)$current['custom'] : 480); |
||
| 215 | |||
| 216 | $this->view->assign('icons', $icons); |
||
| 217 | $this->view->assign('current', $current); |
||
| 218 | $this->view->assign('custom', $custom); |
||
| 219 | $this->view->assign('presetGroups', $this->getPreviewPresets($pageId)); |
||
| 220 | $this->view->assign('url', $targetUrl); |
||
| 221 | |||
| 222 | $this->moduleTemplate->setContent($this->view->render()); |
||
| 223 | return new HtmlResponse($this->moduleTemplate->renderContent()); |
||
| 224 | } |
||
| 225 | |||
| 226 | protected function renderFlashMessage(FlashMessage $flashMessage): HtmlResponse |
||
| 227 | { |
||
| 228 | $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); |
||
| 229 | $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier(); |
||
| 230 | $defaultFlashMessageQueue->enqueue($flashMessage); |
||
| 231 | |||
| 232 | $this->moduleTemplate->setContent($this->view->render()); |
||
| 233 | return new HtmlResponse($this->moduleTemplate->renderContent()); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * With page TS config it is possible to force a specific type id via mod.web_view.type |
||
| 238 | * for a page id or a page tree. |
||
| 239 | * The method checks if a type is set for the given id and returns the additional GET string. |
||
| 240 | * |
||
| 241 | * @param int $pageId |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | protected function getTypeParameterIfSet(int $pageId): string |
||
| 245 | { |
||
| 246 | $typeParameter = ''; |
||
| 247 | $typeId = (int)(BackendUtility::getPagesTSconfig($pageId)['mod.']['web_view.']['type'] ?? 0); |
||
| 248 | if ($typeId > 0) { |
||
| 249 | $typeParameter = '&type=' . $typeId; |
||
| 250 | } |
||
| 251 | return $typeParameter; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get available presets for page id |
||
| 256 | * |
||
| 257 | * @param int $pageId |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | protected function getPreviewPresets(int $pageId): array |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the preview languages |
||
| 299 | * |
||
| 300 | * @param int $pageId |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | protected function getPreviewLanguages(int $pageId): array |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the current language |
||
| 332 | * |
||
| 333 | * @param int $pageId |
||
| 334 | * @param string $languageParam |
||
| 335 | * @return int |
||
| 336 | */ |
||
| 337 | protected function getCurrentLanguage(int $pageId, string $languageParam = null): int |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Verifies if doktype of given page is valid |
||
| 355 | * |
||
| 356 | * @param int $pageId |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | protected function isValidDoktype(int $pageId = 0): bool |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return BackendUserAuthentication |
||
| 378 | */ |
||
| 379 | protected function getBackendUser(): BackendUserAuthentication |
||
| 380 | { |
||
| 381 | return $GLOBALS['BE_USER']; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return LanguageService |
||
| 386 | */ |
||
| 387 | protected function getLanguageService(): LanguageService |
||
| 390 | } |
||
| 391 | } |
||
| 392 |