Completed
Push — master ( 30d5c1...402c86 )
by Nazar
04:08
created

Controller::posts_preview()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 80
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 80
rs 8.4041
cc 5
eloc 54
nc 3
nop 1

How to fix   Long Method   

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 __get_settings () {
22
		$User        = User::instance();
23
		$module_data = Config::instance()->module('Blogs');
24
		return [
25
			'inline_editor'              => functionality('inline_editor'),
26
			'max_sections'               => $module_data->max_sections,
27
			'new_posts_only_from_admins' => $module_data->new_posts_only_from_admins, //TODO use this on frontend
28
			'can_delete_posts'           => //TODO use this on frontend
29
				$User->admin() &&
30
				$User->get_permission('admin/Blogs', 'index') &&
31
				$User->get_permission('admin/Blogs', 'edit_post')
32
		];
33
	}
34
	/**
35
	 * @param \cs\Request $Request
36
	 *
37
	 * @return array
38
	 *
39
	 * @throws ExitException
40
	 */
41
	static function posts_get ($Request) {
42
		$id = $Request->route_ids(0);
43
		if ($id) {
44
			$post = Posts::instance()->get($id);
45
			if (!$post) {
46
				throw new ExitException(404);
47
			}
48
			return $post;
49
		} else {
1 ignored issue
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
50
			// TODO: implement latest posts
51
		}
52
	}
53
	/**
54
	 * @param \cs\Request  $Request
55
	 * @param \cs\Response $Response
56
	 *
57
	 * @return array
58
	 *
59
	 * @throws ExitException
60
	 */
61
	static function posts_post ($Request, $Response) {
62
		$Config      = Config::instance();
63
		$module_data = $Config->module('Blogs');
64
		$L           = new Prefix('blogs_');
65
		$User        = User::instance();
66
		if (!$User->admin() && $module_data->new_posts_only_from_admins) {
67
			throw new ExitException(403);
68
		}
69
		if (!$User->user()) {
70
			throw new ExitException($L->for_registered_users_only, 403);
71
		}
72
		$data = static::check_request_data($Request, $L);
73
		if (!$data) {
74
			throw new ExitException(400);
75
		}
76
		$Posts = Posts::instance();
77
		$id    = $Posts->add($data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft');
78
		if (!$id) {
79
			throw new ExitException($L->post_adding_error, 500);
80
		}
81
		$Response->code = 201;
82
		return [
83
			'id'  => $id,
84
			'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id"
85
		];
86
	}
87
	/**
88
	 * @param \cs\Request $Request
89
	 *
90
	 * @return array
91
	 *
92
	 * @throws ExitException
93
	 */
94
	static function posts_put ($Request) {
95
		$Config = Config::instance();
96
		$L      = new Prefix('blogs_');
97
		$User   = User::instance();
98
		$id     = $Request->route(1);
99
		$data   = static::check_request_data($Request, $L);
100
		if (!$id || !$data) {
101
			throw new ExitException(400);
102
		}
103
		$Posts = Posts::instance();
104
		$post  = $Posts->get($id);
105
		if (!$post) {
106
			throw new ExitException(404);
107
		}
108
		if (
109
			!$User->admin() ||
110
			!$User->get_permission('admin/Blogs', 'index') ||
111
			!$User->get_permission('admin/Blogs', 'edit_post')
112
		) {
113
			throw new ExitException(403);
114
		}
115
		if (!$Posts->set($id, $data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft')) {
116
			throw new ExitException($L->post_saving_error, 500);
117
		}
118
		return [
119
			'id'  => $id,
120
			'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id"
121
		];
122
	}
123
	/**
124
	 * @param \cs\Request $Request
125
	 *
126
	 * @throws ExitException
127
	 */
128
	static function posts_delete ($Request) {
129
		$L    = new Prefix('blogs_');
130
		$User = User::instance();
131
		$id   = $Request->route(1);
132
		if (!$id) {
133
			throw new ExitException(400);
134
		}
135
		$Posts = Posts::instance();
136
		$post  = $Posts->get($id);
137
		if (!$post) {
138
			throw new ExitException(404);
139
		}
140
		if (
141
			$post['user'] != $User->id &&
142
			!(
143
				$User->admin() &&
144
				$User->get_permission('admin/Blogs', 'index') &&
145
				$User->get_permission('admin/Blogs', 'edit_post')
146
			)
147
		) {
148
			throw new ExitException(403);
149
		}
150
		if (!$Posts->del($id)) {
151
			throw new ExitException($L->post_deleting_error, 500);
152
		}
153
	}
154
	/**
155
	 * @param \cs\Request $Request
156
	 * @param Prefix      $L
157
	 *
158
	 * @return array
159
	 *
160
	 * @throws ExitException
161
	 */
162
	protected static function check_request_data ($Request, $L) {
163
		$data = $Request->data('title', 'sections', 'content', 'tags', 'mode');
164
		if (!$data) {
165
			throw new ExitException(400);
166
		}
167
		$data['path'] = $Request->data('path');
168
		if (empty($data['title'])) {
169
			throw new ExitException($L->post_title_empty, 400);
170
		}
171
		if (empty($data['sections']) || !is_array($data['sections'])) {
172
			throw new ExitException($L->no_post_sections_specified, 400);
173
		}
174
		if (empty($data['content'])) {
175
			throw new ExitException($L->post_content_empty, 400);
176
		}
177
		if (empty($data['tags']) || !is_array($data['tags'])) {
178
			throw new ExitException($L->no_post_tags_specified, 400);
179
		}
180
		return $data;
181
	}
182
	/**
183
	 * @param \cs\Request $Request
184
	 *
185
	 * @throws ExitException
186
	 */
187
	static function posts_preview ($Request) {
188
		$Config = Config::instance();
189
		$User   = User::instance();
190
		if (!$User->user()) {
191
			throw new ExitException(403);
192
		}
193
		$L           = new Prefix('blogs_');
194
		$Page        = Page::instance();
195
		$data        = $Request->data('title', 'sections', 'content', 'tags');
196
		$Posts       = Posts::instance();
197
		$Sections    = Sections::instance();
198
		$post        = isset($Request->data['id']) ? $Posts->get($Request->data['id']) : [
199
			'date'           => TIME,
200
			'user'           => $User->id,
201
			'comments_count' => 0
202
		];
203
		$module      = path($L->Blogs);
204
		$module_data = $Config->module('Blogs');
205
		$Page->json(
206
			h::{'section.cs-blogs-post article'}(
207
				h::header(
208
					h::h1(xap($data['title'])).
209
					((array)$data['sections'] != [0] ? h::p(
210
						h::icon('bookmark').
211
						implode(
212
							', ',
213
							array_map(
214
								function ($section) use ($Sections, $L, $module) {
215
									$section = $Sections->get($section);
216
									return h::a(
217
										$section['title'],
218
										[
219
											'href' => "$module/".path($L->section)."/$section[full_path]"
220
										]
221
									);
222
								},
223
								(array)$data['sections']
224
							)
225
						)
226
					) : '')
227
				).
228
				xap($data['content'], true, $module_data->allow_iframes_without_content)."\n".
229
				h::footer(
230
					h::p(
231
						h::icon('tags').
232
						implode(
233
							', ',
234
							array_map(
235
								function ($tag) use ($L, $module) {
236
									$tag = xap($tag);
237
									return h::a(
238
										$tag,
239
										[
240
											'href' => "$module/".path($L->tag)."/$tag",
241
											'rel'  => 'tag'
242
										]
243
									);
244
								},
245
								_trim($data['tags'])
246
							)
247
						)
248
					).
249
					h::hr().
250
					h::p(
251
						h::time(
252
							$L->to_locale(date($L->_datetime_long, $post['date'])),
253
							[
254
								'datetime' => date('c', $post['date'])
255
							]
256
						).
257
						h::icon('user').$User->username($post['user']).
258
						(
259
						$module_data->enable_comments ? h::icon('comments').$post['comments_count'] : ''
260
						)
261
					)
262
				)
263
			).
264
			h::br(2)
265
		);
266
	}
267
	static function sections_get () {
268
		return Sections::instance()->get_all() ?: [];
269
	}
270
}
271