Completed
Push — master ( b84cc6...d91f33 )
by Nazar
06:59
created

Controller   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 127
rs 10
wmc 19
lcom 0
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
B index_post() 0 34 6
C index_put() 0 38 7
B index_delete() 0 34 6
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 string
49
	 *
50
	 * @throws ExitException
51
	 */
52
	static function index_post ($Request) {
53
		if (!User::instance()->user()) {
54
			throw new ExitException(403);
55
		}
56
		$data = $Request->data('item', 'module', 'text', 'parent');
57
		if (!$data) {
58
			throw new ExitException(400);
59
		}
60
		$L = Language::prefix('comments_');
61
		if (!strip_tags($data['text'])) {
62
			throw new ExitException($L->comment_cant_be_empty, 400);
63
		}
64
		$allow = false;
65
		Event::instance()->fire(
66
			'api/Comments/add',
67
			[
68
				'item'   => $data['item'],
69
				'module' => $data['module'],
70
				'allow'  => &$allow
71
			]
72
		);
73
		if (!$allow) {
74
			throw new ExitException($L->comment_sending_server_error, 500);
75
		}
76
		$Comments = Comments::instance();
77
		$id       = $Comments->add($data['item'], $data['module'], $data['text'], $data['parent']);
78
		if (!$id) {
79
			throw new ExitException($L->comment_sending_server_error, 500);
80
		}
81
		$data             = $Comments->get($id);
82
		$data['comments'] = false;
83
		//TODO: get rid of this
84
		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...
85
	}
86
	/**
87
	 * @param \cs\Request $Request
88
	 *
89
	 * @throws ExitException
90
	 */
91
	static function index_put ($Request) {
92
		if (!User::instance()->user()) {
93
			throw new ExitException(403);
94
		}
95
		$id = $Request->route(0);
96
		if (!$id) {
97
			throw new ExitException(400);
98
		}
99
		$L    = Language::prefix('comments_');
100
		$text = $Request->data('text');
101
		if (!strip_tags($text)) {
102
			throw new ExitException($L->comment_cant_be_empty, 400);
103
		}
104
		$Comments = Comments::instance();
105
		$comment  = $Comments->get($id);
106
		if (!$comment) {
107
			throw new ExitException(404);
108
		}
109
		$allow = false;
110
		Event::instance()->fire(
111
			'api/Comments/edit',
112
			[
113
				'id'     => $comment['id'],
114
				'user'   => $comment['user'],
115
				'item'   => $comment['item'],
116
				'module' => $comment['module'],
117
				'allow'  => &$allow
118
			]
119
		);
120
		if (
121
			!$allow ||
122
			!$Comments->set($comment['id'], $text)
123
		) {
124
			throw new ExitException($L->comment_editing_server_error, 500);
125
		}
126
		//TODO: get rid of this
127
		return $Comments->get($comment['id'])['text'];
128
	}
129
	/**
130
	 * @param \cs\Request $Request
131
	 *
132
	 * @return string
133
	 *
134
	 * @throws ExitException
135
	 */
136
	static function index_delete ($Request) {
137
		if (!User::instance()->user()) {
138
			throw new ExitException(403);
139
		}
140
		$id = $Request->route(0);
141
		if (!$id) {
142
			throw new ExitException(400);
143
		}
144
		$Comments = Comments::instance();
145
		$comment  = $Comments->get($id);
146
		if (!$comment) {
147
			throw new ExitException(404);
148
		}
149
		$allow = false;
150
		Event::instance()->fire(
151
			'api/Comments/delete',
152
			[
153
				'id'     => $comment['id'],
154
				'user'   => $comment['user'],
155
				'item'   => $comment['item'],
156
				'module' => $comment['module'],
157
				'allow'  => &$allow
158
			]
159
		);
160
		$L = Language::prefix('comments_');
161
		if (
162
			!$allow ||
163
			!$Comments->del($comment['id'])
164
		) {
165
			throw new ExitException($L->comment_deleting_server_error, 500);
166
		}
167
		//TODO: get rid of this
168
		return '';
169
	}
170
}
171