| Total Complexity | 64 |
| Total Lines | 541 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
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 | * Loads and renders the landing page view. |
||
| 163 | * @param Request $request |
||
| 164 | */ |
||
| 165 | public function invokeLandingPage($request) |
||
| 186 | ) |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Invokes the concept page of a single concept in a specific vocabulary. |
||
| 192 | * |
||
| 193 | * @param Request $request |
||
| 194 | */ |
||
| 195 | public function invokeVocabularyConcept(Request $request) |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Invokes the feedback page with information of the users current vocabulary. |
||
| 237 | */ |
||
| 238 | public function invokeFeedbackForm($request) |
||
| 239 | { |
||
| 240 | $template = $this->twig->load('feedback.twig'); |
||
| 241 | $this->model->setLocale($request->getLang()); |
||
| 242 | $vocabList = $this->model->getVocabularyList(false); |
||
| 243 | $vocab = $request->getVocab(); |
||
| 244 | |||
| 245 | $feedbackSent = false; |
||
| 246 | if ($request->getQueryParamPOST('message')) { |
||
| 247 | $feedbackSent = true; |
||
| 248 | $feedbackMsg = $request->getQueryParamPOST('message'); |
||
| 249 | $feedbackName = $request->getQueryParamPOST('name'); |
||
| 250 | $feedbackEmail = $request->getQueryParamPOST('email'); |
||
| 251 | $msgSubject = $request->getQueryParamPOST('msgsubject'); |
||
| 252 | $feedbackVocab = $request->getQueryParamPOST('vocab'); |
||
| 253 | $feedbackVocabEmail = ($feedbackVocab !== null && $feedbackVocab !== '') ? |
||
| 254 | $this->model->getVocabulary($feedbackVocab)->getConfig()->getFeedbackRecipient() : null; |
||
| 255 | // if the hidden field has been set a value we have found a spam bot |
||
| 256 | // and we do not actually send the message. |
||
| 257 | if ($this->honeypot->validateHoneypot($request->getQueryParamPOST('item-description')) && |
||
| 258 | $this->honeypot->validateHoneytime($request->getQueryParamPOST('user-captcha'), $this->model->getConfig()->getHoneypotTime())) { |
||
| 259 | $this->sendFeedback($request, $feedbackMsg, $msgSubject, $feedbackName, $feedbackEmail, $feedbackVocab, $feedbackVocabEmail); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | echo $template->render( |
||
| 263 | array( |
||
| 264 | 'languages' => $this->languages, |
||
| 265 | 'vocab' => $vocab, |
||
| 266 | 'vocabList' => $vocabList, |
||
| 267 | 'feedback_sent' => $feedbackSent, |
||
| 268 | 'request' => $request, |
||
| 269 | ) |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | |||
| 273 | private function createFeedbackHeaders($fromName, $fromEmail, $toMail, $sender) |
||
| 274 | { |
||
| 275 | $headers = "MIME-Version: 1.0" . "\r\n"; |
||
| 276 | $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n"; |
||
| 277 | if (!empty($toMail)) { |
||
| 278 | $headers .= "Cc: " . $this->model->getConfig()->getFeedbackAddress() . "\r\n"; |
||
| 279 | } |
||
| 280 | if (!empty($fromEmail)) { |
||
| 281 | $headers .= "Reply-To: $fromName <$fromEmail>\r\n"; |
||
| 282 | } |
||
| 283 | $service = $this->model->getConfig()->getServiceName(); |
||
| 284 | return $headers . "From: $fromName via $service <$sender>"; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Sends the user entered message through the php's mailer. |
||
| 289 | * @param string $message content given by user. |
||
| 290 | * @param string $messageSubject subject line given by user. |
||
| 291 | * @param string $fromName senders own name. |
||
| 292 | * @param string $fromEmail senders email address. |
||
| 293 | * @param string $fromVocab which vocabulary is the feedback related to. |
||
| 294 | */ |
||
| 295 | public function sendFeedback($request, $message, $messageSubject, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null) |
||
| 296 | { |
||
| 297 | $toAddress = ($toMail) ? $toMail : $this->model->getConfig()->getFeedbackAddress(); |
||
| 298 | $messageSubject = "[" . $this->model->getConfig()->getServiceName() . "] " . $messageSubject; |
||
| 299 | if ($fromVocab !== null && $fromVocab !== '') { |
||
| 300 | $message = 'Feedback from vocab: ' . strtoupper($fromVocab) . "<br />" . $message; |
||
| 301 | } |
||
| 302 | $envelopeSender = $this->model->getConfig()->getFeedbackEnvelopeSender(); |
||
| 303 | // determine the sender address of the message |
||
| 304 | $sender = $this->model->getConfig()->getFeedbackSender(); |
||
| 305 | if (empty($sender)) { |
||
| 306 | $sender = $envelopeSender; |
||
| 307 | } |
||
| 308 | if (empty($sender)) { |
||
| 309 | $sender = $this->model->getConfig()->getFeedbackAddress(); |
||
| 310 | } |
||
| 311 | |||
| 312 | // determine sender name - default to "anonymous user" if not given by user |
||
| 313 | if (empty($fromName)) { |
||
| 314 | $fromName = "anonymous user"; |
||
| 315 | } |
||
| 316 | $headers = $this->createFeedbackHeaders($fromName, $fromEmail, $toMail, $sender); |
||
| 317 | $params = empty($envelopeSender) ? '' : "-f $envelopeSender"; |
||
| 318 | // adding some information about the user for debugging purposes. |
||
| 319 | $message = $message . "<br /><br /> Debugging information:" |
||
| 320 | . "<br />Timestamp: " . date(DATE_RFC2822) |
||
| 321 | . "<br />User agent: " . $request->getServerConstant('HTTP_USER_AGENT') |
||
| 322 | . "<br />Referer: " . $request->getServerConstant('HTTP_REFERER'); |
||
| 323 | |||
| 324 | try { |
||
| 325 | mail($toAddress, $messageSubject, $message, $headers, $params); |
||
| 326 | } catch (Exception $e) { |
||
| 327 | header("HTTP/1.0 404 Not Found"); |
||
| 328 | $template = $this->twig->load('error.twig'); |
||
| 329 | if ($this->model->getConfig()->getLogCaughtExceptions()) { |
||
| 330 | error_log('Caught exception: ' . $e->getMessage()); |
||
| 331 | } |
||
| 332 | |||
| 333 | echo $template->render( |
||
| 334 | array( |
||
| 335 | 'languages' => $this->languages, |
||
| 336 | ) |
||
| 337 | ); |
||
| 338 | |||
| 339 | return; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Invokes the about page for the Skosmos service. |
||
| 345 | */ |
||
| 346 | public function invokeAboutPage($request) |
||
| 347 | { |
||
| 348 | $template = $this->twig->load('about.twig'); |
||
| 349 | $this->model->setLocale($request->getLang()); |
||
| 350 | $url = $request->getServerConstant('HTTP_HOST'); |
||
| 351 | |||
| 352 | echo $template->render( |
||
| 353 | array( |
||
| 354 | 'languages' => $this->languages, |
||
| 355 | 'server_instance' => $url, |
||
| 356 | 'request' => $request, |
||
| 357 | ) |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Invokes the search for concepts in all the available ontologies. |
||
| 363 | */ |
||
| 364 | public function invokeGlobalSearch($request) |
||
| 429 | ) |
||
| 430 | ); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Invokes the search for a single vocabs concepts. |
||
| 435 | */ |
||
| 436 | public function invokeVocabularySearch($request) |
||
| 505 | ) |
||
| 506 | ); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Loads and renders the view containing a specific vocabulary. |
||
| 511 | */ |
||
| 512 | public function invokeVocabularyHome($request) |
||
| 513 | { |
||
| 514 | $lang = $request->getLang(); |
||
| 515 | $this->model->setLocale($request->getLang()); |
||
| 516 | $vocab = $request->getVocab(); |
||
| 517 | |||
| 518 | $defaultView = $vocab->getConfig()->getDefaultSidebarView(); |
||
| 519 | |||
| 520 | $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); |
||
| 521 | |||
| 522 | $template = $this->twig->load('vocab-home.twig'); |
||
| 523 | |||
| 524 | $vocabSearchLangList = $vocab->getConfig()->getLanguages(); |
||
| 525 | |||
| 526 | echo $template->render( |
||
| 527 | array( |
||
| 528 | 'languages' => $this->languages, |
||
| 529 | 'vocab' => $vocab, |
||
| 530 | 'search_lang_list' => $vocabSearchLangList, |
||
| 531 | 'search_letter' => 'A', |
||
| 532 | 'active_tab' => $defaultView, |
||
| 533 | 'request' => $request, |
||
| 534 | 'plugin_params' => $pluginParameters |
||
| 535 | ) |
||
| 536 | ); |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Invokes a very generic errorpage. |
||
| 541 | */ |
||
| 542 | public function invokeGenericErrorPage($request, $message = null) |
||
| 554 | ) |
||
| 555 | ); |
||
| 556 | } |
||
| 557 | } |
||
| 558 |