Completed
Push — master ( d91f33...8c195c )
by Nazar
09:16 queued 03:58
created

Controller::index_post()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 24
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 34
rs 8.439
1
<?php
2
/**
3
 * @package   Comments
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\Comments\api;
10
use
11
	cs\Event,
12
	cs\ExitException,
13
	cs\Language,
14
	cs\User,
15
	cs\modules\Comments\Comments;
16
17
/**
18
 * Provides next events:
19
 *  api/Comments/add
20
 *  [
21
 *   'item'   => item    //Item id
22
 *   'module' => module  //Module
23
 *   'allow'  => &$allow //Whether allow or not
24
 *  ]
25
 *
26
 *  api/Comments/edit
27
 *  [
28
 *   'id'     => id      //Comment id
29
 *   'user'   => user    //User id
30
 *   'item'   => item_id //Item id
31
 *   'module' => module  //Module
32
 *   'allow'  => &$allow //Whether allow or not
33
 *  ]
34
 *
35
 *  api/Comments/delete
36
 *  [
37
 *   'id'     => id      //Comment id
38
 *   'user'   => user    //User id
39
 *   'item'   => item_id //Item id
40
 *   'module' => module  //Module
41
 *   'allow'  => &$allow //Whether allow or not
42
 *  ]
43
 */
44
class Controller {
45
	/**
46
	 * @param \cs\Request $Request
47
	 *
48
	 * @return array|array[]
1 ignored issue
show
Documentation introduced by
Should the return type not be array|false|integer|string? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
49
	 *
50
	 * @throws ExitException
51
	 */
52
	static function index_get ($Request) {
53
		$query    = $Request->query('module', 'item');
54
		$id       = $Request->route_ids(0);
55
		$Comments = Comments::instance();
56
		if ($query) {
57
			return $Comments->get(
58
				$Comments->get_for_module_item($query['module'], $query['item'])
59
			);
60
		} elseif ($id) {
61
			$comment = $Comments->get($id);
62
			if (!$comment) {
63
				throw new ExitException(404);
64
			}
65
			return $comment;
66
		}
67
		throw new ExitException(400);
68
	}
69
	/**
70
	 * @param \cs\Request $Request
71
	 *
72
	 * @return string
73
	 *
74
	 * @throws ExitException
75
	 */
76
	static function index_post ($Request) {
77
		if (!User::instance()->user()) {
78
			throw new ExitException(403);
79
		}
80
		$data = $Request->data('item', 'module', 'text', 'parent');
81
		if (!$data) {
82
			throw new ExitException(400);
83
		}
84
		$L = Language::prefix('comments_');
85
		if (!strip_tags($data['text'])) {
86
			throw new ExitException($L->comment_cant_be_empty, 400);
87
		}
88
		$allow = false;
89
		Event::instance()->fire(
90
			'api/Comments/add',
91
			[
92
				'item'   => $data['item'],
93
				'module' => $data['module'],
94
				'allow'  => &$allow
95
			]
96
		);
97
		if (!$allow) {
98
			throw new ExitException($L->comment_sending_server_error, 500);
99
		}
100
		$Comments = Comments::instance();
101
		$id       = $Comments->add($data['item'], $data['module'], $data['text'], $data['parent']);
102
		if (!$id) {
103
			throw new ExitException($L->comment_sending_server_error, 500);
104
		}
105
		$data             = $Comments->get($id);
106
		$data['comments'] = false;
107
		//TODO: get rid of this
108
		return $Comments->tree_html([$data]);
0 ignored issues
show
Documentation introduced by
array($data) is of type array<integer,array|fals...false|integer|string"}>, but the function expects a array<integer,array>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
	}
110
	/**
111
	 * @param \cs\Request $Request
112
	 *
113
	 * @throws ExitException
114
	 */
115
	static function index_put ($Request) {
116
		if (!User::instance()->user()) {
117
			throw new ExitException(403);
118
		}
119
		$id = $Request->route(0);
120
		if (!$id) {
121
			throw new ExitException(400);
122
		}
123
		$L    = Language::prefix('comments_');
124
		$text = $Request->data('text');
125
		if (!strip_tags($text)) {
126
			throw new ExitException($L->comment_cant_be_empty, 400);
127
		}
128
		$Comments = Comments::instance();
129
		$comment  = $Comments->get($id);
130
		if (!$comment) {
131
			throw new ExitException(404);
132
		}
133
		$allow = false;
134
		Event::instance()->fire(
135
			'api/Comments/edit',
136
			[
137
				'id'     => $comment['id'],
138
				'user'   => $comment['user'],
139
				'item'   => $comment['item'],
140
				'module' => $comment['module'],
141
				'allow'  => &$allow
142
			]
143
		);
144
		if (
145
			!$allow ||
146
			!$Comments->set($comment['id'], $text)
147
		) {
148
			throw new ExitException($L->comment_editing_server_error, 500);
149
		}
150
		//TODO: get rid of this
151
		return $Comments->get($comment['id'])['text'];
152
	}
153
	/**
154
	 * @param \cs\Request $Request
155
	 *
156
	 * @return string
157
	 *
158
	 * @throws ExitException
159
	 */
160
	static function index_delete ($Request) {
161
		if (!User::instance()->user()) {
162
			throw new ExitException(403);
163
		}
164
		$id = $Request->route(0);
165
		if (!$id) {
166
			throw new ExitException(400);
167
		}
168
		$Comments = Comments::instance();
169
		$comment  = $Comments->get($id);
170
		if (!$comment) {
171
			throw new ExitException(404);
172
		}
173
		$allow = false;
174
		Event::instance()->fire(
175
			'api/Comments/delete',
176
			[
177
				'id'     => $comment['id'],
178
				'user'   => $comment['user'],
179
				'item'   => $comment['item'],
180
				'module' => $comment['module'],
181
				'allow'  => &$allow
182
			]
183
		);
184
		$L = Language::prefix('comments_');
185
		if (
186
			!$allow ||
187
			!$Comments->del($comment['id'])
188
		) {
189
			throw new ExitException($L->comment_deleting_server_error, 500);
190
		}
191
		//TODO: get rid of this
192
		return '';
193
	}
194
}
195