Passed
Branch 3.2.x (99ec7e)
by Mario
04:20
created

overview_controller   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 120
dl 0
loc 259
rs 10
c 0
b 0
f 0
wmc 25

7 Methods

Rating   Name   Duplication   Size   Complexity  
B display_overview() 0 49 7
A __construct() 0 38 1
A do_action() 0 11 3
B exec_action() 0 29 6
B display_confirm() 0 29 6
A get_install_days() 0 3 1
A per_day_stats() 0 3 1
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', (string) false);
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
				return;
189
			}
190
191
			$this->exec_action($action);
192
		}
193
	}
194
195
	/**
196
	 * Display confirm box
197
	 *
198
	 * @param string $action Requested action
199
	 *
200
	 * @return void
201
	 * @access private
202
	 */
203
	private function display_confirm($action)
204
	{
205
		switch ($action)
206
		{
207
			case 'date':
208
				$confirm = true;
209
				$confirm_lang = 'STAT_RESET_DATE_CONFIRM';
210
			break;
211
			case 'esi':
212
				$confirm = true;
213
				$confirm_lang = 'STAT_RETEST_ESI_CONFIRM';
214
			break;
215
			case 'sandbox':
216
				$confirm = true;
217
				$confirm_lang = 'STAT_RESYNC_SANDBOX_STATS_CONFIRM';
218
			break;
219
			case 'stats':
220
				$confirm = true;
221
				$confirm_lang = 'STAT_RESYNC_STATS_CONFIRM';
222
			break;
223
			default:
224
				$confirm = true;
225
				$confirm_lang = 'CONFIRM_OPERATION';
226
		}
227
228
		if ($confirm)
0 ignored issues
show
introduced by
The condition $confirm is always true.
Loading history...
229
		{
230
			confirm_box(false, $this->language->lang($confirm_lang), build_hidden_fields(array(
231
				'action' => $action,
232
			)));
233
		}
234
	}
235
236
	/**
237
	 * @param string $action Requested action
238
	 *
239
	 * @return void
240
	 * @access private
241
	 */
242
	private function exec_action($action)
243
	{
244
		if (!$this->auth->acl_get('a_ppde_manage'))
245
		{
246
			trigger_error($this->language->lang('NO_AUTH_OPERATION') . adm_back_link($this->u_action), E_USER_WARNING);
247
		}
248
249
		switch ($action)
250
		{
251
			case 'date':
252
				$this->config->set('ppde_install_date', time() - 1);
253
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESET_DATE');
254
			break;
255
			case 'esi':
256
				$this->ppde_ipn_paypal->set_curl_info();
257
				$this->ppde_ipn_paypal->set_remote_detected();
258
				$this->ppde_ipn_paypal->check_tls();
259
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RETEST_ESI');
260
			break;
261
			case 'sandbox':
262
				$this->ppde_actions->set_ipn_test_properties(true);
263
				$this->ppde_actions->update_overview_stats();
264
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_SANDBOX_RESYNC');
265
			break;
266
			case 'stats':
267
				$this->ppde_actions->set_ipn_test_properties(false);
268
				$this->ppde_actions->update_overview_stats();
269
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_STAT_RESYNC');
270
			break;
271
		}
272
	}
273
274
	/**
275
	 * Returns the percent of items (transactions, donors) per day
276
	 *
277
	 * @param string $config_name
278
	 *
279
	 * @return string
280
	 * @access private
281
	 */
282
	private function per_day_stats($config_name)
283
	{
284
		return sprintf('%.2f', (float) $this->config[$config_name] / $this->get_install_days());
285
	}
286
287
	/**
288
	 * Returns the number of days from the date of installation of the extension.
289
	 *
290
	 * @return float
291
	 * @access private
292
	 */
293
	private function get_install_days()
294
	{
295
		return (float) (time() - $this->config['ppde_install_date']) / 86400;
296
	}
297
}
298