Total Complexity | 62 |
Total Lines | 500 |
Duplicated Lines | 0 % |
Changes | 1 | ||
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) |
||
28 | { |
||
29 | parent::__construct($model); |
||
30 | |||
31 | // initialize Twig templates |
||
32 | $tmpDir = $model->getConfig()->getTemplateCache(); |
||
33 | |||
34 | // check if the cache pointed by config.ttl exists, if not we create it. |
||
35 | if (!file_exists($tmpDir)) { |
||
36 | mkdir($tmpDir); |
||
37 | } |
||
38 | |||
39 | // specify where to look for templates and cache |
||
40 | $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/../view'); |
||
41 | // initialize Twig environment |
||
42 | $this->twig = new \Twig\Environment($loader, array('cache' => $tmpDir,'auto_reload' => true)); |
||
43 | // used for setting the base href for the relative urls |
||
44 | $this->twig->addGlobal("BaseHref", $this->getBaseHref()); |
||
45 | |||
46 | // pass the GlobalConfig object to templates so they can access configuration |
||
47 | $this->twig->addGlobal("GlobalConfig", $this->model->getConfig()); |
||
48 | |||
49 | // setting the list of properties to be displayed in the search results |
||
50 | $this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related')); |
||
51 | |||
52 | // register a Twig filter for generating URLs for vocabulary resources (concepts and groups) |
||
53 | $this->twig->addExtension(new LinkUrlExtension($model)); |
||
54 | |||
55 | // register a Twig filter for generating strings from language codes with CLDR |
||
56 | $langFilter = new \Twig\TwigFilter('lang_name', function ($langcode, $lang) { |
||
57 | return Language::getName($langcode, $lang); |
||
58 | }); |
||
59 | $this->twig->addFilter($langFilter); |
||
60 | |||
61 | $this->translator = $model->getTranslator(); |
||
62 | $this->twig->addExtension(new TranslationExtension($this->translator)); |
||
63 | |||
64 | // create the honeypot |
||
65 | $this->honeypot = new \Honeypot(); |
||
66 | if (!$this->model->getConfig()->getHoneypotEnabled()) { |
||
67 | $this->honeypot->disable(); |
||
68 | } |
||
69 | $this->twig->addGlobal('honeypot', $this->honeypot); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Guess the language of the user. Return a language string that is one |
||
74 | * of the supported languages defined in the $LANGUAGES setting, e.g. "fi". |
||
75 | * @param Request $request HTTP request |
||
76 | * @param string $vocid identifier for the vocabulary eg. 'yso'. |
||
77 | * @return string returns the language choice as a numeric string value |
||
78 | */ |
||
79 | public function guessLanguage($request, $vocid = null) |
||
80 | { |
||
81 | // 1. select language based on SKOSMOS_LANGUAGE cookie |
||
82 | $languageCookie = $request->getCookie('SKOSMOS_LANGUAGE'); |
||
83 | if ($languageCookie) { |
||
84 | return $languageCookie; |
||
85 | } |
||
86 | |||
87 | // 2. if vocabulary given, select based on the default language of the vocabulary |
||
88 | if ($vocid !== null && $vocid !== '') { |
||
89 | try { |
||
90 | $vocab = $this->model->getVocabulary($vocid); |
||
91 | return $vocab->getConfig()->getDefaultLanguage(); |
||
92 | } catch (Exception $e) { |
||
93 | // vocabulary id not found, move on to the next selection method |
||
94 | } |
||
95 | } |
||
96 | |||
97 | // 3. select language based on Accept-Language header |
||
98 | header('Vary: Accept-Language'); // inform caches that a decision was made based on Accept header |
||
99 | $this->negotiator = new \Negotiation\LanguageNegotiator(); |
||
100 | $langcodes = array_keys($this->languages); |
||
101 | // using a random language from the configured UI languages when there is no accept language header set |
||
102 | $acceptLanguage = $request->getServerConstant('HTTP_ACCEPT_LANGUAGE') ? $request->getServerConstant('HTTP_ACCEPT_LANGUAGE') : $langcodes[0]; |
||
103 | |||
104 | $bestLang = $this->negotiator->getBest($acceptLanguage, $langcodes); |
||
105 | if (isset($bestLang) && in_array($bestLang->getValue(), $langcodes)) { |
||
106 | return $bestLang->getValue(); |
||
107 | } |
||
108 | |||
109 | // show default site or prompt for language |
||
110 | return $langcodes[0]; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Determines a css class that controls width and positioning of the vocabulary list element. |
||
115 | * The layout is wider if the left/right box templates have not been provided. |
||
116 | * @return string css class for the container eg. 'voclist-wide' or 'voclist-right' |
||
117 | */ |
||
118 | private function listStyle() |
||
119 | { |
||
120 | $left = file_exists('view/left.inc'); |
||
121 | $right = file_exists('view/right.inc'); |
||
122 | $ret = 'voclist'; |
||
123 | if (!$left && !$right) { |
||
124 | $ret .= '-wide'; |
||
125 | } elseif (!($left && $right) && ($right || $left)) { |
||
126 | $ret .= ($right) ? '-left' : '-right'; |
||
127 | } |
||
128 | return $ret; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Loads and renders the landing page view. |
||
133 | * @param Request $request |
||
134 | */ |
||
135 | public function invokeLandingPage($request) |
||
155 | ) |
||
156 | ); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Invokes the concept page of a single concept in a specific vocabulary. |
||
161 | * |
||
162 | * @param Request $request |
||
163 | */ |
||
164 | public function invokeVocabularyConcept(Request $request) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Invokes the feedback page with information of the users current vocabulary. |
||
204 | */ |
||
205 | public function invokeFeedbackForm($request) |
||
206 | { |
||
207 | $template = $this->twig->load('feedback.twig'); |
||
208 | $this->model->setLocale($request->getLang()); |
||
209 | $vocabList = $this->model->getVocabularyList(false); |
||
210 | $vocab = $request->getVocab(); |
||
211 | |||
212 | $feedbackSent = false; |
||
213 | if ($request->getQueryParamPOST('message')) { |
||
214 | $feedbackSent = true; |
||
215 | $feedbackMsg = $request->getQueryParamPOST('message'); |
||
216 | $feedbackName = $request->getQueryParamPOST('name'); |
||
217 | $feedbackEmail = $request->getQueryParamPOST('email'); |
||
218 | $msgSubject = $request->getQueryParamPOST('msgsubject'); |
||
219 | $feedbackVocab = $request->getQueryParamPOST('vocab'); |
||
220 | $feedbackVocabEmail = ($feedbackVocab !== null && $feedbackVocab !== '') ? |
||
221 | $this->model->getVocabulary($feedbackVocab)->getConfig()->getFeedbackRecipient() : null; |
||
222 | // if the hidden field has been set a value we have found a spam bot |
||
223 | // and we do not actually send the message. |
||
224 | if ($this->honeypot->validateHoneypot($request->getQueryParamPOST('item-description')) && |
||
225 | $this->honeypot->validateHoneytime($request->getQueryParamPOST('user-captcha'), $this->model->getConfig()->getHoneypotTime())) { |
||
226 | $this->sendFeedback($request, $feedbackMsg, $msgSubject, $feedbackName, $feedbackEmail, $feedbackVocab, $feedbackVocabEmail); |
||
227 | } |
||
228 | } |
||
229 | echo $template->render( |
||
230 | array( |
||
231 | 'languages' => $this->languages, |
||
232 | 'vocab' => $vocab, |
||
233 | 'vocabList' => $vocabList, |
||
234 | 'feedback_sent' => $feedbackSent, |
||
235 | 'request' => $request, |
||
236 | ) |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | private function createFeedbackHeaders($fromName, $fromEmail, $toMail, $sender) |
||
241 | { |
||
242 | $headers = "MIME-Version: 1.0" . "\r\n"; |
||
243 | $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n"; |
||
244 | if (!empty($toMail)) { |
||
245 | $headers .= "Cc: " . $this->model->getConfig()->getFeedbackAddress() . "\r\n"; |
||
246 | } |
||
247 | if (!empty($fromEmail)) { |
||
248 | $headers .= "Reply-To: $fromName <$fromEmail>\r\n"; |
||
249 | } |
||
250 | $service = $this->model->getConfig()->getServiceName(); |
||
251 | return $headers . "From: $fromName via $service <$sender>"; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Sends the user entered message through the php's mailer. |
||
256 | * @param string $message content given by user. |
||
257 | * @param string $messageSubject subject line given by user. |
||
258 | * @param string $fromName senders own name. |
||
259 | * @param string $fromEmail senders email address. |
||
260 | * @param string $fromVocab which vocabulary is the feedback related to. |
||
261 | */ |
||
262 | public function sendFeedback($request, $message, $messageSubject, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null) |
||
307 | } |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Invokes the about page for the Skosmos service. |
||
312 | */ |
||
313 | public function invokeAboutPage($request) |
||
314 | { |
||
315 | $template = $this->twig->load('about.twig'); |
||
316 | $this->model->setLocale($request->getLang()); |
||
317 | $url = $request->getServerConstant('HTTP_HOST'); |
||
318 | |||
319 | echo $template->render( |
||
320 | array( |
||
321 | 'languages' => $this->languages, |
||
322 | 'server_instance' => $url, |
||
323 | 'request' => $request, |
||
324 | ) |
||
325 | ); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Invokes the search for concepts in all the available ontologies. |
||
330 | */ |
||
331 | public function invokeGlobalSearch($request) |
||
332 | { |
||
333 | $lang = $request->getLang(); |
||
334 | $template = $this->twig->load('global-search.twig'); |
||
335 | $this->model->setLocale($request->getLang()); |
||
336 | |||
337 | $parameters = new ConceptSearchParameters($request, $this->model->getConfig()); |
||
338 | |||
339 | $vocabs = $request->getQueryParam('vocabs'); # optional |
||
340 | // convert to vocids array to support multi-vocabulary search |
||
341 | $vocids = ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null; |
||
342 | $vocabObjects = array(); |
||
343 | if ($vocids) { |
||
344 | foreach ($vocids as $vocid) { |
||
345 | try { |
||
346 | $vocabObjects[] = $this->model->getVocabulary($vocid); |
||
347 | } catch (ValueError $e) { |
||
348 | // fail fast with an error page if the vocabulary cannot be found |
||
349 | if ($this->model->getConfig()->getLogCaughtExceptions()) { |
||
350 | error_log('Caught exception: ' . $e->getMessage()); |
||
351 | } |
||
352 | header("HTTP/1.0 400 Bad Request"); |
||
353 | $this->invokeGenericErrorPage($request, $e->getMessage()); |
||
354 | return; |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | $parameters->setVocabularies($vocabObjects); |
||
359 | |||
360 | $counts = null; |
||
361 | $searchResults = null; |
||
362 | $errored = false; |
||
363 | |||
364 | try { |
||
365 | $countAndResults = $this->model->searchConceptsAndInfo($parameters); |
||
366 | $counts = $countAndResults['count']; |
||
367 | $searchResults = $countAndResults['results']; |
||
368 | } catch (Exception $e) { |
||
369 | $errored = true; |
||
370 | header("HTTP/1.0 500 Internal Server Error"); |
||
371 | if ($this->model->getConfig()->getLogCaughtExceptions()) { |
||
372 | error_log('Caught exception: ' . $e->getMessage()); |
||
373 | } |
||
374 | } |
||
375 | $vocabList = $this->model->getVocabularyList(); |
||
376 | $sortedVocabs = $this->model->getVocabularyList(false, true); |
||
377 | $langList = $this->model->getLanguages($lang); |
||
378 | |||
379 | echo $template->render( |
||
380 | array( |
||
381 | 'search_count' => $counts, |
||
382 | 'languages' => $this->languages, |
||
383 | 'search_results' => $searchResults, |
||
384 | 'rest' => $parameters->getOffset() > 0, |
||
385 | 'global_search' => true, |
||
386 | 'search_failed' => $errored, |
||
387 | 'term' => $request->getQueryParamRaw('q'), |
||
388 | 'lang_list' => $langList, |
||
389 | 'vocabs' => isset($vocabs) ? str_replace(' ', '+', $vocabs) : null, |
||
390 | 'vocab_list' => $vocabList, |
||
391 | 'sorted_vocabs' => $sortedVocabs, |
||
392 | 'request' => $request, |
||
393 | 'parameters' => $parameters |
||
394 | ) |
||
395 | ); |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Invokes the search for a single vocabs concepts. |
||
400 | */ |
||
401 | public function invokeVocabularySearch($request) |
||
467 | ) |
||
468 | ); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Loads and renders the view containing a specific vocabulary. |
||
473 | */ |
||
474 | public function invokeVocabularyHome($request) |
||
475 | { |
||
476 | $lang = $request->getLang(); |
||
477 | $this->model->setLocale($request->getLang()); |
||
478 | $vocab = $request->getVocab(); |
||
479 | |||
480 | $defaultView = $vocab->getConfig()->getDefaultSidebarView(); |
||
481 | |||
482 | $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); |
||
483 | |||
484 | $template = $this->twig->load('vocab-home.twig'); |
||
485 | |||
486 | echo $template->render( |
||
487 | array( |
||
488 | 'languages' => $this->languages, |
||
489 | 'vocab' => $vocab, |
||
490 | 'search_letter' => 'A', |
||
491 | 'active_tab' => $defaultView, |
||
492 | 'request' => $request, |
||
493 | 'plugin_params' => $pluginParameters |
||
494 | ) |
||
495 | ); |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Invokes a very generic errorpage. |
||
500 | */ |
||
501 | public function invokeGenericErrorPage($request, $message = null) |
||
513 | ) |
||
514 | ); |
||
515 | } |
||
517 |