Total Complexity | 120 |
Total Lines | 610 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ControllerStartupUrlFormatter 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 ControllerStartupUrlFormatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ControllerStartupUrlFormatter extends \Divine\Engine\Core\Controller |
||
|
|||
24 | { |
||
25 | private $cache_data = null; |
||
26 | private $languages = array(); |
||
27 | private $config_language; |
||
28 | private $url_sheme = 'https'; |
||
29 | private $ssl_routes = array( |
||
30 | 'checkout/', |
||
31 | 'account/', |
||
32 | ); |
||
33 | |||
34 | // exceptions routes |
||
35 | private $valide_routes = array( |
||
36 | 'tracking', |
||
37 | 'utm_source', |
||
38 | 'utm_campaign', |
||
39 | 'utm_medium', |
||
40 | 'type', |
||
41 | 'source', |
||
42 | 'block', |
||
43 | 'position', |
||
44 | 'keyword', |
||
45 | 'yclid', |
||
46 | 'gclid' |
||
47 | ); |
||
48 | |||
49 | public function __construct($registry) |
||
50 | { |
||
51 | parent::__construct($registry); |
||
52 | |||
53 | // cache |
||
54 | $this->cache_data = $this->cache->get('url_formatter'); |
||
55 | |||
56 | if (!$this->cache_data) { |
||
57 | $query = $this->db->query(" |
||
58 | SELECT LOWER(`keyword`) as 'keyword', |
||
59 | `query` |
||
60 | FROM url_alias |
||
61 | "); |
||
62 | |||
63 | $this->cache_data = array(); |
||
64 | |||
65 | foreach ($query->rows as $row) { |
||
66 | $this->cache_data['keywords'][$row['keyword']] = $row['query']; |
||
67 | $this->cache_data['queries'][$row['query']] = $row['keyword']; |
||
68 | } |
||
69 | |||
70 | if (!isset($this->cache_data['queries']['common/home'])) { |
||
71 | $this->cache_data['queries']['common/home'] = ''; |
||
72 | $this->cache_data['keywords'][''] = 'common/home'; |
||
73 | } |
||
74 | |||
75 | $this->cache->set( |
||
76 | 'url_formatter', |
||
77 | $this->cache_data |
||
78 | ); |
||
79 | } |
||
80 | // |
||
81 | |||
82 | $query = $this->db->query(" |
||
83 | SELECT `value` |
||
84 | FROM `setting` |
||
85 | WHERE `key` = 'config_language' |
||
86 | "); |
||
87 | |||
88 | $this->config_language = $query->row['value']; |
||
89 | |||
90 | $query = $this->db->query(" |
||
91 | SELECT * |
||
92 | FROM language |
||
93 | WHERE status = '1' |
||
94 | "); |
||
95 | |||
96 | foreach ($query->rows as $result) { |
||
97 | $this->languages[$result['code']] = $result; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public function index() |
||
102 | { |
||
103 | |||
104 | // Add rewrite to url class |
||
105 | if ($this->config->get('config_seo_url')) { |
||
106 | $this->url->addRewrite($this); |
||
107 | } else { |
||
108 | return; |
||
109 | } |
||
110 | |||
111 | // Для cron скриптов, типа YML feed |
||
112 | if (php_sapi_name() === 'cli') { |
||
113 | return; |
||
114 | } |
||
115 | |||
116 | // Decode URL |
||
117 | if (!isset($this->request->get['_route_'])) { |
||
118 | $this->validate(); |
||
119 | } else { |
||
120 | $route_ = $this->request->get['_route_']; |
||
121 | |||
122 | unset($this->request->get['_route_']); |
||
123 | |||
124 | $parts = explode('/', trim(\voku\helper\UTF8::strtolower($route_), '/')); |
||
125 | |||
126 | list($last_part) = explode('.', array_pop($parts)); |
||
127 | |||
128 | array_push($parts, $last_part); |
||
129 | |||
130 | $rows = array(); |
||
131 | |||
132 | foreach ($parts as $keyword) { |
||
133 | if (isset($this->cache_data['keywords'][$keyword])) { |
||
134 | $rows[] = array('keyword' => $keyword, 'query' => $this->cache_data['keywords'][$keyword]); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | if (count($rows) == sizeof($parts)) { |
||
139 | $queries = array(); |
||
140 | |||
141 | foreach ($rows as $row) { |
||
142 | $queries[\voku\helper\UTF8::strtolower($row['keyword'])] = $row['query']; |
||
143 | } |
||
144 | |||
145 | reset($parts); |
||
146 | |||
147 | foreach ($parts as $part) { |
||
148 | $url = explode('=', $queries[$part], 2); |
||
149 | |||
150 | if ($url[0] == 'category_id') { |
||
151 | if (!isset($this->request->get['path'])) { |
||
152 | $this->request->get['path'] = $url[1]; |
||
153 | } else { |
||
154 | $this->request->get['path'] .= '_' . $url[1]; |
||
155 | } |
||
156 | } elseif ($url[0] == 'blog_category_id') { |
||
157 | if (!isset($this->request->get['blog_category_id'])) { |
||
158 | $this->request->get['blog_category_id'] = $url[1]; |
||
159 | } else { |
||
160 | $this->request->get['blog_category_id'] .= '_' . $url[1]; |
||
161 | } |
||
162 | } elseif (count($url) > 1) { |
||
163 | $this->request->get[$url[0]] = $url[1]; |
||
164 | } |
||
165 | } |
||
166 | } else { |
||
167 | $this->request->get['route'] = 'error/not_found'; |
||
168 | } |
||
169 | |||
170 | if (isset($this->request->get['product_id'])) { |
||
171 | $this->request->get['route'] = 'product/product'; |
||
172 | |||
173 | if (!isset($this->request->get['path'])) { |
||
174 | $path = $this->getPathByProduct($this->request->get['product_id']); |
||
175 | |||
176 | if ($path) { |
||
177 | $this->request->get['path'] = $path; |
||
178 | } |
||
179 | } |
||
180 | } elseif (isset($this->request->get['path'])) { |
||
181 | $this->request->get['route'] = 'product/category'; |
||
182 | |||
183 | //blog |
||
184 | } elseif (isset($this->request->get['article_id'])) { |
||
185 | $this->request->get['route'] = 'blog/article'; |
||
186 | |||
187 | if (!isset($this->request->get['blog_category_id'])) { |
||
188 | $blog_category_id = $this->getPathByArticle($this->request->get['article_id']); |
||
189 | |||
190 | if ($blog_category_id) { |
||
191 | $this->request->get['blog_category_id'] = $blog_category_id; |
||
192 | } |
||
193 | } |
||
194 | } elseif (isset($this->request->get['blog_category_id'])) { |
||
195 | $this->request->get['route'] = 'blog/category'; |
||
196 | |||
197 | //blog |
||
198 | } elseif (isset($this->request->get['manufacturer_id'])) { |
||
199 | $this->request->get['route'] = 'product/manufacturer/info'; |
||
200 | } elseif (isset($this->request->get['information_id'])) { |
||
201 | $this->request->get['route'] = 'information/information'; |
||
202 | } elseif (isset($this->cache_data['queries'][$route_])) { |
||
203 | $this->response->redirect($this->cache_data['queries'][$route_], 301); |
||
204 | } else { |
||
205 | if (isset($queries[$parts[0]])) { |
||
206 | $this->request->get['route'] = $queries[$parts[0]]; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | $this->validate(); |
||
211 | |||
212 | if (isset($this->request->get['route'])) { |
||
213 | return new \Divine\Engine\Core\Action($this->request->get['route']); |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | public function rewrite($link) |
||
418 | } |
||
419 | |||
420 | private function getPathByProduct($product_id) |
||
456 | } |
||
457 | |||
458 | private function getPathByCategory($category_id) |
||
459 | { |
||
460 | $category_id = (int) $category_id; |
||
461 | |||
462 | if ($category_id < 1) { |
||
463 | return false; |
||
464 | } |
||
465 | |||
466 | static $path = null; |
||
467 | |||
468 | if (!is_array($path)) { |
||
469 | $path = $this->cache->get('category.seopath'); |
||
470 | |||
471 | if (!is_array($path)) { |
||
472 | $path = array(); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | if (!isset($path[$category_id])) { |
||
477 | $max_level = 10; |
||
478 | |||
479 | $sql = "SELECT CONCAT_WS('_'"; |
||
480 | |||
481 | for ($i = $max_level - 1; $i >= 0; --$i) { |
||
482 | $sql .= ",t$i.category_id"; |
||
483 | } |
||
484 | |||
485 | $sql .= ") AS path FROM category t0"; |
||
486 | |||
487 | for ($i = 1; $i < $max_level; ++$i) { |
||
488 | $sql .= " LEFT JOIN category t$i ON (t$i.category_id = t" . ($i - 1) . ".parent_id)"; |
||
489 | } |
||
490 | |||
491 | $sql .= " WHERE t0.category_id = '" . $category_id . "'"; |
||
492 | |||
493 | $query = $this->db->query($sql); |
||
494 | |||
495 | $path[$category_id] = $query->num_rows ? $query->row['path'] : false; |
||
496 | |||
497 | $this->cache->set( |
||
498 | 'category.seopath', |
||
499 | $path |
||
500 | ); |
||
501 | } |
||
502 | |||
503 | return $path[$category_id]; |
||
504 | } |
||
505 | |||
506 | //blog |
||
507 | private function getPathByBlogCategory($blog_category_id) |
||
508 | { |
||
509 | $blog_category_id = (int) $blog_category_id; |
||
510 | |||
511 | if ($blog_category_id < 1) { |
||
512 | return false; |
||
513 | } |
||
514 | |||
515 | static $path = null; |
||
516 | |||
517 | if (!is_array($path)) { |
||
518 | |||
519 | // $path = $this->cache->get('blog_category.seopath'); |
||
520 | |||
521 | if (!is_array($path)) { |
||
522 | $path = array(); |
||
523 | } |
||
524 | } |
||
525 | |||
526 | if (!isset($path[$blog_category_id])) { |
||
527 | $max_level = 10; |
||
528 | |||
529 | $sql = "SELECT CONCAT_WS('_'"; |
||
530 | |||
531 | for ($i = $max_level - 1; $i >= 0; --$i) { |
||
532 | $sql .= ",t$i.blog_category_id"; |
||
533 | } |
||
534 | |||
535 | $sql .= ") AS path FROM blog_category t0"; |
||
536 | |||
537 | for ($i = 1; $i < $max_level; ++$i) { |
||
538 | $sql .= " LEFT JOIN blog_category t$i ON (t$i.blog_category_id = t" . ($i - 1) . ".parent_id)"; |
||
539 | } |
||
540 | |||
541 | $sql .= " WHERE t0.blog_category_id = '" . $blog_category_id . "'"; |
||
542 | |||
543 | $query = $this->db->query($sql); |
||
544 | |||
545 | $path[$blog_category_id] = $query->num_rows ? $query->row['path'] : false; |
||
546 | |||
547 | // $this->cache->set( |
||
548 | // 'blog_category.seopath', |
||
549 | // $path |
||
550 | // ); |
||
551 | } |
||
552 | |||
553 | return $path[$blog_category_id]; |
||
554 | } |
||
555 | |||
556 | private function getPathByArticle($article_id) |
||
557 | { |
||
558 | $article_id = (int) $article_id; |
||
559 | |||
560 | if ($article_id < 1) { |
||
561 | return false; |
||
562 | } |
||
563 | |||
564 | static $path = null; |
||
565 | |||
566 | if (!is_array($path)) { |
||
567 | |||
568 | // $path = $this->cache->get('article.seopath'); |
||
569 | |||
570 | if (!is_array($path)) { |
||
571 | $path = array(); |
||
572 | } |
||
573 | } |
||
574 | |||
575 | if (!isset($path[$article_id])) { |
||
576 | $query = $this->db->query("SELECT blog_category_id FROM article_to_blog_category WHERE article_id = '" . $article_id . "' ORDER BY main_blog_category DESC LIMIT 1"); |
||
577 | |||
578 | $path[$article_id] = $this->getPathByBlogCategory($query->num_rows ? (int) $query->row['blog_category_id'] : 0); |
||
579 | |||
580 | // $this->cache->set( |
||
581 | // 'article.seopath', |
||
582 | // $path |
||
583 | // ); |
||
584 | } |
||
585 | |||
586 | return $path[$article_id]; |
||
587 | } |
||
588 | |||
589 | //blog |
||
590 | private function validate() |
||
591 | { |
||
592 | |||
593 | //fix flat link for xml feed |
||
594 | if (isset($this->request->get['route'])) { |
||
595 | $break_routes = [ |
||
596 | 'error/not_found', |
||
597 | 'extension/feed/sitemap', |
||
598 | ]; |
||
599 | |||
600 | if (in_array($this->request->get['route'], $break_routes)) { |
||
601 | return; |
||
602 | } |
||
603 | } |
||
604 | |||
605 | if (empty($this->request->get['route'])) { |
||
606 | $this->request->get['route'] = 'common/home'; |
||
607 | } |
||
608 | |||
609 | if (isset($this->request->server['HTTP_X_REQUESTED_WITH']) && strtolower($this->request->server['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
||
610 | return; |
||
611 | } |
||
612 | |||
613 | $url = str_replace('&', '&', substr($this->config->get('config_url'), 0, strpos($this->config->get('config_url'), '/', 10)) . $this->request->server['REQUEST_URI']); |
||
614 | |||
615 | $seo = str_replace('&', '&', $this->url->link($this->request->get['route'], $this->getQueryString(array('route')))); |
||
616 | |||
617 | if (rawurldecode($url) != rawurldecode($seo)) { |
||
618 | $this->response->redirect($seo, 301); |
||
619 | } |
||
620 | } |
||
621 | |||
622 | private function getQueryString($exclude = array()) |
||
633 | ) |
||
634 | ) |
||
635 | ); |
||
636 | } |
||
637 | } |
||
638 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.