| Total Complexity | 67 |
| Total Lines | 559 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
Complex classes like WebController 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 WebController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class WebController extends Controller |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Provides access to the templating engine. |
||
| 17 | * @property object $twig the twig templating engine. |
||
| 18 | */ |
||
| 19 | public $twig; |
||
| 20 | public $honeypot; |
||
| 21 | public $translator; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor for the WebController. |
||
| 25 | * @param Model $model |
||
| 26 | */ |
||
| 27 | public function __construct($model) |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Find any custom templates from the given directory and return them as an array. |
||
| 81 | * @param string $dir path of custom templates directory |
||
| 82 | * @return array array of custom template filenames, keyed by slot |
||
| 83 | */ |
||
| 84 | public function findCustomTemplates($dir) |
||
| 85 | { |
||
| 86 | $customTemplateSubDirs = glob($dir . '/*', GLOB_ONLYDIR); |
||
| 87 | $customTemplates = []; |
||
| 88 | |||
| 89 | foreach ($customTemplateSubDirs as $slotDir) { |
||
| 90 | $slotName = basename($slotDir); |
||
| 91 | $files = glob($slotDir . '/*.twig'); |
||
| 92 | // Strip the directory part to make the file paths relative to the directory. |
||
| 93 | // The "custom-templates" directory is registered to the Twig FilesystemLoader. |
||
| 94 | $customTemplates[$slotName] = array_map(function ($file) use ($dir) { |
||
| 95 | return str_replace($dir . '/', '', $file); |
||
| 96 | }, $files); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $customTemplates; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Guess the language of the user. Return a language string that is one |
||
| 104 | * of the supported languages defined in the $LANGUAGES setting, e.g. "fi". |
||
| 105 | * @param Request $request HTTP request |
||
| 106 | * @param string $vocid identifier for the vocabulary eg. 'yso'. |
||
| 107 | * @return string returns the language choice as a numeric string value |
||
| 108 | */ |
||
| 109 | public function guessLanguage($request, $vocid = null) |
||
| 110 | { |
||
| 111 | // 1. select language based on SKOSMOS_LANGUAGE cookie |
||
| 112 | $languageCookie = $request->getCookie('SKOSMOS_LANGUAGE'); |
||
| 113 | if ($languageCookie) { |
||
| 114 | return $languageCookie; |
||
| 115 | } |
||
| 116 | |||
| 117 | // 2. if vocabulary given, select based on the default language of the vocabulary |
||
| 118 | if ($vocid !== null && $vocid !== '') { |
||
| 119 | try { |
||
| 120 | $vocab = $this->model->getVocabulary($vocid); |
||
| 121 | return $vocab->getConfig()->getDefaultLanguage(); |
||
| 122 | } catch (Exception $e) { |
||
| 123 | // vocabulary id not found, move on to the next selection method |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // 3. select language based on Accept-Language header |
||
| 128 | header('Vary: Accept-Language'); // inform caches that a decision was made based on Accept header |
||
| 129 | $this->negotiator = new \Negotiation\LanguageNegotiator(); |
||
| 130 | $langcodes = array_keys($this->languages); |
||
| 131 | // using a random language from the configured UI languages when there is no accept language header set |
||
| 132 | $acceptLanguage = $request->getServerConstant('HTTP_ACCEPT_LANGUAGE') ? $request->getServerConstant('HTTP_ACCEPT_LANGUAGE') : $langcodes[0]; |
||
| 133 | |||
| 134 | $bestLang = $this->negotiator->getBest($acceptLanguage, $langcodes); |
||
| 135 | if (isset($bestLang) && in_array($bestLang->getValue(), $langcodes)) { |
||
| 136 | return $bestLang->getValue(); |
||
| 137 | } |
||
| 138 | |||
| 139 | // show default site or prompt for language |
||
| 140 | return $langcodes[0]; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Determines a css class that controls width and positioning of the vocabulary list element. |
||
| 145 | * The layout is wider if the left/right box templates have not been provided. |
||
| 146 | * @return string css class for the container eg. 'voclist-wide' or 'voclist-right' |
||
| 147 | */ |
||
| 148 | private function listStyle() |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Renders the list of supported languages from vocabulary config in order. |
||
| 163 | * The ordering is done according to the language order parameter in vocabulary config if such exists |
||
| 164 | * @param Vocabulary $vocab the vocabulary object |
||
| 165 | * @return array with language codes as keys and language labels as values |
||
| 166 | */ |
||
| 167 | public function parseVocabularyLanguageOrder($vocab) |
||
| 168 | { |
||
| 169 | $vocabContentLanguages = array_flip($vocab->getConfig()->getLanguages()); |
||
| 170 | $languageOrder = $vocab->getConfig()->getLanguageOrder(); |
||
| 171 | |||
| 172 | $tmpList = []; |
||
| 173 | |||
| 174 | foreach ($languageOrder as $vocLang) { |
||
| 175 | if (isset($vocabContentLanguages[$vocLang])) { |
||
| 176 | $tmpList[$vocLang] = $vocabContentLanguages[$vocLang]; |
||
| 177 | unset($vocabContentLanguages[$vocLang]); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return $tmpList + $vocabContentLanguages; |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Loads and renders the landing page view. |
||
| 186 | * @param Request $request |
||
| 187 | */ |
||
| 188 | public function invokeLandingPage($request) |
||
| 189 | { |
||
| 190 | $this->model->setLocale($request->getLang()); |
||
| 191 | // load template |
||
| 192 | $template = $this->twig->load('landing.twig'); |
||
| 193 | // set template variables |
||
| 194 | $categoryLabel = $this->model->getClassificationLabel($request->getLang()); |
||
| 195 | $sortedVocabs = $this->model->getVocabularyList(false, true); |
||
| 196 | $contentLanguages = array_flip($this->model->getLanguages($request->getLang())); |
||
| 197 | $listStyle = $this->listStyle(); |
||
| 198 | |||
| 199 | // render template |
||
| 200 | echo $template->render( |
||
| 201 | array( |
||
| 202 | 'sorted_vocabs' => $sortedVocabs, |
||
| 203 | 'category_label' => $categoryLabel, |
||
| 204 | 'languages' => $this->languages, |
||
| 205 | 'content_languages' => $contentLanguages, |
||
| 206 | 'request' => $request, |
||
| 207 | 'list_style' => $listStyle |
||
| 208 | ) |
||
| 209 | ); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Invokes the concept page of a single concept in a specific vocabulary. |
||
| 214 | * |
||
| 215 | * @param Request $request |
||
| 216 | */ |
||
| 217 | public function invokeVocabularyConcept(Request $request) |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Invokes the feedback page with information of the users current vocabulary. |
||
| 258 | */ |
||
| 259 | public function invokeFeedbackForm($request) |
||
| 260 | { |
||
| 261 | $template = $this->twig->load('feedback.twig'); |
||
| 262 | $this->model->setLocale($request->getLang()); |
||
| 263 | $vocabList = $this->model->getVocabularyList(false); |
||
| 264 | $vocab = $request->getVocab(); |
||
| 265 | |||
| 266 | $feedbackSent = false; |
||
| 267 | if ($request->getQueryParamPOST('message')) { |
||
| 268 | $feedbackSent = true; |
||
| 269 | $feedbackMsg = $request->getQueryParamPOST('message'); |
||
| 270 | $feedbackName = $request->getQueryParamPOST('name'); |
||
| 271 | $feedbackEmail = $request->getQueryParamPOST('email'); |
||
| 272 | $msgSubject = $request->getQueryParamPOST('msgsubject'); |
||
| 273 | $feedbackVocab = $request->getQueryParamPOST('vocab'); |
||
| 274 | $feedbackVocabEmail = ($feedbackVocab !== null && $feedbackVocab !== '') ? |
||
| 275 | $this->model->getVocabulary($feedbackVocab)->getConfig()->getFeedbackRecipient() : null; |
||
| 276 | // if the hidden field has been set a value we have found a spam bot |
||
| 277 | // and we do not actually send the message. |
||
| 278 | if ($this->honeypot->validateHoneypot($request->getQueryParamPOST('item-description')) && |
||
| 279 | $this->honeypot->validateHoneytime($request->getQueryParamPOST('user-captcha'), $this->model->getConfig()->getHoneypotTime())) { |
||
| 280 | $this->sendFeedback($request, $feedbackMsg, $msgSubject, $feedbackName, $feedbackEmail, $feedbackVocab, $feedbackVocabEmail); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | echo $template->render( |
||
| 284 | array( |
||
| 285 | 'languages' => $this->languages, |
||
| 286 | 'vocab' => $vocab, |
||
| 287 | 'vocabList' => $vocabList, |
||
| 288 | 'feedback_sent' => $feedbackSent, |
||
| 289 | 'request' => $request, |
||
| 290 | ) |
||
| 291 | ); |
||
| 292 | } |
||
| 293 | |||
| 294 | private function createFeedbackHeaders($fromName, $fromEmail, $toMail, $sender) |
||
| 295 | { |
||
| 296 | $headers = "MIME-Version: 1.0" . "\r\n"; |
||
| 297 | $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n"; |
||
| 298 | if (!empty($toMail)) { |
||
| 299 | $headers .= "Cc: " . $this->model->getConfig()->getFeedbackAddress() . "\r\n"; |
||
| 300 | } |
||
| 301 | if (!empty($fromEmail)) { |
||
| 302 | $headers .= "Reply-To: $fromName <$fromEmail>\r\n"; |
||
| 303 | } |
||
| 304 | $service = $this->model->getConfig()->getServiceName(); |
||
| 305 | return $headers . "From: $fromName via $service <$sender>"; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Sends the user entered message through the php's mailer. |
||
| 310 | * @param string $message content given by user. |
||
| 311 | * @param string $messageSubject subject line given by user. |
||
| 312 | * @param string $fromName senders own name. |
||
| 313 | * @param string $fromEmail senders email address. |
||
| 314 | * @param string $fromVocab which vocabulary is the feedback related to. |
||
| 315 | */ |
||
| 316 | public function sendFeedback($request, $message, $messageSubject, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null) |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Invokes the about page for the Skosmos service. |
||
| 366 | */ |
||
| 367 | public function invokeAboutPage($request) |
||
| 378 | ) |
||
| 379 | ); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Invokes the search for concepts in all the available ontologies. |
||
| 384 | */ |
||
| 385 | public function invokeGlobalSearch($request) |
||
| 450 | ) |
||
| 451 | ); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Invokes the search for a single vocabs concepts. |
||
| 456 | */ |
||
| 457 | public function invokeVocabularySearch($request) |
||
| 525 | ) |
||
| 526 | ); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Loads and renders the view containing a specific vocabulary. |
||
| 531 | */ |
||
| 532 | public function invokeVocabularyHome($request) |
||
| 533 | { |
||
| 534 | $lang = $request->getLang(); |
||
| 535 | $this->model->setLocale($request->getLang()); |
||
| 536 | $vocab = $request->getVocab(); |
||
| 537 | |||
| 538 | $defaultView = $vocab->getConfig()->getDefaultSidebarView(); |
||
| 539 | |||
| 540 | $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); |
||
| 541 | |||
| 542 | $template = $this->twig->load('vocab-home.twig'); |
||
| 543 | |||
| 544 | echo $template->render( |
||
| 545 | array( |
||
| 546 | 'languages' => $this->languages, |
||
| 547 | 'vocab' => $vocab, |
||
| 548 | 'content_languages' => $this->parseVocabularyLanguageOrder($vocab), |
||
| 549 | 'search_letter' => 'A', |
||
| 550 | 'active_tab' => $defaultView, |
||
| 551 | 'request' => $request, |
||
| 552 | 'plugin_params' => $pluginParameters |
||
| 553 | ) |
||
| 554 | ); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Invokes a very generic errorpage. |
||
| 559 | */ |
||
| 560 | public function invokeGenericErrorPage($request, $message = null) |
||
| 572 | ) |
||
| 573 | ); |
||
| 576 |