Completed
Pull Request — master (#47)
by Matt
76:34 queued 41:31
created

manager::increment_ads_views()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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\ad;
12
13
class manager
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
16
	protected $db;
17
18
	/** @var \phpbb\config\config */
19
	protected $config;
20
21
	/** @var string */
22
	protected $ads_table;
23
24
	/** @var string */
25
	protected $ad_locations_table;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param    \phpbb\db\driver\driver_interface $db                 DB driver interface
31 46
	 * @param    \phpbb\config\config              $config             Config object
32
	 * @param    string                            $ads_table          Ads table
33 46
	 * @param    string                            $ad_locations_table Ad locations table
34 46
	 */
35 46
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $ads_table, $ad_locations_table)
36 46
	{
37
		$this->db = $db;
38
		$this->config = $config;
39
		$this->ads_table = $ads_table;
40
		$this->ad_locations_table = $ad_locations_table;
41
	}
42
43
	/**
44 4
	* Get specific ad
45
	*
46
	* @param	int		$ad_id	Advertisement ID
47 4
	* @return	mixed	Array with advertisement data, false if ad doesn't exist
48 4
	*/
49 4 View Code Duplication
	public function get_ad($ad_id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 4
	{
51 4
		$sql = 'SELECT *
52
			FROM ' . $this->ads_table . '
53 4
			WHERE ad_id = ' . (int) $ad_id;
54
		$result = $this->db->sql_query($sql);
55
		$data = $this->db->sql_fetchrow($result);
56
		$this->db->sql_freeresult($result);
57
58
		return $data;
59
	}
60
61
	/**
62 2
	* Get one ad per every location
63
	*
64
	* @param	array	$ad_locations	List of ad locations to fetch ads for
65
	* @return	array	List of ad codes for each location
66
	*/
67 2
	public function get_ads($ad_locations)
68 2
	{
69
		$sql_where_views = $this->config['phpbb_ads_enable_views'] ? 'AND (a.ad_views_limit = 0 OR a.ad_views_limit > a.ad_views)' : '';
70
		$sql_where_clicks = $this->config['phpbb_ads_enable_clicks'] ? 'AND (a.ad_clicks_limit = 0 OR a.ad_clicks_limit > a.ad_clicks)' : '';
71
72 2
		$sql = 'SELECT location_id, ad_id, ad_code
73 2
			FROM (
74 2
				SELECT al.location_id, a.ad_id, a.ad_code
75
				FROM ' . $this->ad_locations_table . ' al
76 2
				LEFT JOIN ' . $this->ads_table . " a
77 2
					ON (al.ad_id = a.ad_id)
78 2
				WHERE a.ad_enabled = 1
79 2
					AND (a.ad_end_date = 0
80
						OR a.ad_end_date > " . time() . ")
81 2
					$sql_where_views
82
					$sql_where_clicks
83
					AND " . $this->db->sql_in_set('al.location_id', $ad_locations) . '
84
				ORDER BY (' . $this->sql_random() . ' * a.ad_priority) DESC
85
			) z
86
			ORDER BY z.location_id';
87
		$result = $this->db->sql_query($sql);
88
		$data = $this->db->sql_fetchrowset($result);
89 1
		$this->db->sql_freeresult($result);
90
91
		$current_location_id = '';
92 1
		$data = array_filter($data, function($row) use (&$current_location_id) {
93 1
			$return = $current_location_id != $row['location_id'];
94 1
			$current_location_id = $row['location_id'];
95 1
			return $return;
96
		});
97 1
98
		return $data;
99
	}
100
101
	/**
102
	* Get all advertisements
103
	*
104
	* @return	array	List of all ads
105
	*/
106 1 View Code Duplication
	public function get_all_ads()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
	{
108 1
		$sql = 'SELECT ad_id, ad_name, ad_enabled, ad_end_date, ad_views, ad_clicks, ad_views_limit, ad_clicks_limit
109
			FROM ' . $this->ads_table;
110 1
		$result = $this->db->sql_query($sql);
111 1
		$data = $this->db->sql_fetchrowset($result);
112
		$this->db->sql_freeresult($result);
113 1
114
		return $data;
115
	}
116
117
	/**
118
	* Increment views for specified ads
119
	*
120
	* Note, that views are incremented only by one even when
121
	* an ad is displayed multiple times on the same page.
122
	*
123 7
	* @param	array	$ad_ids	IDs of ads to increment views
124
	* @return	void
125 7
	*/
126
	public function increment_ads_views($ad_ids)
127 7
	{
128 7
		if (!empty($ad_ids))
129 7
		{
130 7
			$sql = 'UPDATE ' . $this->ads_table . '
131
				SET ad_views = ad_views + 1
132 7
				WHERE ' . $this->db->sql_in_set('ad_id', $ad_ids);
133
			$this->db->sql_query($sql);
134
		}
135
	}
136
137
	/**
138
	* Increment clicks for specified ad
139
	*
140
	* @param	int	$ad_id	ID of an ad to increment clicks
141 2
	* @return	void
142
	*/
143 2
	public function increment_ad_clicks($ad_id)
144 2
	{
145 2
		$sql = 'UPDATE ' . $this->ads_table . '
146
			SET ad_clicks = ad_clicks + 1
147 2
			WHERE ad_id = ' . (int) $ad_id;
148
		$this->db->sql_query($sql);
149
	}
150
151
	/**
152
	* Insert new advertisement to the database
153
	*
154
	* @param	array	$data	New ad data
155
	* @return	int		New advertisement ID
156 1
	*/
157 View Code Duplication
	public function insert_ad($data)
158 1
	{
159
		$data = $this->intersect_ad_data($data);
160
161 1
		$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
162 1
		$this->db->sql_query($sql);
163 1
164 1
		return $this->db->sql_nextid();
165
	}
166 1
167 1
	/**
168 1
	* Update advertisement
169
	*
170 1
	* @param	int		$ad_id	Advertisement ID
171
	* @param	array	$data	List of data to update in the database
172
	* @return	int		Number of affected rows. Can be used to determine if any ad has been updated.
173
	*/
174 View Code Duplication
	public function update_ad($ad_id, $data)
175
	{
176
		$data = $this->intersect_ad_data($data);
177
178
		$sql = 'UPDATE ' . $this->ads_table . '
179
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
180 2
			WHERE ad_id = ' . (int) $ad_id;
181
		$this->db->sql_query($sql);
182 2
183 2
		return $this->db->sql_affectedrows();
184
	}
185 2
186 2
	/**
187 2
	* Delete advertisement
188
	*
189 2
	* @param	int		$ad_id	Advertisement ID
190 2
	* @return	int		Number of affected rows. Can be used to determine if any ad has been deleted.
191 2
	*/
192
	public function delete_ad($ad_id)
193
	{
194
		$sql = 'DELETE FROM ' . $this->ads_table . '
195
			WHERE ad_id = ' . (int) $ad_id;
196
		$this->db->sql_query($sql);
197
198
		return $this->db->sql_affectedrows();
199 3
	}
200
201 3
	/**
202 3
	* Get all locations for specified advertisement
203 3
	*
204 3
	* @param	int		$ad_id	Advertisement ID
205
	* @return	array	List of template locations for specified ad
206
	*/
207 View Code Duplication
	public function get_ad_locations($ad_id)
208
	{
209
		$ad_locations = array();
210
211
		$sql = 'SELECT location_id
212 4
			FROM ' . $this->ad_locations_table . '
213
			WHERE ad_id = ' . (int) $ad_id;
214 4
		$result = $this->db->sql_query($sql);
215
		while ($row = $this->db->sql_fetchrow($result))
216 4
		{
217 4
			$ad_locations[] = $row['location_id'];
218 4
		}
219 4
		$this->db->sql_freeresult($result);
220 4
221
		return $ad_locations;
222 4
	}
223 4
224 4
	/**
225 4
	* Insert advertisement locations
226
	*
227
	* @param	int		$ad_id			Advertisement ID
228
	* @param	array	$ad_locations	List of template locations for this ad
229
	* @return	void
230
	*/
231
	public function insert_ad_locations($ad_id, $ad_locations)
232
	{
233 2
		$sql_ary = array();
234
		foreach ($ad_locations as $ad_location)
235
		{
236 2
			$sql_ary[] = array(
237 2
				'ad_id'			=> $ad_id,
238 2
				'location_id'	=> $ad_location,
239 2
			);
240 2
		}
241
		$this->db->sql_multi_insert($this->ad_locations_table, $sql_ary);
242 2
	}
243
244
	/**
245
	* Delete advertisement locations
246
	*
247
	* @param	int		$ad_id	Advertisement ID
248
	* @return	void
249
	*/
250
	public function delete_ad_locations($ad_id)
251 8
	{
252
		$sql = 'DELETE FROM ' . $this->ad_locations_table . '
253 8
			WHERE ad_id = ' . (int) $ad_id;
254 8
		$this->db->sql_query($sql);
255 8
	}
256 8
257 8
	/**
258 8
	* Load memberships of the user
259 8
	*
260 8
	* @param	int		$user_id	User ID to load memberships
261
	* @return	array	List of group IDs user is member of
262
	*/
263 View Code Duplication
	public function load_memberships($user_id)
264
	{
265
		$memberships = array();
266
		$sql = 'SELECT group_id
267
			FROM ' . USER_GROUP_TABLE . '
268
			WHERE user_id = ' . (int) $user_id . '
269 2
			AND user_pending = 0';
270
		$result = $this->db->sql_query($sql, 3600);
271 2
		while ($row = $this->db->sql_fetchrow($result))
272
		{
273 2
			$memberships[] = $row['group_id'];
274
		}
275
		$this->db->sql_freeresult($result);
276 2
		return $memberships;
277
	}
278
279
	/**
280 2
	* Load all board groups
281 2
	*
282
	* @return	array	List of groups
283
	*/
284 View Code Duplication
	public function load_groups()
285
	{
286
		$sql = 'SELECT group_id, group_name, group_type
287
			FROM ' . GROUPS_TABLE . '
288
			ORDER BY group_name ASC';
289
		$result = $this->db->sql_query($sql);
290 2
		$groups = $this->db->sql_fetchrowset($result);
291 2
		$this->db->sql_freeresult($result);
292 2
293
		return $groups;
294
	}
295
296
	/**
297
	* Make sure only necessary data make their way to SQL query
298
	*
299
	* @param	array	$data	List of data to query the database
300
	* @return	array	Cleaned data that contain only valid keys
301
	*/
302
	protected function intersect_ad_data($data)
303
	{
304
		return array_intersect_key($data, array(
305
			'ad_name'			=> '',
306
			'ad_note'			=> '',
307
			'ad_code'			=> '',
308
			'ad_enabled'		=> '',
309
			'ad_end_date'		=> '',
310
			'ad_priority'		=> '',
311
			'ad_views_limit'	=> '',
312
			'ad_clicks_limit'	=> '',
313
		));
314
	}
315
316
	/**
317
	* Get the random statement for this database layer
318
	* Random function should generate a float value between 0 and 1
319
	*
320
	* @return	string	Random statement for current database layer
321
	*/
322
	protected function sql_random()
323
	{
324
		switch ($this->db->get_sql_layer())
325
		{
326
			case 'oracle':
327
				return 'VALUE()';
328
329
			case 'postgres':
330
				return 'RANDOM()';
331
332
			// https://stackoverflow.com/a/35369410/2908600
333
			case 'sqlite':
334
			case 'sqlite3':
335
				return '(0.5 - RANDOM() / CAST(-9223372036854775808 AS REAL) / 2)';
336
337
			/* All other cases should use the default
1 ignored issue
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
338
			case 'mssql':
339
			case 'mssql_odbc':
340
			case 'mssqlnative':
341
			case 'mysql':
342
			case 'mysqli':*/
343
			default:
344
				return 'RAND()';
345
		}
346
	}
347
}
348