recent_topics::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 14
dl 0
loc 23
rs 10

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
 *
5
 * @package sitemaker
6
 * @copyright (c) 2021 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\blocks;
12
13
/**
14
 * Recent Topics Block
15
 */
16
class recent_topics extends forum_topics
17
{
18
	/** @var \phpbb\cache\service */
19
	protected $cache;
20
21
	/** @var \phpbb\request\request_interface */
22
	protected $request;
23
24
	/** @var \phpbb\pagination */
25
	protected $pagination;
26
27
	/** @var \phpbb\template\template */
28
	protected $template;
29
30
	/** @var int */
31
	protected $start = 0;
32
33
	/** @var int */
34
	protected $total_topics = 0;
35
36
	/** @var string */
37
	protected $param;
38
39
	/** @var string */
40
	protected $block;
41
42
	/** @var array */
43
	protected $icons;
44
45
	/** @var array */
46
	protected $topic_type_class = [
47
		POST_GLOBAL		=> ' global-announce',
48
		POST_ANNOUNCE	=> ' announce',
49
		POST_STICKY		=> ' sticky',
50
		POST_NORMAL		=> '',
51
	];
52
53
	/**
54
	 * Constructor
55
	 *
56
	 * @param \phpbb\auth\auth							$auth				Permission object
57
	 * @param \phpbb\content_visibility					$content_visibility	Content visibility object
58
	 * @param \phpbb\language\language					$translator			Language object
59
	 * @param \phpbb\user								$user				User object
60
	 * @param \Urodoz\Truncate\TruncateService			$truncator			Truncator service
61
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
62
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
63
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
64
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
65
	 * @param string									$php_ext			php file extension
66
	 * @param \phpbb\cache\service						$cache				Cache Service object
67
	 * @param \phpbb\request\request_interface			$request			Request object
68
	 * @param \phpbb\pagination							$pagination			Pagination object
69
	 * @param \phpbb\template\template					$template			Template object
70
	 */
71
	public function __construct(
72
		\phpbb\auth\auth $auth,
73
		\phpbb\content_visibility $content_visibility,
74
		\phpbb\language\language $translator,
75
		\phpbb\user $user,
76
		\Urodoz\Truncate\TruncateService $truncator,
77
		\blitze\sitemaker\services\date_range $date_range,
78
		\blitze\sitemaker\services\forum\data $forum_data,
79
		\blitze\sitemaker\services\forum\options $forum_options,
80
		$phpbb_root_path,
81
		$php_ext,
82
		\phpbb\cache\service $cache,
83
		\phpbb\request\request_interface $request,
84
		\phpbb\pagination $pagination,
85
		\phpbb\template\template $template
86
	)
87
	{
88
		parent::__construct($auth, $content_visibility, $translator, $user, $truncator, $date_range, $forum_data, $forum_options, $phpbb_root_path, $php_ext);
89
90
		$this->cache = $cache;
91
		$this->request = $request;
92
		$this->pagination = $pagination;
93
		$this->template = $template;
94
	}
95
96
	/**
97
	 * {@inheritdoc}
98
	 */
99
	public function get_config(array $settings)
100
	{
101
		$orig_config = parent::get_config($settings);
102
		$config = $orig_config;
103
		$config['max_topics']['default'] = 5;
104
		$config['max_topics']['lang'] = 'TOPICS_PER_PAGE';
105
		unset($config['template'], $config['context']);
106
107
		$config['date_range']	= array('lang' => 'TOPICS_LOOK_BACK', 'validate' => 'int:0:255', 'type' => 'number:1:255', 'maxlength' => 3, 'default' => 60, 'append' => 'DAYS');
108
		$config['last_post']	= array('lang' => 'SHOW_LAST_POST', 'validate' => 'bool', 'type' => 'radio:yes_no', 'default' => true);
109
110
		$keys = array_keys($config);
111
		$keys[array_search('max_topics', $keys)] = 'per_page';
112
		$keys[array_search('date_range', $keys)] = 'look_back';
113
114
		return array_combine($keys, $config);
115
	}
116
117
	/**
118
	 * {@inheritdoc}
119
	 */
120
	public function display(array $bdata, $edit_mode = false)
121
	{
122
		$this->icons = $this->cache->obtain_icons();
123
		$this->param = 'pg' . $bdata['bid'];
124
		$this->block = 'bk-' . $bdata['bid'];
125
126
		$bdata['settings']['context'] = 'last';
127
		$bdata['settings']['template'] = '';
128
		$bdata['settings']['date_range'] = '';
129
130
		if (!$bdata['settings']['last_post'])
131
		{
132
			$bdata['settings']['preview_chars'] = 0;
133
		}
134
135
		return parent::display($bdata, $edit_mode);
136
	}
137
138
	/**
139
	 * {@inheritdoc}
140
	 */
141
	protected function get_block_content(array $topic_data)
142
	{
143
		$base_url = trim(build_url(array($this->param)), '?');
144
145
		return array_merge(
146
			parent::get_block_content($topic_data),
147
			array(
148
				'T_ICONS_PATH'	=> $this->template->retrieve_var('T_ICONS_PATH'),
149
				'S_LAST_POST'	=> $this->settings['last_post'],
150
				'TOTAL_PAGES'	=> ceil($this->total_topics / $this->settings['per_page']),
151
				'CURRENT_PAGE'	=> $this->start + 1,
152
				'BLOCK_ID'		=> $this->block,
153
				'BASE_URL'		=> $base_url . '#' . $this->block,
154
				'PAGE_URL'		=> $base_url . (strpos($base_url, '?') === false ? '?' : '&amp;') . $this->param . '=%s#' . $this->block,
155
			)
156
		);
157
	}
158
159
	/**
160
	 * {@inheritdoc}
161
	 */
162
	protected function build_query()
163
	{
164
		parent::build_query();
165
		$this->forum_data->fetch_custom(array(
166
			'WHERE' => array($this->sort_order[$this->settings['order_by']] . ' > ' . $this->get_time_limit()),
167
		));
168
		$this->forum_data->fetch_db_track();
169
	}
170
171
	/**
172
	 * @return int
173
	 */
174
	protected function get_time_limit()
175
	{
176
		return time() - ($this->settings['look_back'] * 24 * 3600);
177
	}
178
179
	/**
180
	 * {@inheritdoc}
181
	 */
182
	protected function get_topic_data()
183
	{
184
		$this->set_start_page();
185
186
		return $this->forum_data->get_topic_data($this->settings['per_page'], $this->start * $this->settings['per_page']);
187
	}
188
189
	/**
190
	 * @return void
191
	 */
192
	protected function set_start_page()
193
	{
194
		$page = $this->request->variable($this->param, 1);
195
196
		$this->total_topics = $this->forum_data->get_topics_count();
197
		$this->start = $this->pagination->validate_start($page - 1, $this->settings['per_page'], $this->total_topics);
198
	}
199
200
	/**
201
	 * {@inheritdoc}
202
	 */
203
	protected function get_topics_template_data(array &$topic_data, array &$post_data, array $user_data)
204
	{
205
		$template_data = parent::get_topics_template_data($topic_data, $post_data, $user_data);
206
207
		$replies = $template_data['REPLIES'];
208
		$unread_topic = $template_data['S_UNREAD_TOPIC'];
209
210
		// Get folder img, topic status/type related information
211
		$folder_img = $folder_alt = $topic_type = '';
212
		topic_status($topic_data, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
213
214
		$template_data['S_HAS_POLL']			= (bool) $topic_data['poll_start'];
215
		$template_data['S_TOPIC_ICONS']			= (bool) $topic_data['enable_icons'];
216
		$template_data['TOPIC_IMG_STYLE']		= $folder_img;
217
		$template_data['TOPIC_FOLDER_IMG']		= $this->user->img($folder_img, $folder_alt);
218
		$template_data['TOPIC_FOLDER_IMG_ALT']	= $this->user->lang[$folder_alt];
219
		$template_data['TOPIC_TYPE_CLASS']		= $this->topic_type_class[$topic_data['topic_type']];
220
		$template_data['TOPIC_TIME_RFC3339']	= gmdate(DATE_RFC3339, $topic_data['topic_time']);
221
		$template_data['LAST_POST_TIME_RFC3339'] = gmdate(DATE_RFC3339, $topic_data['topic_last_post_time']);
222
		$template_data['LAST_POST_TIME']		= $this->user->format_date($topic_data['topic_last_post_time']);
223
224
		if (!empty($this->icons[$topic_data['icon_id']]))
225
		{
226
			$template_data['TOPIC_ICON_IMG']		= $this->icons[$topic_data['icon_id']]['img'];
227
			$template_data['TOPIC_ICON_IMG_WIDTH']	= $this->icons[$topic_data['icon_id']]['width'];
228
			$template_data['TOPIC_ICON_IMG_HEIGHT']	= $this->icons[$topic_data['icon_id']]['height'];
229
		}
230
231
		return $template_data;
232
	}
233
234
	/**
235
	 * {@inheritdoc}
236
	 */
237
	public function get_template()
238
	{
239
		return '@blitze_sitemaker/blocks/recent_topics.html';
240
	}
241
}
242