Total Complexity | 50 |
Total Lines | 488 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Controller { |
||
20 | /** |
||
21 | * @param \cs\Request $Request |
||
22 | */ |
||
23 | public static function latest_posts ($Request) { |
||
24 | if (!Event::instance()->fire('Blogs/latest_posts')) { |
||
25 | return; |
||
26 | } |
||
27 | $Config = Config::instance(); |
||
28 | $L = new Prefix('blogs_'); |
||
29 | $Meta = Meta::instance(); |
||
30 | $Page = Page::instance(); |
||
31 | $Posts = Posts::instance(); |
||
32 | /** |
||
33 | * Page title |
||
34 | */ |
||
35 | $Page->title($L->latest_posts); |
||
|
|||
36 | /** |
||
37 | * Now add link to Atom feed for latest posts |
||
38 | */ |
||
39 | $Page->atom('Blogs/atom.xml', $L->latest_posts); |
||
40 | /** |
||
41 | * Set page of blog type (Open Graph protocol) |
||
42 | */ |
||
43 | $Meta->blog(); |
||
44 | /** |
||
45 | * Determine current page |
||
46 | */ |
||
47 | $page = static::get_page_and_set_title($Request, $Page, $L); |
||
48 | /** |
||
49 | * Get posts for current page in JSON-LD structure format |
||
50 | */ |
||
51 | $posts_per_page = $Config->module('Blogs')->posts_per_page; |
||
52 | $posts = $Posts->get_latest_posts($page, $posts_per_page); |
||
53 | /** |
||
54 | * Base url (without page number) |
||
55 | */ |
||
56 | $base_url = $Config->base_url().'/'.path($L->Blogs).'/'.path($L->latest_posts); |
||
57 | /** |
||
58 | * Render posts page |
||
59 | */ |
||
60 | Helpers::show_posts_list( |
||
61 | $posts, |
||
62 | $Posts->get_total_count(), |
||
63 | $page, |
||
64 | $base_url |
||
65 | ); |
||
66 | } |
||
67 | /** |
||
68 | * @param \cs\Request $Request |
||
69 | * @param Page $Page |
||
70 | * @param Prefix $L |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | protected static function get_page_and_set_title ($Request, $Page, $L) { |
||
75 | $page = max( |
||
76 | isset($Request->route_ids[0]) ? array_slice($Request->route_ids, -1)[0] : 1, |
||
77 | 1 |
||
78 | ); |
||
79 | /** |
||
80 | * If this is not first page - show that in page title |
||
81 | */ |
||
82 | if ($page > 1) { |
||
83 | $Page->title($L->page_number($page)); |
||
84 | } |
||
85 | return $page; |
||
86 | } |
||
87 | /** |
||
88 | * @param \cs\Request $Request |
||
89 | * |
||
90 | * @throws ExitException |
||
91 | */ |
||
92 | public static function section ($Request) { |
||
93 | if (!Event::instance()->fire('Blogs/section')) { |
||
94 | return; |
||
95 | } |
||
96 | $Config = Config::instance(); |
||
97 | $L = new Prefix('blogs_'); |
||
98 | $Meta = Meta::instance(); |
||
99 | $Page = Page::instance(); |
||
100 | $Posts = Posts::instance(); |
||
101 | $Sections = Sections::instance(); |
||
102 | /** |
||
103 | * At first - determine part of url and get sections list based on that path |
||
104 | */ |
||
105 | $sections = $Sections->get_by_path( |
||
106 | array_slice($Request->route_path, 1) |
||
107 | ); |
||
108 | if (!$sections) { |
||
109 | throw new ExitException(400); |
||
110 | } |
||
111 | $sections = $Sections->get($sections); |
||
112 | /** |
||
113 | * Now lets set page title using sections names from page path |
||
114 | * We will not remove `$section` variable after, since it will be direct parent of each shown post |
||
115 | */ |
||
116 | foreach ($sections as $section) { |
||
117 | $Page->title($section['title']); |
||
118 | } |
||
119 | /** |
||
120 | * Now add link to Atom feed for posts from current section only |
||
121 | */ |
||
122 | /** @noinspection PhpUndefinedVariableInspection */ |
||
123 | $Page->atom( |
||
124 | "Blogs/atom.xml/?section=$section[id]", |
||
125 | implode($Config->core['title_delimiter'], [$L->latest_posts, $L->section, $section['title']]) |
||
126 | ); |
||
127 | /** |
||
128 | * Set page of blog type (Open Graph protocol) |
||
129 | */ |
||
130 | $Meta->blog(); |
||
131 | /** |
||
132 | * Determine current page |
||
133 | */ |
||
134 | $page = static::get_page_and_set_title($Request, $Page, $L); |
||
135 | /** |
||
136 | * Get posts for current page in JSON-LD structure format |
||
137 | */ |
||
138 | $posts_per_page = $Config->module('Blogs')->posts_per_page; |
||
139 | $posts = $Posts->get_for_section($section['id'], $page, $posts_per_page); |
||
140 | /** |
||
141 | * Base url (without page number) |
||
142 | */ |
||
143 | $base_url = $Config->base_url().'/'.path($L->Blogs).'/'.path($L->section)."/$section[full_path]"; |
||
144 | /** |
||
145 | * Render posts page |
||
146 | */ |
||
147 | Helpers::show_posts_list( |
||
148 | $posts, |
||
149 | $section['posts'], |
||
150 | $page, |
||
151 | $base_url |
||
152 | ); |
||
153 | } |
||
154 | /** |
||
155 | * @param \cs\Request $Request |
||
156 | * @param \cs\Response $Response |
||
157 | * |
||
158 | * @throws ExitException |
||
159 | */ |
||
160 | public static function post ($Request, $Response) { |
||
161 | if (!Event::instance()->fire('Blogs/post')) { |
||
162 | return; |
||
163 | } |
||
164 | $Page = Page::instance(); |
||
165 | $Posts = Posts::instance(); |
||
166 | $rc = $Request->route; |
||
167 | $post_id = (int)mb_substr($rc[1], mb_strrpos($rc[1], ':') + 1); |
||
168 | $post = $Posts->get_as_json_ld($post_id); |
||
169 | if ( |
||
170 | !$post || |
||
171 | ( |
||
172 | $post['draft'] && $post['user'] != User::instance()->id |
||
173 | ) |
||
174 | ) { |
||
175 | throw new ExitException(404); |
||
176 | } |
||
177 | if ($post['path'] != mb_substr($rc[1], 0, mb_strrpos($rc[1], ':'))) { |
||
178 | $Response->redirect($post['url'], 303); |
||
179 | return; |
||
180 | } |
||
181 | $Page->title($post['title']); |
||
182 | $Page->Description = description($post['short_content']); |
||
183 | $Page->canonical_url($post['url']); |
||
184 | $Meta = Meta::instance(); |
||
185 | $Meta |
||
186 | ->article() |
||
187 | ->article('published_time', date('Y-m-d', $post['date'] ?: time())) |
||
188 | ->article('section', $post['articleSection'] ? $post['articleSection'][0] : false) |
||
189 | ->article('tag', $post['tags']); |
||
190 | array_map([$Meta, 'image'], $post['image']); |
||
191 | $Page->content( |
||
192 | h::{'cs-blogs-post script[type=application/ld+json]'}( |
||
193 | json_encode($post, JSON_UNESCAPED_UNICODE) |
||
194 | ) |
||
195 | ); |
||
196 | } |
||
197 | protected static function is_blogs_admin () { |
||
198 | $User = User::instance(); |
||
199 | return |
||
200 | $User->admin() && |
||
201 | $User->get_permission('admin/Blogs', 'index') && |
||
202 | $User->get_permission('admin/Blogs', 'edit_post'); |
||
203 | } |
||
204 | /** |
||
205 | * @param \cs\Request $Request |
||
206 | * |
||
207 | * @throws ExitException |
||
208 | */ |
||
209 | public static function tag ($Request) { |
||
210 | if (!Event::instance()->fire('Blogs/tag')) { |
||
211 | return; |
||
212 | } |
||
213 | $Config = Config::instance(); |
||
214 | $L = new Prefix('blogs_'); |
||
215 | $Meta = Meta::instance(); |
||
216 | $Page = Page::instance(); |
||
217 | $Posts = Posts::instance(); |
||
218 | $Tags = Tags::instance(); |
||
219 | /** |
||
220 | * If no tag specified |
||
221 | */ |
||
222 | if (!isset($Request->route[1])) { |
||
223 | throw new ExitException(404); |
||
224 | } |
||
225 | /** |
||
226 | * Find tag |
||
227 | */ |
||
228 | $tag = $Tags->get_by_text($Request->route[1]); |
||
229 | if (!$tag) { |
||
230 | throw new ExitException(404); |
||
231 | } |
||
232 | $tag = $Tags->get($tag); |
||
233 | /** |
||
234 | * Add tag to page title |
||
235 | */ |
||
236 | $Page->title($tag['text']); |
||
237 | /** |
||
238 | * Now add link to Atom feed for posts with current tag only |
||
239 | */ |
||
240 | $Page->atom( |
||
241 | "Blogs/atom.xml/?tag=$tag[id]", |
||
242 | implode($Config->core['title_delimiter'], [$L->latest_posts, $L->tag, $tag['text']]) |
||
243 | ); |
||
244 | /** |
||
245 | * Set page of blog type (Open Graph protocol) |
||
246 | */ |
||
247 | $Meta->blog(); |
||
248 | /** |
||
249 | * Determine current page |
||
250 | */ |
||
251 | $page = max($Request->route(2) ?: 1, 1); |
||
252 | /** |
||
253 | * If this is not first page - show that in page title |
||
254 | */ |
||
255 | if ($page > 1) { |
||
256 | $Page->title($L->blogs_nav_page($page)); |
||
257 | } |
||
258 | /** |
||
259 | * Get posts for current page in JSON-LD structure format |
||
260 | */ |
||
261 | $posts_per_page = $Config->module('Blogs')->posts_per_page; |
||
262 | $posts = $Posts->get_for_tag($tag['id'], $page, $posts_per_page); |
||
263 | $posts_count = $Posts->get_for_tag_count($tag['id']); |
||
264 | /** |
||
265 | * Base url (without page number) |
||
266 | */ |
||
267 | $base_url = $Config->base_url().'/'.path($L->Blogs).'/'.path($L->tag).'/'.$Request->route[1]; |
||
268 | /** |
||
269 | * Render posts page |
||
270 | */ |
||
271 | Helpers::show_posts_list( |
||
272 | $posts, |
||
273 | $posts_count, |
||
274 | $page, |
||
275 | $base_url |
||
276 | ); |
||
277 | } |
||
278 | public static function new_post () { |
||
279 | if (!Event::instance()->fire('Blogs/new_post')) { |
||
280 | return; |
||
281 | } |
||
282 | |||
283 | $Config = Config::instance(); |
||
284 | $module_data = $Config->module('Blogs'); |
||
285 | $L = new Prefix('blogs_'); |
||
286 | $Page = Page::instance(); |
||
287 | $User = User::instance(); |
||
288 | $Page->title($L->new_post); |
||
289 | if (!$User->admin() && $module_data->new_posts_only_from_admins) { |
||
290 | throw new ExitException(403); |
||
291 | } |
||
292 | if (!$User->user()) { |
||
293 | $Page->warning($L->for_registered_users_only); |
||
294 | return; |
||
295 | } |
||
296 | $Page->content( |
||
297 | h::cs_blogs_add_edit_post() |
||
298 | ); |
||
299 | } |
||
300 | /** |
||
301 | * @param \cs\Request $Request |
||
302 | * |
||
303 | * @throws ExitException |
||
304 | */ |
||
305 | public static function edit_post ($Request) { |
||
306 | if (!Event::instance()->fire('Blogs/edit_post')) { |
||
307 | return; |
||
308 | } |
||
309 | |||
310 | $Posts = Posts::instance(); |
||
311 | $Config = Config::instance(); |
||
312 | $module_data = $Config->module('Blogs'); |
||
313 | $L = new Prefix('blogs_'); |
||
314 | $Page = Page::instance(); |
||
315 | $User = User::instance(); |
||
316 | if ($module_data->new_posts_only_from_admins && !$User->admin()) { |
||
317 | throw new ExitException(403); |
||
318 | } |
||
319 | $id = $Request->route(1); |
||
320 | if (!$id) { |
||
321 | throw new ExitException(400); |
||
322 | } |
||
323 | $post = $Posts->get($id); |
||
324 | if (!$post) { |
||
325 | throw new ExitException(404); |
||
326 | } |
||
327 | if ($post['user'] != $User->id && !static::is_blogs_admin()) { |
||
328 | throw new ExitException(403); |
||
329 | } |
||
330 | $Page->title( |
||
331 | $L->editing_of_post($post['title']) |
||
332 | ); |
||
333 | $Page->content( |
||
334 | h::cs_blogs_add_edit_post( |
||
335 | [ |
||
336 | 'id' => $post['id'] |
||
337 | ] |
||
338 | ) |
||
339 | ); |
||
340 | } |
||
341 | /** |
||
342 | * @param \cs\Request $Request |
||
343 | */ |
||
344 | public static function drafts ($Request) { |
||
345 | if (!Event::instance()->fire('Blogs/drafts')) { |
||
346 | return; |
||
347 | } |
||
348 | $Config = Config::instance(); |
||
349 | $L = new Prefix('blogs_'); |
||
350 | $Page = Page::instance(); |
||
351 | $Posts = Posts::instance(); |
||
352 | $User = User::instance(); |
||
353 | $Page->title($L->drafts); |
||
354 | /** |
||
355 | * Determine current page |
||
356 | */ |
||
357 | $page = static::get_page_and_set_title($Request, $Page, $L); |
||
358 | /** |
||
359 | * Get posts for current page in JSON-LD structure format |
||
360 | */ |
||
361 | $posts_per_page = $Config->module('Blogs')->posts_per_page; |
||
362 | $posts = $Posts->get_drafts($User->id, $page, $posts_per_page); |
||
363 | $posts_count = $Posts->get_drafts_count($User->id); |
||
364 | /** |
||
365 | * Base url (without page number) |
||
366 | */ |
||
367 | $base_url = $Config->base_url().'/'.path($L->Blogs).'/'.path($L->drafts); |
||
368 | /** |
||
369 | * Render posts page |
||
370 | */ |
||
371 | Helpers::show_posts_list( |
||
372 | $posts, |
||
373 | $posts_count, |
||
374 | $page, |
||
375 | $base_url |
||
376 | ); |
||
377 | } |
||
378 | /** |
||
379 | * @param \cs\Request $Request |
||
380 | * @param \cs\Response $Response |
||
381 | * |
||
382 | * @throws ExitException |
||
383 | */ |
||
384 | public static function atom_xml ($Request, $Response) { |
||
492 | ] |
||
493 | ) |
||
494 | ); |
||
495 | } |
||
496 | /** |
||
497 | * @param string $theme |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | protected static function get_favicon_path ($theme) { |
||
507 | } |
||
508 | } |
||
509 |