Passed
Pull Request — develop-3.2.x (#69)
by Mario
04:12
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', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $value of phpbb\config\config::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
			$this->config->set('ppde_first_start', /** @scrutinizer ignore-type */ false);
Loading history...
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
			'PPDE_ESI_TLS'                   => $this->config['ppde_tls_detected'] ? $this->language->lang('PPDE_ESI_DETECTED') : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
141
			'PPDE_ESI_VERSION'               => $ext_meta['version'],
142
			'PPDE_ESI_VERSION_CURL'          => !empty($this->config['ppde_curl_version']) ? $this->config['ppde_curl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
143
			'PPDE_ESI_VERSION_SSL'           => !empty($this->config['ppde_curl_ssl_version']) ? $this->config['ppde_curl_ssl_version'] : $this->language->lang('PPDE_ESI_NOT_DETECTED'),
144
			'S_ACTION_OPTIONS'               => ($this->auth->acl_get('a_ppde_manage')) ? true : false,
145
			'S_CURL'                         => $this->config['ppde_curl_detected'],
146
			'S_SSL'                          => $this->config['ppde_curl_detected'],
147
			'S_TLS'                          => $this->config['ppde_tls_detected'],
148
			'STATS_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count'],
149
			'STATS_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count'),
150
			'STATS_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count'],
151
			'STATS_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count'),
152
			'STATS_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count'],
153
			'STATS_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count'),
154
			'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'])),
155
			'U_ACTION'                       => $this->u_action,
156
		));
157
158
		if ($this->ppde_controller_main->use_sandbox())
159
		{
160
			// Set output block vars for display in the template
161
			$this->template->assign_vars(array(
162
				'S_IPN_TEST'                       => true,
163
				'SANDBOX_ANONYMOUS_DONORS_COUNT'   => $this->config['ppde_anonymous_donors_count_ipn'],
164
				'SANDBOX_ANONYMOUS_DONORS_PER_DAY' => $this->per_day_stats('ppde_anonymous_donors_count_ipn'),
165
				'SANDBOX_KNOWN_DONORS_COUNT'       => $this->config['ppde_known_donors_count_ipn'],
166
				'SANDBOX_KNOWN_DONORS_PER_DAY'     => $this->per_day_stats('ppde_known_donors_count_ipn'),
167
				'SANDBOX_TRANSACTIONS_COUNT'       => $this->config['ppde_transactions_count_ipn'],
168
				'SANDBOX_TRANSACTIONS_PER_DAY'     => $this->per_day_stats('ppde_transactions_count_ipn'),
169
			));
170
		}
171
	}
172
173
	/**
174
	 * Do action regarding the value of $action
175
	 *
176
	 * @param string $action Requested action
177
	 *
178
	 * @return void
179
	 * @access private
180
	 */
181
	private function do_action($action)
182
	{
183
		if ($action)
184
		{
185
			if (!confirm_box(true))
186
			{
187
				$this->display_confirm($action);
188
			}
189
			else
190
			{
191
				$this->exec_action($action);
192
			}
193
		}
194
	}
195
196
	/**
197
	 * Display confirm box
198
	 *
199
	 * @param string $action Requested action
200
	 *
201
	 * @return void
202
	 * @access private
203
	 */
204
	private function display_confirm($action)
205
	{
206
		switch ($action)
207
		{
208
			case 'date':
209
				$confirm = true;
210
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
211
			break;
212
			case 'esi':
213
				$confirm = true;
214
				$confirm_lang = 'STAT_RETEST_ESI_CONFIRM';
215
			break;
216
			case 'sandbox':
217
				$confirm = true;
218
				$confirm_lang = 'STAT_RESYNC_SANDBOX_STATS_CONFIRM';
219
			break;
220
			case 'stats':
221
				$confirm = true;
222
				$confirm_lang = 'STAT_RESYNC_STATS_CONFIRM';
223
			break;
224
			default:
225
				$confirm = true;
226
				$confirm_lang = 'CONFIRM_OPERATION';
227
		}
228
229
		if ($confirm)
0 ignored issues
show
introduced by
The condition $confirm is always true.
Loading history...
230
		{
231
			confirm_box(false, $this->language->lang($confirm_lang), build_hidden_fields(array(
232
				'action' => $action,
233
			)));
234
		}
235
	}
236
237
	/**
238
	 * @param string $action Requested action
239
	 *
240
	 * @return void
241
	 * @access private
242
	 */
243
	private function exec_action($action)
244
	{
245
		if (!$this->auth->acl_get('a_ppde_manage'))
246
		{
247
			trigger_error($this->language->lang('NO_AUTH_OPERATION') . adm_back_link($this->u_action), E_USER_WARNING);
248
		}
249
250
		switch ($action)
251
		{
252
			case 'date':
253
				$this->config->set('ppde_install_date', time() - 1);
254
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESET_DATE');
255
			break;
256
			case 'esi':
257
				$this->ppde_ipn_paypal->set_curl_info();
258
				$this->ppde_ipn_paypal->set_remote_detected();
259
				$this->ppde_ipn_paypal->check_tls();
260
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RETEST_ESI');
261
			break;
262
			case 'sandbox':
263
				$this->ppde_actions->set_ipn_test_properties(true);
264
				$this->ppde_actions->update_overview_stats();
265
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_SANDBOX_RESYNC');
266
			break;
267
			case 'stats':
268
				$this->ppde_actions->set_ipn_test_properties(false);
269
				$this->ppde_actions->update_overview_stats();
270
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESYNC');
271
			break;
272
		}
273
	}
274
275
	/**
276
	 * Returns the percent of items (transactions, donors) per day
277
	 *
278
	 * @param string $config_name
279
	 *
280
	 * @return string
281
	 * @access private
282
	 */
283
	private function per_day_stats($config_name)
284
	{
285
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
286
	}
287
288
	/**
289
	 * Returns the number of days from the date of installation of the extension.
290
	 *
291
	 * @return float
292
	 * @access private
293
	 */
294
	private function get_install_days()
295
	{
296
		return (float) (time() - $this->config['ppde_install_date']) / 86400;
297
	}
298
}
299