Passed
Push — develop-3.2.x ( 41953a...e9a235 )
by Mario
03:04
created

overview_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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