Completed
Pull Request — master (#56)
by Jakub
34:46 queued 32:33
created

admin_input   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.24%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 34
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 276
ccs 105
cts 119
cp 0.8824
rs 9.2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get_errors() 0 4 1
A has_errors() 0 4 1
B get_form_data() 0 39 5
B banner_upload() 0 30 5
A validate_ad_name() 0 11 3
A validate_ad_end_date() 0 16 4
A validate_ad_priority() 0 7 3
A validate_ad_views_limit() 0 7 2
A validate_ad_clicks_limit() 0 7 2
A validate_ad_owner() 0 8 3
A end_date_to_timestamp() 0 4 1
A owner_to_id() 0 10 2
A send_ajax_response() 0 9 1
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
/**
14
 * Admin input
15
 */
16
class admin_input
17
{
18
	const MAX_NAME_LENGTH = 255;
19
	const DATE_FORMAT = 'Y-m-d';
20
	const DEFAULT_PRIORITY = 5;
21
22
	/** @var \phpbb\user */
23
	protected $user;
24
25
	/** @var \phpbb\language\language */
26
	protected $language;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\ads\banner\banner */
32
	protected $banner;
33
34
	/** @var array Form validation errors */
35
	protected $errors = array();
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \phpbb\user								$user			User object
41
	 * @param \phpbb\language\language                  $language       Language object
42
	 * @param \phpbb\request\request					$request		Request object
43
	 * @param \phpbb\ads\banner\banner					$banner			Banner upload object
44
	 */
45 16
	public function __construct(\phpbb\user $user, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
46
	{
47 16
		$this->user = $user;
48 16
		$this->language = $language;
49 16
		$this->request = $request;
50 16
		$this->banner = $banner;
51 16
	}
52
53
	/**
54
	 * Gets all errors
55
	 *
56
	 * @return	array	Errors
57
	 */
58 13
	public function get_errors()
59
	{
60 13
		return $this->errors;
61
	}
62
63
	/**
64
	 * Returns number of errors.
65
	 *
66
	 * @return	int	Number of errors
67
	 */
68 13
	public function has_errors()
69
	{
70 13
		return count($this->errors);
71
	}
72
73
	/**
74
	 * Get admin form data.
75
	 *
76
	 * @param	string	$form_name	The form name.
77
	 * @return	array	Form data
78
	 */
79 11
	public function get_form_data($form_name)
80
	{
81
		$data = array(
82 11
			'ad_name'         => $this->request->variable('ad_name', '', true),
83 11
			'ad_note'         => $this->request->variable('ad_note', '', true),
84 11
			'ad_code'         => $this->request->variable('ad_code', '', true),
85 11
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
86 11
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
87 11
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
88 11
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
89 11
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
90 11
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
91 11
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
92 11
		);
93
94
		// Validate form key
95 11
		if (!check_form_key($form_name))
96 11
		{
97 2
			$this->errors[] = $this->language->lang('FORM_INVALID');
98 2
		}
99
100
		// Validate each property. Every method adds errors directly to $this->errors.
101 11
		foreach ($data as $prop_name => $prop_val)
102
		{
103 11
			if (method_exists($this, 'validate_' . $prop_name))
104 11
			{
105 11
				$this->{'validate_' . $prop_name}($prop_val);
106 11
			}
107 11
		}
108
109
		// Replace end date and owner with IDs that will be stored in the DB
110 11
		$data['ad_end_date'] = $this->end_date_to_timestamp($data['ad_end_date']);
111 11
		if (!in_array('AD_OWNER_INVALID', $this->errors))
112 11
		{
113 9
			$data['ad_owner'] = $this->owner_to_id($data['ad_owner']);
114 9
		}
115
116 11
		return $data;
117
	}
118
119
	/**
120
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
121
	 *
122
	 * @param	 string	 $ad_code	 Current ad code
123
	 * @return	 mixed	 \phpbb\json_response when request is ajax or updated ad code otherwise.
124
	 */
125 5
	public function banner_upload($ad_code)
126
	{
127
		try
128
		{
129 5
			$this->banner->create_storage_dir();
130 3
			$realname = $this->banner->upload();
131
132 2
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
133
134 2
			if ($this->request->is_ajax())
135 2
			{
136
				$this->send_ajax_response(true, $banner_html);
137
			}
138
139 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
140
		}
141 5
		catch (\phpbb\exception\runtime_exception $e)
0 ignored issues
show
Bug introduced by
The class phpbb\exception\runtime_exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
142
		{
143 3
			$this->banner->remove();
144
145 3
			if ($this->request->is_ajax())
146 3
			{
147
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
148
			}
149
150 3
			$this->errors[] = $this->language->lang($e->getMessage());
151
		}
152
153 5
		return $ad_code;
154
	}
155
156
	/**
157
	 * Validate advertisement name
158
	 *
159
	 * @param string $ad_name Advertisement name
160
	 */
161 11
	protected function validate_ad_name($ad_name)
162
	{
163 11
		if ($ad_name === '')
164 11
		{
165 2
			$this->errors[] = 'AD_NAME_REQUIRED';
166 2
		}
167 11
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
168 11
		{
169
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
170
		}
171 11
	}
172
173
	/**
174
	 * Validate advertisement end date
175
	 *
176
	 * @param string $end_date Advertisement end date
177
	 */
178 11
	protected function validate_ad_end_date($end_date)
179
	{
180 11
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
181 11
		{
182 2
			$end_date = (int) $this->end_date_to_timestamp($end_date);
183
184 2
			if ($end_date < time())
185 2
			{
186 1
				$this->errors[] = 'AD_END_DATE_INVALID';
187 1
			}
188 2
		}
189 9
		else if ($end_date !== '')
190 9
		{
191 2
			$this->errors[] = 'AD_END_DATE_INVALID';
192 2
		}
193 11
	}
194
195
	/**
196
	 * Validate advertisement priority
197
	 *
198
	 * @param int $ad_priority Advertisement priority
199
	 */
200 11
	protected function validate_ad_priority($ad_priority)
201
	{
202 11
		if ($ad_priority < 1 || $ad_priority > 10)
203 11
		{
204 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
205 3
		}
206 11
	}
207
208
	/**
209
	 * Validate advertisement views limit
210
	 *
211
	 * @param int $ad_views_limit Advertisement views limit
212
	 */
213 11
	protected function validate_ad_views_limit($ad_views_limit)
214
	{
215 11
		if ($ad_views_limit < 0)
216 11
		{
217 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
218 2
		}
219 11
	}
220
221
	/**
222
	 * Validate advertisement clicks limit
223
	 *
224
	 * @param int $ad_clicks_limit Advertisement clicks limit
225
	 */
226 11
	protected function validate_ad_clicks_limit($ad_clicks_limit)
227
	{
228 11
		if ($ad_clicks_limit < 0)
229 11
		{
230 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
231 2
		}
232 11
	}
233
234
	/**
235
	 * Validate advertisement owner
236
	 *
237
	 * @param string $ad_owner Advertisement owner
238
	 */
239 11
	protected function validate_ad_owner($ad_owner)
240
	{
241
		// user_get_id_name function returns false if everything is OK.
242 11
		if (!empty($ad_owner) && user_get_id_name($ad_owner_id, $ad_owner))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
243 11
		{
244 2
			$this->errors[] = 'AD_OWNER_INVALID';
245 2
		}
246 11
	}
247
248
	/**
249
	 * Convert format of end date from string to unix timestamp
250
	 *
251
	 * @param string $end_date Advertisement end date in YYYY-MM-DD format
252
	 * @return int Advertisement end date in unix timestamp
253
	 */
254 11
	protected function end_date_to_timestamp($end_date)
255
	{
256 11
		return (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $end_date);
257
	}
258
259
	/**
260
	 * Convert advertisement owner username to ID
261
	 *
262
	 * @param string $ad_owner Advertisement owner username
263
	 * @return int Advertisement owner ID
264
	 */
265 9
	protected function owner_to_id($ad_owner)
266
	{
267 9
		if (empty($ad_owner))
268 9
		{
269 8
			return 0;
270
		}
271
272 1
		user_get_id_name($ad_owner_id, $ad_owner);
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
273 1
		return $ad_owner_id[0];
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
274
	}
275
276
	/**
277
	 * Send ajax response
278
	 *
279
	 * @param bool $success Is request successful?
280
	 * @param string $text Text to return
281
	 */
282
	protected function send_ajax_response($success, $text)
283
	{
284
		$json_response = new \phpbb\json_response;
285
		$json_response->send(array(
286
			'success'	=> $success,
287
			'title'		=> $this->language->lang('INFORMATION'),
288
			'text'		=> $text,
289
		));
290
	}
291
}
292