Completed
Push — master ( ec5717...657e76 )
by Matt
07:27
created

acp_controller::__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
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
c 1
b 0
f 1
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
* Topic Preview
5
*
6
* @copyright (c) 2013 Matt Friedman
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace vse\topicpreview\controller;
12
13
use phpbb\cache\driver\driver_interface as cache_driver;
14
use phpbb\config\config;
15
use phpbb\db\driver\driver_interface as db_driver;
16
use phpbb\extension\manager;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
21
/**
22
 * Class acp_controller
23
 *
24
 * @package vse\topicpreview\controller
25
 */
26
class acp_controller implements acp_controller_interface
27
{
28
	const NO_THEME = 'no';
29
	const DEFAULT_THEME = 'light';
30
31
	/** @var cache_driver */
32
	protected $cache;
33
34
	/** @var config */
35
	protected $config;
36
37
	/** @var db_driver */
38
	protected $db;
39
40
	/** @var manager */
41
	protected $ext_manager;
42
43
	/** @var request */
44
	protected $request;
45
46
	/** @var template */
47
	protected $template;
48
49
	/** @var user */
50
	protected $user;
51
52
	/** @var string */
53
	protected $phpbb_root_path;
54
55
	/** @var string */
56
	protected $u_action;
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param cache_driver $cache
62
	 * @param config       $config
63
	 * @param db_driver    $db
64
	 * @param manager      $phpbb_extension_manager
65
	 * @param request      $request
66
	 * @param template     $template
67
	 * @param user         $user
68
	 * @param              $phpbb_root_path
69
	 */
70 3
	public function __construct(cache_driver $cache, config $config, db_driver $db, manager $phpbb_extension_manager, request $request, template $template, user $user, $phpbb_root_path)
71
	{
72 3
		$this->cache = $cache;
73 3
		$this->config = $config;
74 3
		$this->db = $db;
75 3
		$this->ext_manager = $phpbb_extension_manager;
76 3
		$this->request = $request;
77 3
		$this->template = $template;
78 3
		$this->user = $user;
79 3
		$this->phpbb_root_path = $phpbb_root_path;
80 3
	}
81
82
	/**
83
	 * @inheritdoc
84
	 */
85 3
	public function handle()
86
	{
87 3
		$this->user->add_lang_ext('vse/topicpreview', 'topic_preview_acp');
88
89 3
		$form_key = 'acp_topic_preview';
90 3
		add_form_key($form_key);
91
92 3
		if ($this->request->is_set_post('submit'))
93 3
		{
94 2
			if (!check_form_key($form_key))
95 2
			{
96 1
				trigger_error($this->user->lang('FORM_INVALID') . adm_back_link($this->u_action), E_USER_WARNING);
97
			}
98
99 1
			$this->submit_settings();
100
101 1
			trigger_error($this->user->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
102
		}
103
104 1
		$this->display_settings();
105 1
	}
106
107
	/**
108
	 * @inheritdoc
109
	 */
110 2
	public function set_u_action($u_action)
111
	{
112 2
		$this->u_action = $u_action;
113 2
		return $this;
114
	}
115
116
	/**
117
	 * Display the settings with the current config values
118
	 */
119 1
	protected function display_settings()
120
	{
121 1
		$this->template->assign_vars(array(
122 1
			'TOPIC_PREVIEW_LIMIT'		=> $this->config['topic_preview_limit'],
123 1
			'TOPIC_PREVIEW_WIDTH'		=> $this->config['topic_preview_width'],
124 1
			'TOPIC_PREVIEW_DELAY'		=> $this->config['topic_preview_delay'],
125 1
			'TOPIC_PREVIEW_DRIFT'		=> $this->config['topic_preview_drift'],
126 1
			'S_TOPIC_PREVIEW_AVATARS'	=> $this->config['topic_preview_avatars'],
127 1
			'S_TOPIC_PREVIEW_LAST_POST'	=> $this->config['topic_preview_last_post'],
128 1
			'TOPIC_PREVIEW_STRIP'		=> $this->config['topic_preview_strip_bbcodes'],
129 1
			'TOPIC_PREVIEW_STYLES'		=> $this->get_styles(),
130 1
			'TOPIC_PREVIEW_THEMES'		=> $this->get_themes(),
131 1
			'TOPIC_PREVIEW_DEFAULT'		=> self::DEFAULT_THEME,
132 1
			'TOPIC_PREVIEW_NO_THEME'	=> self::NO_THEME,
133 1
			'U_ACTION'					=> $this->u_action,
134 1
		));
135 1
	}
136
137
	/**
138
	 * Submit the settings from the form to the database
139
	 */
140 1
	protected function submit_settings()
141
	{
142 1
		$this->config->set('topic_preview_limit', abs($this->request->variable('topic_preview_limit', 0))); // abs() no negative values
143 1
		$this->config->set('topic_preview_width', abs($this->request->variable('topic_preview_width', 0))); // abs() no negative values
144 1
		$this->config->set('topic_preview_delay', abs($this->request->variable('topic_preview_delay', 0))); // abs() no negative values
145 1
		$this->config->set('topic_preview_drift', $this->request->variable('topic_preview_drift', 0));
146 1
		$this->config->set('topic_preview_avatars', $this->request->variable('topic_preview_avatars', 0));
147 1
		$this->config->set('topic_preview_last_post', $this->request->variable('topic_preview_last_post', 0));
148 1
		$this->config->set('topic_preview_strip_bbcodes', $this->request->variable('topic_preview_strip_bbcodes', ''));
149
150 1
		$styles = $this->get_styles();
151 1
		foreach ($styles as $row)
152
		{
153 1
			$this->set_style_theme($row['style_id'], $this->request->variable('style_' . $row['style_id'], ''));
154 1
		}
155 1
	}
156
157
	/**
158
	 * Update topic_preview_theme setting in the styles table
159
	 *
160
	 * @param int    $style_id Identifier of the board style
161
	 * @param string $theme    Name of the selected theme
162
	 */
163 1
	protected function set_style_theme($style_id, $theme)
164
	{
165 1
		$sql = 'UPDATE ' . STYLES_TABLE . "
166 1
			SET topic_preview_theme = '" . $this->db->sql_escape($theme) . "'
167 1
			WHERE style_id = " . (int) $style_id;
168
169 1
		$this->db->sql_query($sql);
170
171 1
		$this->cache->destroy('sql', STYLES_TABLE);
172 1
	}
173
174
	/**
175
	 * Get style data from the styles table
176
	 *
177
	 * @return array Style data
178
	 */
179 2
	protected function get_styles()
180
	{
181
		$sql = 'SELECT style_id, style_name, topic_preview_theme
182 2
			FROM ' . STYLES_TABLE . '
183 2
			WHERE style_active = 1';
184 2
		$result = $this->db->sql_query($sql);
185
186 2
		$rows = $this->db->sql_fetchrowset($result);
187 2
		$this->db->sql_freeresult($result);
188
189 2
		return $rows;
190
	}
191
192
	/**
193
	 * Get file names from Topic Preview's CSS files
194
	 *
195
	 * @return array File name data
196
	 */
197 1
	protected function get_themes()
198
	{
199 1
		$finder = $this->ext_manager->get_finder();
200
201
		// Find css files in ext/vse/topicpreview/styles/all/theme/
202
		$themes = $finder
203 1
			->extension_suffix('.css')
204 1
			->extension_directory('/styles/all/theme')
205 1
			->find_from_extension('topicpreview', $this->phpbb_root_path . 'ext/vse/topicpreview/');
206
207
		// Get just basenames of array keys
208 1
		$themes = array_map(function ($value) {
209 1
			return basename($value, '.css');
210 1
		}, array_keys($themes));
211
212
		// Add option for native browser tooltip (aka no theme)
213 1
		$themes[] = self::NO_THEME;
214
215 1
		return $themes;
216
	}
217
}
218