Passed
Push — develop-3.3.x ( 0d6b30...b6d44e )
by Mario
02:37
created

overview_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 16
dl 0
loc 40
rs 9.584
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-2020 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\auth\auth;
14
use phpbb\config\config;
15
use phpbb\language\language;
16
use phpbb\log\log;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
use skouat\ppde\actions\core;
21
use skouat\ppde\actions\locale_icu;
22
use skouat\ppde\controller\extension_manager;
23
use skouat\ppde\controller\ipn_paypal;
24
use skouat\ppde\controller\main_controller;
25
26
/**
27
 * @property config     config              Config object
28
 * @property string     id_prefix_name      Prefix name for identifier in the URL
29
 * @property string     lang_key_prefix     Prefix for the messages thrown by exceptions
30
 * @property language   language            Language user object
31
 * @property log        log                 The phpBB log system
32
 * @property string     module_name         Name of the module currently used
33
 * @property locale_icu ppde_actions_locale PPDE Locale actions object
34
 * @property ipn_paypal ppde_ipn_paypal     IPN PayPal object
35
 * @property request    request             Request object
36
 * @property template   template            Template object
37
 * @property string     u_action            Action URL
38
 * @property user       user                User object
39
 */
40
class overview_controller extends admin_main
41
{
42
	protected $adm_relative_path;
43
	protected $auth;
44
	protected $ppde_actions;
45
	protected $ppde_controller_main;
46
	protected $ppde_controller_transactions;
47
	protected $ppde_ext_manager;
48
	protected $php_ext;
49
	protected $phpbb_admin_path;
50
	protected $phpbb_root_path;
51
52
	/**
53
	 * Constructor
54
	 *
55
	 * @param auth                    $auth                         Authentication object
56
	 * @param config                  $config                       Config object
57
	 * @param language                $language                     Language user object
58
	 * @param log                     $log                          The phpBB log system
59
	 * @param core                    $ppde_actions                 PPDE core actions object
60
	 * @param locale_icu              $ppde_actions_locale          PPDE Locale actions object
61
	 * @param main_controller         $ppde_controller_main         Main controller object
62
	 * @param transactions_controller $ppde_controller_transactions Admin transactions controller object
63
	 * @param extension_manager       $ppde_ext_manager             Extension manager object
64
	 * @param ipn_paypal              $ppde_ipn_paypal              IPN PayPal object
65
	 * @param request                 $request                      Request object
66
	 * @param template                $template                     Template object
67
	 * @param user                    $user                         User object
68
	 * @param string                  $adm_relative_path            phpBB admin relative path
69
	 * @param string                  $phpbb_root_path              phpBB root path
70
	 * @param string                  $php_ext                      phpEx
71
	 *
72
	 * @access public
73
	 */
74
	public function __construct(
75
		auth $auth,
76
		config $config,
77
		language $language,
78
		log $log,
79
		core $ppde_actions,
80
		locale_icu $ppde_actions_locale,
81
		main_controller $ppde_controller_main,
82
		transactions_controller $ppde_controller_transactions,
83
		extension_manager $ppde_ext_manager,
84
		ipn_paypal $ppde_ipn_paypal,
85
		request $request,
86
		template $template,
87
		user $user,
88
		$adm_relative_path,
89
		$phpbb_root_path,
90
		$php_ext
91
	)
92
	{
93
		$this->auth = $auth;
94
		$this->config = $config;
95
		$this->language = $language;
96
		$this->log = $log;
97
		$this->ppde_actions = $ppde_actions;
98
		$this->ppde_actions_locale = $ppde_actions_locale;
99
		$this->ppde_controller_main = $ppde_controller_main;
100
		$this->ppde_controller_transactions = $ppde_controller_transactions;
101
		$this->ppde_ext_manager = $ppde_ext_manager;
102
		$this->ppde_ipn_paypal = $ppde_ipn_paypal;
103
		$this->request = $request;
104
		$this->template = $template;
105
		$this->user = $user;
106
		$this->php_ext = $php_ext;
107
		$this->adm_relative_path = $adm_relative_path;
108
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
109
		$this->phpbb_root_path = $phpbb_root_path;
110
		parent::__construct(
111
			'overview',
112
			'PPDE_',
113
			''
114
		);
115
	}
116
117
	/**
118
	 * Display the overview page
119
	 *
120
	 * @param string $action Action name
121
	 *
122
	 * @return void
123
	 * @throws \ReflectionException
124
	 * @access public
125
	 */
126
	public function display_overview($action): void
127
	{
128
		$this->ppde_first_start();
129
130
		$this->do_action($action);
131
132
		//Load metadata for this extension
133
		$ext_meta = $this->ppde_ext_manager->get_ext_meta();
134
135
		// Set output block vars for display in the template
136
		$template_vars = $this->prepare_template_vars($ext_meta);
137
		$this->template->assign_vars($template_vars);
138
139
		if ($this->ppde_controller_main->use_sandbox())
140
		{
141
			$sandbox_template_vars = $this->prepare_sandbox_template_vars();
142
			$this->template->assign_vars($sandbox_template_vars);
143
		}
144
	}
145
146
	private function prepare_template_vars($ext_meta)
147
	{
148
		return array(
149
			'L_PPDE_ESI_INSTALL_DATE'        => $this->language->lang('PPDE_ESI_INSTALL_DATE', $ext_meta['extra']['display-name']),
150
			'L_PPDE_ESI_VERSION'             => $this->language->lang('PPDE_ESI_VERSION', $ext_meta['extra']['display-name']),
151
			'PPDE_ESI_INSTALL_DATE'          => $this->user->format_date($this->config['ppde_install_date']),
152
			'PPDE_ESI_TLS'                   => $this->config['ppde_tls_detected'] ? $this->language->lang('PPDE_ESI_DETECTED') : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
153
			'PPDE_ESI_VERSION'               => $ext_meta['version'],
154
			'PPDE_ESI_VERSION_CURL'          => !empty($this->config['ppde_curl_version']) ? $this->config['ppde_curl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
155
			'PPDE_ESI_VERSION_INTL'          => $this->config['ppde_intl_detected'] ? $this->config['ppde_intl_version'] : $this->language->lang('PPDE_ESI_INTL_NOT_DETECTED'),
156
			'PPDE_ESI_VERSION_SSL'           => !empty($this->config['ppde_curl_ssl_version']) ? $this->config['ppde_curl_ssl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
157
			'S_ACTION_OPTIONS'               => $this->auth->acl_get('a_ppde_manage'),
158
			'S_CURL'                         => $this->config['ppde_curl_detected'],
159
			'S_INTL'                         => $this->config['ppde_intl_detected'] && $this->config['ppde_intl_version_valid'],
160
			'S_SSL'                          => $this->config['ppde_curl_detected'],
161
			'S_TLS'                          => $this->config['ppde_tls_detected'],
162
			'STATS_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count'],
163
			'STATS_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count'),
164
			'STATS_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count'],
165
			'STATS_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count'),
166
			'STATS_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count'],
167
			'STATS_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count'),
168
			'U_PPDE_MORE_INFORMATION'        => append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=acp_extensions&amp;mode=main&amp;action=details&amp;ext_name=' . urlencode($ext_meta['name'])),
169
			'U_ACTION'                       => $this->u_action,
170
		);
171
	}
172
173
	/**
174
	 * Prepares the template variables for the PayPal sandbox environment.
175
	 *
176
	 * @return array An array of template variables for the PayPal sandbox environment.
177
	 */
178
	private function prepare_sandbox_template_vars()
179
	{
180
		return [
181
			'S_IPN_TEST'                       => true,
182
			'SANDBOX_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count_ipn'],
183
			'SANDBOX_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count_ipn'),
184
			'SANDBOX_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count_ipn'],
185
			'SANDBOX_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count_ipn'),
186
			'SANDBOX_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count_ipn'],
187
			'SANDBOX_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count_ipn'),
188
		];
189
	}
190
191
	/**
192
	 * Executes the specified action.
193
	 *
194
	 * @param string $action The action to be executed.
195
	 *
196
	 * @return void
197
	 * @throws \ReflectionException
198
	 * @access private
199
	 */
200
	private function do_action($action): void
201
	{
202
		if (!$action)
203
		{
204
			return;
205
		}
206
207
		if (!confirm_box(true))
208
		{
209
			$this->display_confirm($action);
210
			return;
211
		}
212
213
		$this->exec_action($action);
214
	}
215
216
	/**
217
	 * Display confirm box
218
	 *
219
	 * @param string $action Requested action
220
	 *
221
	 * @return void
222
	 * @access private
223
	 */
224
	private function display_confirm($action): void
225
	{
226
		switch ($action)
227
		{
228
			case 'date':
229
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
230
			break;
231
			case 'esi':
232
				$confirm_lang = 'STAT_RETEST_ESI_CONFIRM';
233
			break;
234
			case 'sandbox':
235
				$confirm_lang = 'STAT_RESYNC_SANDBOX_STATS_CONFIRM';
236
			break;
237
			case 'stats':
238
				$confirm_lang = 'STAT_RESYNC_STATS_CONFIRM';
239
			break;
240
			default:
241
				$confirm_lang = 'CONFIRM_OPERATION';
242
		}
243
244
		confirm_box(false, $this->language->lang($confirm_lang), build_hidden_fields(['action' => $action]));
245
	}
246
247
	/**
248
	 * @param string $action Requested action
249
	 *
250
	 * @return void
251
	 * @throws \ReflectionException
252
	 * @access private
253
	 */
254
	private function exec_action($action): void
255
	{
256
		if (!$this->auth->acl_get('a_ppde_manage'))
257
		{
258
			trigger_error($this->language->lang('NO_AUTH_OPERATION') . adm_back_link($this->u_action), E_USER_WARNING);
259
		}
260
261
		switch ($action)
262
		{
263
			case 'date':
264
				$this->config->set('ppde_install_date', time() - 1);
265
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESET_DATE');
266
			break;
267
			case 'esi':
268
				$this->config->set('ppde_first_start', 1);
269
				$this->ppde_first_start();
270
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RETEST_ESI');
271
			break;
272
			case 'sandbox':
273
				$this->ppde_actions->set_ipn_test_properties(true);
274
				$this->ppde_actions->update_overview_stats();
275
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_SANDBOX_RESYNC');
276
			break;
277
			case 'stats':
278
				$this->ppde_actions->set_ipn_test_properties(false);
279
				$this->ppde_actions->update_overview_stats();
280
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESYNC');
281
			break;
282
		}
283
	}
284
285
	/**
286
	 * Returns the percent of items (transactions, donors) per day
287
	 *
288
	 * @param string $config_name
289
	 *
290
	 * @return string
291
	 * @access private
292
	 */
293
	private function per_day_stats($config_name): string
294
	{
295
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
296
	}
297
298
	/**
299
	 * Returns the number of days from the date of installation of the extension.
300
	 *
301
	 * @return float
302
	 * @access private
303
	 */
304
	private function get_install_days()
305
	{
306
		return (float) (time() - $this->config['ppde_install_date']) / 86400;
307
	}
308
}
309