Completed
Pull Request — master (#62)
by Matt
09:08
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 97.48%

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 116
cts 119
cp 0.9748
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
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
B banner_upload() 0 30 5
A validate_ad_name() 0 11 3
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 19
	public function __construct(\phpbb\user $user, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
46
	{
47 19
		$this->user = $user;
48 19
		$this->language = $language;
49 19
		$this->request = $request;
50 19
		$this->banner = $banner;
51 19
	}
52
53
	/**
54
	 * Gets all errors
55
	 *
56
	 * @return	array	Errors
57
	 */
58 14
	public function get_errors()
59
	{
60 14
		return $this->errors;
61
	}
62
63
	/**
64
	 * Returns number of errors.
65
	 *
66
	 * @return	int	Number of errors
67
	 */
68 14
	public function has_errors()
69
	{
70 14
		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 12
	public function get_form_data($form_name)
80
	{
81
		$data = array(
82 12
			'ad_name'         => $this->request->variable('ad_name', '', true),
83 12
			'ad_note'         => $this->request->variable('ad_note', '', true),
84 12
			'ad_code'         => $this->request->variable('ad_code', '', true),
85 12
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
86 12
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
87 12
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
88 12
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
89 12
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
90 12
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
91 12
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
92 12
		);
93
94
		// Validate form key
95 12
		if (!check_form_key($form_name))
96 12
		{
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 12
		foreach ($data as $prop_name => $prop_val)
102
		{
103 12
			if (method_exists($this, 'validate_' . $prop_name))
104 12
			{
105 12
				$this->{'validate_' . $prop_name}($prop_val);
106 12
			}
107 12
		}
108
109
		// Replace end date and owner with IDs that will be stored in the DB
110 12
		$data['ad_end_date'] = $this->end_date_to_timestamp($data['ad_end_date']);
111 12
		if (!in_array('AD_OWNER_INVALID', $this->errors))
112 12
		{
113 10
			$data['ad_owner'] = $this->owner_to_id($data['ad_owner']);
114 10
		}
115
116 12
		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 7
	public function banner_upload($ad_code)
126
	{
127
		try
128
		{
129 7
			$this->banner->create_storage_dir();
130 4
			$realname = $this->banner->upload();
131
132 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
133
134 3
			if ($this->request->is_ajax())
135 3
			{
136 1
				$this->send_ajax_response(true, $banner_html);
137
			}
138
139 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
140
		}
141 7
		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 4
			$this->banner->remove();
144
145 4
			if ($this->request->is_ajax())
146 4
			{
147 1
				$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 12
	protected function validate_ad_name($ad_name)
162
	{
163 12
		if ($ad_name === '')
164 12
		{
165 2
			$this->errors[] = 'AD_NAME_REQUIRED';
166 2
		}
167 12
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
168 12
		{
169 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
170 1
		}
171 12
	}
172
173
	/**
174
	 * Validate advertisement end date
175
	 *
176
	 * @param string $end_date Advertisement end date
177
	 */
178 12
	protected function validate_ad_end_date($end_date)
179
	{
180 12
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
181 12
		{
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 10
		else if ($end_date !== '')
190 10
		{
191 2
			$this->errors[] = 'AD_END_DATE_INVALID';
192 2
		}
193 12
	}
194
195
	/**
196
	 * Validate advertisement priority
197
	 *
198
	 * @param int $ad_priority Advertisement priority
199
	 */
200 12
	protected function validate_ad_priority($ad_priority)
201
	{
202 12
		if ($ad_priority < 1 || $ad_priority > 10)
203 12
		{
204 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
205 3
		}
206 12
	}
207
208
	/**
209
	 * Validate advertisement views limit
210
	 *
211
	 * @param int $ad_views_limit Advertisement views limit
212
	 */
213 12
	protected function validate_ad_views_limit($ad_views_limit)
214
	{
215 12
		if ($ad_views_limit < 0)
216 12
		{
217 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
218 2
		}
219 12
	}
220
221
	/**
222
	 * Validate advertisement clicks limit
223
	 *
224
	 * @param int $ad_clicks_limit Advertisement clicks limit
225
	 */
226 12
	protected function validate_ad_clicks_limit($ad_clicks_limit)
227
	{
228 12
		if ($ad_clicks_limit < 0)
229 12
		{
230 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
231 2
		}
232 12
	}
233
234
	/**
235
	 * Validate advertisement owner
236
	 *
237
	 * @param string $ad_owner Advertisement owner
238
	 */
239 12
	protected function validate_ad_owner($ad_owner)
240
	{
241
		// user_get_id_name function returns false if everything is OK.
242 12
		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 12
		{
244 2
			$this->errors[] = 'AD_OWNER_INVALID';
245 2
		}
246 12
	}
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 12
	protected function end_date_to_timestamp($end_date)
255
	{
256 12
		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 10
	protected function owner_to_id($ad_owner)
266
	{
267 10
		if (empty($ad_owner))
268 10
		{
269 9
			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 2
	protected function send_ajax_response($success, $text)
283
	{
284 2
		$json_response = new \phpbb\json_response;
285 2
		$json_response->send(array(
286 2
			'success'	=> $success,
287 2
			'title'		=> $this->language->lang('INFORMATION'),
288 2
			'text'		=> $text,
289 2
		));
290
	}
291
}
292