Completed
Push — master ( 4a8b55...d86878 )
by Daniel
11:13
created

attachments::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4286
cc 1
eloc 9
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\blocks;
11
12
/**
13
 * Attachments Block
14
 */
15
class attachments extends \blitze\sitemaker\services\blocks\driver\block
16
{
17
	/** @var \phpbb\auth\auth */
18
	protected $auth;
19
20
	/** @var \phpbb\cache\service */
21
	protected $cache;
22
23
	/** @var \phpbb\user */
24
	protected $user;
25
26
	/** @var \blitze\sitemaker\services\date_range */
27
	protected $date_range;
28
29
	/** @var \blitze\sitemaker\services\forum\data */
30
	protected $forum_data;
31
32
	/** @var \blitze\sitemaker\services\forum\options */
33
	protected $forum_options;
34
35
	/** @var string */
36
	protected $phpbb_root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var array */
42
	private $settings = array();
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\auth\auth							$auth				Permission object
48
	 * @param \phpbb\cache\service						$cache				Cache Service object
49
	 * @param \phpbb\user								$user				User object
50
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
51
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
52
	 * @param \blitze\sitemaker\services\forum\options	$forum_data			Forum Data object
53
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
54
	 * @param string									$php_ext			php file extension
55
	 */
56 6
	public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\user $user, \blitze\sitemaker\services\date_range $date_range, \blitze\sitemaker\services\forum\data $forum_data, \blitze\sitemaker\services\forum\options $forum_options, $phpbb_root_path, $php_ext)
57
	{
58 6
		$this->auth = $auth;
59 6
		$this->cache = $cache;
60 6
		$this->user = $user;
61 6
		$this->date_range = $date_range;
62 6
		$this->forum_data = $forum_data;
63 6
		$this->forum_options = $forum_options;
64 6
		$this->phpbb_root_path = $phpbb_root_path;
65 6
		$this->php_ext = $php_ext;
66 6
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71 1
	public function get_config(array $settings)
72
	{
73 1
		$forum_options = $this->forum_options->get_all();
74 1
		$topic_type_options = $this->_get_topic_type_options();
75 1
		$range_options = $this->_get_range_options();
76 1
		$attach_type_options = array('' => 'ALL', 'IMAGES' => 'IMAGES', 'ARCHIVES' => 'ARCHIVES');
77
78
		return array(
79 1
			'legend1'		=> $this->user->lang('SETTINGS'),
80 1
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
81 1
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
82 1
			'first_only'		=> array('lang' => 'FIRST_POST_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => 1),
83 1
			'post_ids'			=> array('lang' => 'ATTACHMENTS_FROM_POSTS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
84 1
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $range_options, 'default' => '', 'explain' => false),
85 1
			'limit'				=> array('lang' => 'LIMIT', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
86 1
			'ext_type'			=> array('lang' => 'EXTENSION_GROUP', 'validate' => 'string', 'type' => 'radio', 'options' => $attach_type_options, 'default' => '', 'explain' => false),
87 1
		);
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93 5
	public function display(array $bdata, $edit_mode = false)
94
	{
95 5
		$this->settings = $bdata['settings'];
96
97 5
		$extensions = $this->cache->obtain_attach_extensions(0);
98 5
		$ext_groups = $this->_get_extension_groups($extensions);
99
100 5
		$posts_data = $this->_get_posts_data();
101 5
		$attachments = $this->forum_data->get_attachments(0, $ext_groups[$this->settings['ext_type']], false, 'download_count DESC');
102
103 5
		$content = '';
104 5
		if (sizeof($attachments))
105 5
		{
106 4
			$this->_get_block_content($attachments, $posts_data, $extensions);
107
108 4
			$content = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/attachments.html', 'attachments');
109 4
		}
110
111
		return array(
112 5
			'title'		=> 'ATTACHMENTS',
113 5
			'content'	=> $content,
114 5
		);
115
	}
116
117
	/**
118
	 * @param array $attachments_ary
119
	 * @param array $posts_data
120
	 * @param array $extensions
121
	 */
122 4
	protected function _get_block_content(array $attachments_ary, array $posts_data, array $extensions)
123
	{
124 4
		$message = '';
125 4
		$update_count = array();
126
127 4
		foreach ($attachments_ary as $post_id => $attachments)
128
		{
129 4
			$topic_id = $attachments[0]['topic_id'];
130 4
			$post_row = $posts_data[$topic_id][$post_id];
131
132 4
			parse_attachments($post_row['forum_id'], $message, $attachments, $update_count, true);
133
134 4
			$this->ptemplate->assign_block_vars('postrow', array());
135 4
			foreach ($attachments as $i => $attachment)
136
			{
137 4
				$row = $attachments_ary[$post_id][$i];
138 4
				$topic_id = $row['topic_id'];
139 4
				$post_id = $row['post_msg_id'];
140
141 4
				$this->ptemplate->assign_block_vars('postrow.attachment', array(
142 4
					'DISPLAY_ATTACHMENT'	=> $attachment,
143 4
					'EXTENSION_GROUP'		=> $extensions[$row['extension']]['group_name'],
144 4
					'U_VIEWTOPIC'			=> append_sid("{$this->phpbb_root_path}viewtopic.{$this->php_ext}", "t=$topic_id&amp;p=$post_id") . '#p' . $post_id,
145 4
				));
146 4
			}
147 4
		}
148 4
	}
149
150
	/**
151
	 * @return array
152
	 */
153 5
	private function _get_posts_data()
154
	{
155 5
		$range_info = $this->date_range->get($this->settings['date_range']);
156 5
		$allowed_forums = $this->_get_allowed_forums();
157 5
		$post_ids = array_filter(explode(',', $this->settings['post_ids']));
158
159 5
		$sql_array = $this->forum_data->query()
160 5
			->fetch_forum($allowed_forums)
161 5
			->fetch_topic_type($this->settings['topic_type'])
162 5
			->fetch_date_range($range_info['start'], $range_info['stop'])
163 5
			->build()
164 5
			->get_sql_array();
165
166 5
		$sql_array['SELECT'] = '';
167 5
		$sql_array['WHERE'] .= ' AND p.post_attachment <> 0';
168
169 5
		if ($this->settings['first_only'])
170 5
		{
171 2
			$sql_array['WHERE'] .= " AND p.post_id = t.topic_first_post_id";
172 2
		}
173
174 5
		return $this->forum_data->get_post_data(false, $post_ids, $this->settings['limit'], 0, $sql_array);
175
	}
176
177
	/**
178
	 * @param array $extensions
179
	 * @return array
180
	 */
181 5
	protected function _get_extension_groups(array $extensions)
182
	{
183 5
		array_shift($extensions);
184
185 5
		$ext_groups = array('' => array());
186 5
		foreach ($extensions as $ext => $row)
187
		{
188 5
			$ext_groups[$row['group_name']][] = $ext;
189 5
		}
190
191 5
		return $ext_groups;
192
	}
193
194
	/**
195
	 * @return array
196
	 */
197 5
	private function _get_allowed_forums()
198
	{
199 5
		$allowed_forums = array_unique(array_keys($this->auth->acl_getf('f_download', true)));
200 5
		if (sizeof($this->settings['forum_ids']))
201 5
		{
202
			$allowed_forums = array_intersect($this->settings['forum_ids'], $allowed_forums);
203
		}
204
205 5
		return $allowed_forums;
206
	}
207
208
	/**
209
	 * @return array
210
	 */
211 1
	private function _get_topic_type_options()
212
	{
213
		return array(
214 1
			POST_NORMAL     => 'POST_NORMAL',
215 1
			POST_STICKY     => 'POST_STICKY',
216 1
			POST_ANNOUNCE   => 'POST_ANNOUNCEMENT',
217 1
			POST_GLOBAL     => 'POST_GLOBAL',
218 1
		);
219
	}
220
221
	/**
222
	 * @return array
223
	 */
224 1
	private function _get_range_options()
225
	{
226
		return array(
227 1
			''      => 'ALL_TIME',
228 1
			'today' => 'TODAY',
229 1
			'week'  => 'THIS_WEEK',
230 1
			'month' => 'THIS_MONTH',
231 1
			'year'  => 'THIS_YEAR',
232 1
		);
233
	}
234
}
235