Completed
Push — master ( 5f6697...2f4e66 )
by Nazar
04:19
created

Controller::posts_preview()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 99
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 99
rs 4.8514
cc 10
eloc 69
nc 7
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   Blogs
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Blogs\api;
10
use
11
	h,
12
	cs\Config,
13
	cs\ExitException,
14
	cs\Language\Prefix,
15
	cs\Page,
16
	cs\User,
17
	cs\modules\Blogs\Posts,
18
	cs\modules\Blogs\Sections;
19
20
class Controller {
21
	static function posts_preview () {
22
		$Config = Config::instance();
23
		$User   = User::instance();
24
		if (!$User->user()) {
25
			throw new ExitException(403);
26
		}
27
		$L    = new Prefix('blogs_');
28
		$Page = Page::instance();
29
		if (empty($_POST['title'])) {
30
			$Page->warning($L->post_title_empty);
31
			$Page->json($Page->Top);
32
			return;
33
		}
34
		if (empty($_POST['sections']) && $_POST['sections'] !== '0') {
35
			$Page->warning($L->no_post_sections_specified);
36
			$Page->json($Page->Top);
37
			return;
38
		}
39
		if (empty($_POST['content'])) {
40
			$Page->warning($L->post_content_empty);
41
			$Page->json($Page->Top);
42
			return;
43
		}
44
		if (empty($_POST['tags'])) {
45
			$Page->warning($L->no_post_tags_specified);
46
			$Page->json($Page->Top);
47
			return;
48
		}
49
		$Posts       = Posts::instance();
50
		$Sections    = Sections::instance();
51
		$post        = isset($_POST['id']) ? $Posts->get($_POST['id']) : [
52
			'date'           => TIME,
53
			'user'           => $User->id,
54
			'comments_count' => 0
55
		];
56
		$module      = path($L->Blogs);
57
		$module_data = $Config->module('Blogs');
58
		$Page->json(
59
			h::{'section.cs-blogs-post article'}(
60
				h::header(
61
					h::h1(xap($_POST['title'])).
62
					((array)$_POST['sections'] != [0] ? h::p(
63
						h::icon('bookmark').
64
						implode(
65
							', ',
66
							array_map(
67
								function ($section) use ($Sections, $L, $module) {
68
									$section = $Sections->get($section);
69
									return h::a(
70
										$section['title'],
71
										[
72
											'href' => "$module/".path($L->section)."/$section[full_path]"
73
										]
74
									);
75
								},
76
								(array)$_POST['sections']
77
							)
78
						)
79
					) : '')
80
				).
81
				xap($_POST['content'], true, $module_data->allow_iframes_without_content)."\n".
82
				h::footer(
83
					h::p(
84
						h::icon('tags').
85
						implode(
86
							', ',
87
							array_map(
88
								function ($tag) use ($L, $module) {
89
									$tag = xap($tag);
90
									return h::a(
91
										$tag,
92
										[
93
											'href' => "$module/".path($L->tag)."/$tag",
94
											'rel'  => 'tag'
95
										]
96
									);
97
								},
98
								_trim(explode(',', $_POST['tags']))
99
							)
100
						)
101
					).
102
					h::hr().
103
					h::p(
104
						h::time(
105
							$L->to_locale(date($L->_datetime_long, $post['date'])),
106
							[
107
								'datetime' => date('c', $post['date'])
108
							]
109
						).
110
						h::icon('user').$User->username($post['user']).
111
						(
112
						$module_data->enable_comments ? h::icon('comments').$post['comments_count'] : ''
113
						)
114
					)
115
				)
116
			).
117
			h::br(2)
118
		);
119
	}
120
}
121