Completed
Pull Request — master (#5)
by Jakub
08:28
created

manager::disable_expired_ads()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 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\admanagement\ad;
12
13
class manager
14
{
15
	/** @var \phpbb\db\driver\driver_interface */
16
	protected $db;
17
18
	/** @var string */
19
	protected $ads_table;
20
21
	/** @var string */
22
	protected $ad_locations_table;
23
24
	/**
25
	* Constructor
26
	*
27
	* @param	\phpbb\db\driver\driver_interface	$db					DB driver interface
28
	* @param	string								$ads_table			Ads table
29
	* @param	string								$ad_locations_table	Ad locations table
30
	*/
31 34
	public function __construct(\phpbb\db\driver\driver_interface $db, $ads_table, $ad_locations_table)
32
	{
33 34
		$this->db = $db;
34 34
		$this->ads_table = $ads_table;
35 34
		$this->ad_locations_table = $ad_locations_table;
36 34
	}
37
38
	/**
39
	* Get specific ad
40
	*
41
	* @param	int		$ad_id	Advertisement ID
42
	* @return	array	Advertisement data
43
	*/
44 4
	public function get_ad($ad_id)
45
	{
46
		$sql = 'SELECT *
47 4
			FROM ' . $this->ads_table . '
48 4
			WHERE ad_id = ' . (int) $ad_id;
49 4
		$result = $this->db->sql_query($sql);
50 4
		$data = $this->db->sql_fetchrow($result);
51 4
		$this->db->sql_freeresult($result);
52
53 4
		return $data;
54
	}
55
56
	/**
57
	* Get one ad per every location
58
	*
59
	* @param	array	$ad_locations	List of ad locations to fetch ads for
60
	* @return	array	List of ad codes for each location
61
	*/
62 1
	public function get_ads($ad_locations)
63
	{
64
		$sql = 'SELECT location_id, ad_code
65
			FROM (
66
				SELECT al.location_id, a.ad_code
67 1
				FROM ' . $this->ad_locations_table . ' al
68 1
				LEFT JOIN ' . $this->ads_table . ' a
69
					ON (al.ad_id = a.ad_id)
70
				WHERE a.ad_enabled = 1
71
					AND (a.ad_end_date = 0
72 1
						OR a.ad_end_date > ' . time() . ')
73 1
					AND ' . $this->db->sql_in_set('al.location_id', $ad_locations) . '
74 1
				ORDER BY ' . $this->sql_random() . '
75
			) z
76 1
			ORDER BY z.location_id';
77 1
		$result = $this->db->sql_query($sql);
78 1
		$data = $this->db->sql_fetchrowset($result);
79 1
		$this->db->sql_freeresult($result);
80
81 1
		return $data;
82
	}
83
84
	/**
85
	* Get all advertisements
86
	*
87
	* @return	array	List of all ads
88
	*/
89 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...
90
	{
91
		$sql = 'SELECT ad_id, ad_name, ad_enabled, ad_end_date
92 1
			FROM ' . $this->ads_table;
93 1
		$result = $this->db->sql_query($sql);
94 1
		$data = $this->db->sql_fetchrowset($result);
95 1
		$this->db->sql_freeresult($result);
96
97 1
		return $data;
98
	}
99
100
	/**
101
	* Insert new advertisement to the database
102
	*
103
	* @param	array	$data	New ad data
104
	* @return	int		New advertisement ID
105
	*/
106 1 View Code Duplication
	public function insert_ad($data)
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
		$data = $this->intersect_ad_data($data);
109
110 1
		$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
111 1
		$this->db->sql_query($sql);
112
113 1
		return $this->db->sql_nextid();
114
	}
115
116
	/**
117
	* Update advertisement
118
	*
119
	* @param	int		$ad_id	Advertisement ID
120
	* @param	array	$data	List of data to update in the database
121
	* @return	int		Number of affected rows. Can be used to determine if any ad has been updated.
122
	*/
123 6 View Code Duplication
	public function update_ad($ad_id, $data)
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...
124
	{
125 6
		$data = $this->intersect_ad_data($data);
126
127 6
		$sql = 'UPDATE ' . $this->ads_table . '
128 6
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
129 6
			WHERE ad_id = ' . (int) $ad_id;
130 6
		$this->db->sql_query($sql);
131
132 6
		return $this->db->sql_affectedrows();
133
	}
134
135
	/**
136
	* Delete advertisement
137
	*
138
	* @param	int		$ad_id	Advertisement ID
139
	* @return	int		Number of affected rows. Can be used to determine if any ad has been deleted.
140
	*/
141 2
	public function delete_ad($ad_id)
142
	{
143 2
		$sql = 'DELETE FROM ' . $this->ads_table . '
144 2
			WHERE ad_id = ' . (int) $ad_id;
145 2
		$this->db->sql_query($sql);
146
147 2
		return $this->db->sql_affectedrows();
148
	}
149
150
	/**
151
	* Disable expired advertisements
152
	*
153
	* @return	void
154
	*/
155 6
	public function disable_expired_ads()
156
	{
157 6
		$sql = 'UPDATE ' . $this->ads_table . '
158
			SET ad_enabled = 0
159 6
			WHERE ad_end_date < ' . time();
160 6
		$this->db->sql_query($sql);
161 6
	}
162
163
	/**
164
	* Get all locations for specified advertisement
165
	*
166
	* @param	int		$ad_id	Advertisement ID
167
	* @return	array	List of template locations for specified ad
168
	*/
169 1
	public function get_ad_locations($ad_id)
170
	{
171 1
		$ad_locations = array();
172
173
		$sql = 'SELECT location_id
174 1
			FROM ' . $this->ad_locations_table . '
175 1
			WHERE ad_id = ' . (int) $ad_id;
176 1
		$result = $this->db->sql_query($sql);
177 1
		while ($row = $this->db->sql_fetchrow($result))
178
		{
179 1
			$ad_locations[] = $row['location_id'];
180 1
		}
181 1
		$this->db->sql_freeresult($result);
182
183 1
		return $ad_locations;
184
	}
185
186
	/**
187
	* Insert advertisement locations
188
	*
189
	* @param	int		$ad_id			Advertisement ID
190
	* @param	array	$ad_locations	List of template locations for this ad
191
	* @return	void
192
	*/
193 2
	public function insert_ad_locations($ad_id, $ad_locations)
194
	{
195 2
		$sql_ary = array();
196 2
		foreach ($ad_locations as $ad_location)
197
		{
198 2
			$sql_ary[] = array(
199 2
				'ad_id'			=> $ad_id,
200 2
				'location_id'	=> $ad_location,
201
			);
202 2
		}
203 2
		$this->db->sql_multi_insert($this->ad_locations_table, $sql_ary);
204 2
	}
205
206
	/**
207
	* Delete advertisement locations
208
	*
209
	* @param	int		$ad_id	Advertisement ID
210
	* @return	void
211
	*/
212 3
	public function delete_ad_locations($ad_id)
213
	{
214 3
		$sql = 'DELETE FROM ' . $this->ad_locations_table . '
215 3
			WHERE ad_id = ' . (int) $ad_id;
216 3
		$this->db->sql_query($sql);
217 3
	}
218
219
	/**
220
	* Make sure only necessary data make their way to SQL query
221
	*
222
	* @param	array	$data	List of data to query the database
223
	* @return	array	Cleaned data that contain only valid keys
224
	*/
225 7
	protected function intersect_ad_data($data)
226
	{
227 7
		return array_intersect_key($data, array(
228 7
			'ad_name'		=> '',
229 7
			'ad_note'		=> '',
230 7
			'ad_code'		=> '',
231 7
			'ad_enabled'	=> '',
232 7
			'ad_end_date'	=> '',
233 7
		));
234
	}
235
236
	/**
237
	* Get the random statement for this database layer
238
	*
239
	* @return	string	Random statement for current database layer
240
	*/
241 1
	protected function sql_random()
242
	{
243 1
		switch ($this->db->get_sql_layer())
244
		{
245 1
			case 'oracle':
246 1
			case 'postgres':
247 1
			case 'sqlite':
248 1
			case 'sqlite3':
249
				return 'RANDOM()';
250
251
			/* 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...
252
			case 'mssql':
253
			case 'mssql_odbc':
254
			case 'mssqlnative':
255
			case 'mysql':
256
			case 'mysqli':*/
257 1
			default:
258 1
				return 'RAND()';
259 1
		}
260
	}
261
}
262