Completed
Push — develop ( 29678f...b18c7a )
by Daniel
14:18
created

data   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 337
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.53%

Importance

Changes 18
Bugs 5 Features 4
Metric Value
wmc 44
c 18
b 5
f 4
lcom 1
cbo 1
dl 0
loc 337
ccs 121
cts 128
cp 0.9453
rs 8.3396

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A get_topics_count() 0 14 1
A get_topic_data() 0 18 2
A get_post_data() 0 18 2
A get_attachments() 0 20 3
A get_topic_tracking_info() 0 11 4
A get_topic_post_ids() 0 4 2
A get_posters_info() 0 11 2
A _get_posts_sql_array() 0 17 1
A _get_post_data_where() 0 17 3
A _limit_posts_by_topic() 0 9 2
A _get_tracking_info() 0 14 3
A _build_tracking_info() 0 10 2
A _can_track_by_lastread() 0 4 3
A _can_track_anonymous() 0 4 3
A _attachments_allowed() 0 4 3
A _user_can_download_attachments() 0 4 4
A _get_attachment_sql() 0 9 3

How to fix   Complexity   

Complex Class

Complex classes like data often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use data, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\forum;
11
12
class data extends query_builder
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\content_visibility */
21
	protected $content_visibility;
22
23
	/** @var \phpbb\db\driver\driver_interface */
24
	protected $db;
25
26
	/** @var \phpbb\user */
27
	protected $user;
28
29
	/** @var \blitze\sitemaker\services\user_data */
30
	protected $user_data;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\auth\auth						$auth					Auth object
36
	 * @param \phpbb\config\config					$config					Config object
37
	 * @param \phpbb\content_visibility				$content_visibility		Content visibility
38
	 * @param \phpbb\db\driver\driver_interface		$db     				Database connection
39
	 * @param \phpbb\user							$user					User object
40
	 * @param \blitze\sitemaker\services\user_data	$user_data				Sitemaker User data object
41
	 * @param integer								$cache_time				Cache results for 3 hours by default
42
	 */
43
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, \blitze\sitemaker\services\user_data $user_data, $cache_time = 10800)
44
	{
45
		parent::__construct($auth, $config, $content_visibility, $db, $user, $cache_time);
46
47
		$this->auth = $auth;
48
		$this->config = $config;
49
		$this->content_visibility = $content_visibility;
50
		$this->db = $db;
51 29
		$this->user = $user;
52
		$this->user_data = $user_data;
53 29
	}
54
55 29
	/**
56 29
	 * Get topics count
57 29
	 *
58 29
	 * @return integer
59 29
	 */
60 29
	public function get_topics_count()
61 29
	{
62 29
		$sql_array = array(
63 29
			'SELECT'	=> 'COUNT(*) AS total_topics',
64
			'FROM'		=> $this->store['sql_array']['FROM'],
65
			'WHERE'		=> $this->store['sql_array']['WHERE'],
66
		);
67
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
68
		$result = $this->db->sql_query($sql);
69
		$total_topics = $this->db->sql_fetchfield('total_topics');
70 1
		$this->db->sql_freeresult($result);
71
72
		return $total_topics;
73 1
	}
74 1
75 1
	/**
76 1
	 * Get topic data
77 1
	 *
78 1
	 * @param mixed|false $limit
79 1
	 * @param int $start
80 1
	 * @return array
81
	 */
82 1
	public function get_topic_data($limit = false, $start = 0)
83
	{
84
		$sql = $this->db->sql_build_query('SELECT', $this->store['sql_array']);
85
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
86
87
		while ($row = $this->db->sql_fetchrow($result))
88
		{
89
			$this->store['topic'][$row['topic_id']] = $row;
90
91
			$this->store['tracking'][$row['forum_id']]['topic_list'][] = $row['topic_id'];
92 12
			$this->store['tracking'][$row['forum_id']]['mark_time'] =& $row['forum_mark_time'];
93
			$this->store['post_ids']['first'][] = $row['topic_first_post_id'];
94 12
			$this->store['post_ids']['last'][] = $row['topic_last_post_id'];
95 12
		}
96
		$this->db->sql_freeresult($result);
97 12
98
		return $this->store['topic'];
99 10
	}
100
101 10
	/**
102 10
	 * Get post data
103 10
	 *
104 10
	 * @param mixed|false $topic_first_or_last_post (first|last)
105 10
	 * @param array $post_ids
106 12
	 * @param bool|false $limit
107
	 * @param int $start
108 12
	 * @param array $sql_array
109
	 * @return array
110
	 */
111
	public function get_post_data($topic_first_or_last_post = false, $post_ids = array(), $limit = false, $start = 0, $sql_array = array())
112
	{
113
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', $this->_get_posts_sql_array($topic_first_or_last_post, $post_ids, $sql_array));
114
		$result = $this->db->sql_query_limit($sql, $limit, $start, $this->cache_time);
115
116
		$post_data = array();
117
		while ($row = $this->db->sql_fetchrow($result))
118
		{
119
			$post_data[$row['topic_id']][$row['post_id']] = $row;
120
			$this->store['poster_ids'][] = $row['poster_id'];
121 10
			$this->store['poster_ids'][] = $row['post_edit_user'];
122
			$this->store['poster_ids'][] = $row['post_delete_user'];
123 10
			$this->store['attachments'][] = $row['post_id'];
124 10
		}
125
		$this->db->sql_freeresult($result);
126 10
127 10
		return $post_data;
128
	}
129 9
130 9
	/**
131 9
	 * Get attachments...
132 9
	 *
133 9
	 * @param int $forum_id
134 9
	 * @param array $allowed_extensions
135 10
	 * @param mixed|bool $limit
136
	 * @param bool $exclude_in_message
137 10
	 * @param string $order_by
138
	 * @return array
139
	 */
140
	public function get_attachments($forum_id = 0, $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC')
141
	{
142
		$this->store['attachments'] = array_filter($this->store['attachments']);
143
144
		$attachments = array();
145
		if ($this->_attachments_allowed($forum_id))
146
		{
147
			$sql = $this->_get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by);
148
			$result = $this->db->sql_query_limit($sql, $limit);
149
150 7
			while ($row = $this->db->sql_fetchrow($result))
151
			{
152 7
				$attachments[$row['post_msg_id']][] = $row;
153
			}
154 7
			$this->db->sql_freeresult($result);
155 7
		}
156 7
		$this->store['attachments'] = array();
157 6
158 6
		return $attachments;
159
	}
160 6
161
	/**
162 6
	 * Get topic tracking info
163 6
	 *
164 6
	 * @param int $forum_id
165 6
	 * @return array
166 7
	 */
167
	public function get_topic_tracking_info($forum_id = 0)
168 7
	{
169
		if (!sizeof($this->store['tracking']))
170
		{
171
			return array();
172
		}
173
174
		$tracking_info = $this->_get_tracking_info();
175
176
		return ($forum_id) ? (isset($tracking_info[$forum_id]) ? $tracking_info[$forum_id] : array()) : $tracking_info;
177 5
	}
178
179 5
	/**
180 5
	 * Returns an array of topic first post or last post ids
181
	 *
182
	 * @param string $first_or_last_post
183
	 * @return array
184 5
	 */
185
	public function get_topic_post_ids($first_or_last_post = 'first')
186 5
	{
187
		return (isset($this->store['post_ids'][$first_or_last_post])) ? $this->store['post_ids'][$first_or_last_post] : array();
188
	}
189
190
	/**
191
	 * Returns an array of topic first post or last post ids
192
	 */
193
	public function get_posters_info()
194
	{
195 3
		$this->store['poster_ids'] = array_filter(array_unique($this->store['poster_ids']));
196
197 3
		if (!sizeof($this->store['poster_ids']))
198
		{
199
			return array();
200
		}
201
202
		return $this->user_data->get_ids($this->store['poster_ids']);
0 ignored issues
show
Bug introduced by
The method get_ids() does not seem to exist on object<blitze\sitemaker\services\user_data>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
	}
204
205
	/**
206
	 * @param mixed $topic_first_or_last_post
207
	 * @param array $post_ids
208
	 * @param array $sql_array
209
	 * @return array
210
	 */
211
	private function _get_posts_sql_array($topic_first_or_last_post, array $post_ids, array $sql_array)
212
	{
213
		$sql_array = array_merge_recursive(
214
			array(
215
				'SELECT'	=> array('p.*'),
216
				'FROM'		=> array(POSTS_TABLE => 'p'),
217
				'WHERE'		=> $this->_get_post_data_where($post_ids, $topic_first_or_last_post),
218
				'ORDER_BY'	=> 'p.post_time DESC',
219
			),
220
			$sql_array
221 10
		);
222
223 10
		$sql_array['SELECT'] = join(', ', array_filter($sql_array['SELECT']));
224
		$sql_array['WHERE'] = join(' AND ', array_filter($sql_array['WHERE']));
225 10
226 10
		return $sql_array;
227 10
	}
228 10
229 10
	/**
230
	 * @param array $post_ids
231 10
	 * @param string $topic_first_or_last_post
232
	 * @return array
233 10
	 */
234 10
	private function _get_post_data_where(array $post_ids, $topic_first_or_last_post)
235
	{
236 10
		$sql_where = array();
237
238
		if (sizeof($post_ids))
239
		{
240
			$sql_where[] = $this->db->sql_in_set('p.post_id', $post_ids);
241
		}
242
		else if (sizeof($this->store['topic']))
243
		{
244 10
			$this->_limit_posts_by_topic($sql_where, $topic_first_or_last_post);
245
		}
246 10
247
		$sql_where[] = $this->content_visibility->get_global_visibility_sql('post', $this->ex_fid_ary, 'p.');
248 10
249 10
		return $sql_where;
250 2
	}
251 2
252 8
	/**
253 8
	 * @param array $sql_where
254 3
	 * @param string $topic_first_or_last_post
255 3
	 */
256
	private function _limit_posts_by_topic(array &$sql_where, $topic_first_or_last_post)
257 10
	{
258
		$sql_where[] = $this->db->sql_in_set('p.topic_id', array_keys($this->store['topic']));
259 10
260
		if ($topic_first_or_last_post)
261
		{
262
			$sql_where[] = $this->db->sql_in_set('p.post_id', $this->get_topic_post_ids($topic_first_or_last_post));
263
		}
264
	}
265
266 3
	/**
267
	 * @return array
268 3
	 */
269
	private function _get_tracking_info()
270
	{
271 3
		$info = array();
272 3
		if ($this->_can_track_by_lastread())
273 3
		{
274 3
			$info = $this->_build_tracking_info('get_topic_tracking');
275
		}
276
		else if ($this->_can_track_anonymous())
277
		{
278
			$info = $this->_build_tracking_info('get_complete_topic_tracking');
279 5
		}
280
281 5
		return $info;
282 5
	}
283 5
284 4
	/**
285 4
	 * @param string $function
286 1
	 * @return array
287 1
	 */
288 1
	private function _build_tracking_info($function)
289 1
	{
290
		$tracking_info = array();
291 5
		foreach ($this->store['tracking'] as $fid => $forum)
292
		{
293
			$tracking_info[$fid] = call_user_func_array($function, array($fid, $forum['topic_list'], &$this->store['topic'], array($fid => $forum['mark_time'])));
294
		}
295
296
		return $tracking_info;
297
	}
298 5
299
	/**
300 5
	 * @return bool
301 5
	 */
302
	private function _can_track_by_lastread()
303 5
	{
304 5
		return ($this->config['load_db_lastread'] && $this->user->data['is_registered']) ? true : false;
305
	}
306 5
307
	/**
308
	 * @return bool
309
	 */
310
	private function _can_track_anonymous()
311
	{
312 5
		return ($this->config['load_anon_lastread'] || $this->user->data['is_registered']) ? true : false;
313
	}
314 5
315
	/**
316
	 * @param int $forum_id
317
	 * @return bool
318
	 */
319
	private function _attachments_allowed($forum_id)
320 1
	{
321
		return ($this->store['attachments'] && $this->_user_can_download_attachments($forum_id)) ? true : false;
322 1
	}
323
324
	/**
325
	 * @param int $forum_id
326
	 * @return bool
327
	 */
328
	private function _user_can_download_attachments($forum_id)
329 7
	{
330
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
331 7
	}
332
333
	/**
334
	 * @param array $allowed_extensions
335
	 * @param bool $exclude_in_message
336
	 * @param string $order_by
337
	 * @return string
338 6
	 */
339
	private function _get_attachment_sql($allowed_extensions, $exclude_in_message, $order_by)
340 6
	{
341
		return 'SELECT *
342
			FROM ' . ATTACHMENTS_TABLE . '
343
			WHERE ' . $this->db->sql_in_set('post_msg_id', $this->store['attachments']) .
344
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
345
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
346
			ORDER BY ' . $order_by;
347
	}
348
}
349