settings_controller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 21
rs 9.9666
c 0
b 0
f 0

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
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2024 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller\admin;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\log\log;
16
use phpbb\request\request;
17
use phpbb\template\template;
18
use phpbb\user;
19
use skouat\ppde\helpers\auth_helper;
20
use skouat\ppde\actions\currency;
21
use skouat\ppde\actions\locale_icu;
22
23
/**
24
 * @property config   config             Config object
25
 * @property string   id_prefix_name     Prefix name for identifier in the URL
26
 * @property string   lang_key_prefix    Prefix for the messages thrown by exceptions
27
 * @property language language           Language user object
28
 * @property log      log                The phpBB log system
29
 * @property string   module_name        Name of the module currently used
30
 * @property request  request            Request object
31
 * @property bool     submit             State of submit $_POST variable
32
 * @property template template           Template object
33
 * @property string   u_action           Action URL
34
 * @property user     user               User object
35
 */
36
class settings_controller extends admin_main
37
{
38
	protected $ppde_auth;
39
	protected $ppde_actions_currency;
40
	protected $ppde_actions_locale;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param config      $config                Config object
46
	 * @param language    $language              Language user object
47
	 * @param log         $log                   The phpBB log system
48
	 * @param currency    $ppde_actions_currency PPDE currency actions object
49
	 * @param locale_icu  $ppde_actions_locale   PPDE locale actions object
50
	 * @param auth_helper $ppde_auth             PPDE auth actions object
51
	 * @param request     $request               Request object
52
	 * @param template    $template              Template object
53
	 * @param user        $user                  User object
54
	 */
55
	public function __construct(
56
		config $config,
57
		language $language,
58
		log $log,
59
		auth_helper $ppde_auth,
60
		currency $ppde_actions_currency,
61
		locale_icu $ppde_actions_locale,
62
		request $request,
63
		template $template,
64
		user $user
65
	)
66
	{
67
		$this->config = $config;
68
		$this->language = $language;
69
		$this->log = $log;
70
		$this->ppde_auth = $ppde_auth;
71
		$this->ppde_actions_currency = $ppde_actions_currency;
72
		$this->ppde_actions_locale = $ppde_actions_locale;
73
		$this->request = $request;
74
		$this->template = $template;
75
		$this->user = $user;
76
	}
77
78
	/**
79
	 * Display the general settings a user can configure for this extension
80
	 */
81
	public function display_settings(): void
82
	{
83
		// Define the name of the form for use as a form key
84
		add_form_key('ppde_settings');
85
86
		// Create an array to collect errors that will be output to the user
87
		$errors = [];
88
89
		$this->submit_settings();
90
91
		// Set output vars for display in the template
92
		$this->s_error_assign_template_vars($errors);
93
		$this->u_action_assign_template_vars();
94
		$this->ppde_actions_currency->build_currency_select_menu((int) $this->config['ppde_default_currency']);
95
		$this->ppde_actions_locale->build_locale_select_menu($this->config['ppde_default_locale']);
96
		$this->build_remote_uri_select_menu((int) $this->config['ppde_default_remote'], 'live');
97
		$this->build_stat_position_select_menu($this->config['ppde_stats_position']);
98
99
		$this->template->assign_vars([
100
			// Global Settings vars
101
			'PPDE_ACCOUNT_ID'           => $this->check_config($this->config['ppde_account_id'], 'string'),
102
			'PPDE_DEFAULT_VALUE'        => $this->check_config($this->config['ppde_default_value'], 'integer', 0),
103
			'PPDE_DROPBOX_VALUE'        => $this->check_config($this->config['ppde_dropbox_value'], 'string', '1,2,3,4,5,10,20,25,50,100'),
104
			'S_PPDE_DEFAULT_LOCALE'     => $this->ppde_actions_locale->icu_requirements(),
105
			'S_PPDE_DROPBOX_ENABLE'     => $this->check_config($this->config['ppde_dropbox_enable']),
106
			'S_PPDE_ENABLE'             => $this->check_config($this->config['ppde_enable']),
107
			'S_PPDE_HEADER_LINK'        => $this->check_config($this->config['ppde_header_link']),
108
			'S_PPDE_ALLOW_GUEST'        => $this->check_config($this->config['ppde_allow_guest'], 'boolean', false),
109
110
			// Statistics Settings vars
111
			'PPDE_GOAL'                 => $this->check_config($this->config['ppde_goal'], 'float', 0),
112
			'PPDE_RAISED'               => $this->check_config($this->config['ppde_raised'], 'float', 0),
113
			'PPDE_USED'                 => $this->check_config($this->config['ppde_used'], 'float', 0),
114
			'S_PPDE_GOAL_ENABLE'        => $this->check_config($this->config['ppde_goal_enable']),
115
			'S_PPDE_RAISED_ENABLE'      => $this->check_config($this->config['ppde_raised_enable']),
116
			'S_PPDE_STATS_INDEX_ENABLE' => $this->check_config($this->config['ppde_stats_index_enable']),
117
			'S_PPDE_STATS_TEXT_ONLY'    => $this->check_config($this->config['ppde_stats_text_only']),
118
			'S_PPDE_USED_ENABLE'        => $this->check_config($this->config['ppde_used_enable']),
119
		]);
120
	}
121
122
	/**
123
	 * {@inheritdoc}
124
	 */
125
	protected function set_settings(): void
126
	{
127
		// Set options for Global settings
128
		$this->config->set('ppde_allow_guest', $this->request->variable('ppde_allow_guest', false));
129
		$this->config->set('ppde_default_currency', $this->request->variable('ppde_default_currency', 0));
130
		$this->config->set('ppde_default_locale', $this->request->variable('ppde_default_locale', $this->ppde_actions_locale->locale_get_default()));
131
		$this->config->set('ppde_default_value', $this->request->variable('ppde_default_value', 0));
132
		$this->config->set('ppde_dropbox_enable', $this->request->variable('ppde_dropbox_enable', false));
133
		$this->config->set('ppde_dropbox_value', $this->rebuild_items_list($this->request->variable('ppde_dropbox_value', '1,2,3,4,5,10,20,25,50,100'), (int) $this->config['ppde_default_value']));
134
		$this->config->set('ppde_enable', $this->request->variable('ppde_enable', false));
135
		$this->config->set('ppde_header_link', $this->request->variable('ppde_header_link', false));
136
137
		// Set options for Advanced settings
138
		$this->config->set('ppde_default_remote', $this->request->variable('ppde_default_remote', 0));
139
140
		// Set options for Statistics Settings
141
		$this->config->set('ppde_stats_index_enable', $this->request->variable('ppde_stats_index_enable', false));
142
		$this->config->set('ppde_stats_position', $this->request->variable('ppde_stats_position', 'bottom'));
143
		$this->config->set('ppde_stats_text_only', $this->request->variable('ppde_stats_text_only', false));
144
		$this->config->set('ppde_raised_enable', $this->request->variable('ppde_raised_enable', false));
145
		$this->config->set('ppde_raised', $this->request->variable('ppde_raised', 0.0));
146
		$this->config->set('ppde_goal_enable', $this->request->variable('ppde_goal_enable', false));
147
		$this->config->set('ppde_goal', $this->request->variable('ppde_goal', 0.0));
148
		$this->config->set('ppde_used_enable', $this->request->variable('ppde_used_enable', false));
149
		$this->config->set('ppde_used', $this->request->variable('ppde_used', 0.0));
150
151
		// Settings with dependencies are the last to be set.
152
		$this->config->set('ppde_account_id', $this->required_settings($this->request->variable('ppde_account_id', ''), (bool) $this->config['ppde_enable']));
153
		$this->ppde_auth->set_guest_acl();
154
	}
155
156
	/**
157
	 * Rebuilds the items list to conserve only numeric values.
158
	 *
159
	 * @param string $config_value The config value representing the initial items list.
160
	 * @param int    $added_value  The optional value to be added to the items list. Default is 0.
161
	 *
162
	 * @return string The rebuilt items list.
163
	 */
164
	private function rebuild_items_list(string $config_value, int $added_value = 0): string
165
	{
166
		$items_list = explode(',', $config_value);
167
168
		// Map each item in the list to an integer value
169
		$merged_items = array_map('intval', $items_list);
170
171
		// If $added_value is not zero, add it to $merged_items
172
		if ($added_value !== 0)
173
		{
174
			$merged_items[] = $added_value;
175
		}
176
		// Remove '0' values from $merged_items
177
		$merged_items = array_filter($merged_items);
178
179
		// Remove any duplicate items
180
		$merged_items = array_unique($merged_items);
181
182
		// Sort the items
183
		sort($merged_items, SORT_NUMERIC);
184
185
		// Convert array back to string and return
186
		return implode(',', $merged_items);
187
	}
188
189
	/**
190
	 * Build pull down menu options of available positions
191
	 *
192
	 * @param string $default Value of the selected item.
193
	 */
194
	public function build_stat_position_select_menu($default): void
195
	{
196
		// List of positions allowed
197
		$positions = ['top', 'bottom', 'both'];
198
199
		// Process each menu item for pull-down
200
		foreach ($positions as $position)
201
		{
202
			// Set output block vars for display in the template
203
			$this->template->assign_block_vars('positions_options', [
204
				'POSITION_NAME' => $position,
205
				'S_DEFAULT'     => (string) $default === $position,
206
			]);
207
		}
208
		unset ($positions);
209
	}
210
}
211