main_donor_list::handle()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 96
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 53
c 2
b 0
f 0
nc 48
nop 0
dl 0
loc 96
rs 7.781

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
12
13
class main_donor_list extends main_controller
14
{
15
	/** @var \skouat\ppde\entity\transactions */
16
	protected $ppde_entity_transactions;
17
	/** @var \skouat\ppde\operators\transactions */
18
	protected $ppde_operator_transactions;
19
	/** @var  \phpbb\pagination */
20
	protected $pagination;
21
	/** @var  \phpbb\path_helper */
22
	protected $path_helper;
23
	/** @var string */
24
	private $u_action;
25
26
	public function set_entity_transactions(\skouat\ppde\entity\transactions $ppde_entity_transactions): void
27
	{
28
		$this->ppde_entity_transactions = $ppde_entity_transactions;
29
	}
30
31
	public function set_operator_transactions(\skouat\ppde\operators\transactions $ppde_operator_transactions): void
32
	{
33
		$this->ppde_operator_transactions = $ppde_operator_transactions;
34
	}
35
36
	public function set_pagination(\phpbb\pagination $pagination): void
37
	{
38
		$this->pagination = $pagination;
39
	}
40
41
	public function set_path_helper(\phpbb\path_helper $path_helper): void
42
	{
43
		$this->path_helper = $path_helper;
44
	}
45
46
	public function handle()
47
	{
48
		// If the donorlist is not enabled, redirect users back to the forum index.
49
		// Else if user is not allowed to view the donors list, disallow access to the extension page.
50
		if (!$this->donorlist_is_enabled())
51
		{
52
			redirect(append_sid($this->root_path . 'index.' . $this->php_ext));
53
		}
54
		else if (!$this->ppde_auth->can_view_ppde_donorlist())
55
		{
56
			trigger_error('NOT_AUTHORISED');
57
		}
58
59
		// Set up general vars
60
		$default_key = 'd';
61
		$sort_key = $this->request->variable('sk', $default_key);
62
		$sort_dir = $this->request->variable('sd', 'd');
63
		$start = $this->request->variable('start', 0);
64
65
		// Sorting and order
66
		$sort_key_sql = ['a' => 'amount', 'd' => 'txn.payment_date', 'u' => 'u.username_clean'];
67
68
		if (!isset($sort_key_sql[$sort_key]))
69
		{
70
			$sort_key = $default_key;
71
		}
72
73
		$order_by = ($sort_key === 'a' ? $sort_key_sql[$sort_key] . ' ' : 'MAX(' . $sort_key_sql[$sort_key] . ') ') . (($sort_dir === 'a') ? 'ASC' : 'DESC');
74
75
		// Build a relevant pagination_url and sort_url.
76
		// We do not use request_var() here directly to save some calls (not all variables are set)
77
		$check_params = [
78
			'sk'    => ['sk', $default_key],
79
			'sd'    => ['sd', 'a'],
80
			'start' => ['start', 0],
81
		];
82
83
		$params = $this->check_params($check_params, ['start']);
84
		$sort_params = $this->check_params($check_params, ['sk', 'sd']);
85
86
		// Set '$this->u_action'
87
		$use_page = $this->u_action ?: $this->user->page['page_name'];
88
		$this->u_action = reapply_sid($this->path_helper->get_valid_page($use_page, (bool) $this->config['enable_mod_rewrite']));
89
90
		$pagination_url = append_sid($this->u_action, implode('&amp;', $params), true, false, true);
91
		$sort_url = $this->set_url_delim(append_sid($this->u_action, implode('&amp;', $sort_params), true, false, true), $sort_params);
92
93
		$sql_count_donors = $this->ppde_operator_transactions->sql_donorlist_ary();
94
		$total_donors = $this->ppde_operator_transactions->query_sql_count($sql_count_donors, 'txn.user_id');
95
		$start = $this->pagination->validate_start($start, (int) $this->config['topics_per_page'], $total_donors);
96
97
		$this->pagination->generate_template_pagination($pagination_url, 'pagination', 'start', $total_donors, (int) $this->config['topics_per_page'], $start);
98
99
		// Assign vars to the template
100
		$this->template->assign_vars([
101
			'L_PPDE_DONORLIST_TITLE' => $this->language->lang('PPDE_DONORLIST_TITLE'),
102
			'TOTAL_DONORS'           => $this->language->lang('PPDE_DONORS', $total_donors),
103
			'U_SORT_AMOUNT'          => $sort_url . 'sk=a&amp;sd=' . $this->set_sort_key($sort_key, 'a', $sort_dir),
104
			'U_SORT_DONATED'         => $sort_url . 'sk=d&amp;sd=' . $this->set_sort_key($sort_key, 'd', $sort_dir),
105
			'U_SORT_USERNAME'        => $sort_url . 'sk=u&amp;sd=' . $this->set_sort_key($sort_key, 'u', $sort_dir),
106
		]);
107
108
		// Adds fields to the table schema needed by entity->import()
109
		$donorlist_table_schema = [
110
			'item_amount'      => ['name' => 'amount', 'type' => 'float'],
111
			'item_max_txn_id'  => ['name' => 'max_txn_id', 'type' => 'integer'],
112
			'item_user_id'     => ['name' => 'user_id', 'type' => 'integer'],
113
			'item_mc_currency' => ['name' => 'mc_currency', 'type' => 'string'],
114
		];
115
116
		$sql_donorlist_ary = $this->ppde_operator_transactions->sql_donorlist_ary(true, $order_by);
117
		$data_ary = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($sql_donorlist_ary), $donorlist_table_schema, (int) $this->config['topics_per_page'], $start, true);
118
119
		// Adds fields to the table schema needed by entity->import()
120
		$last_donation_table_schema = [
121
			'item_payment_date' => ['name' => 'payment_date', 'type' => 'integer'],
122
			'item_mc_gross'     => ['name' => 'mc_gross', 'type' => 'float'],
123
			'item_mc_currency'  => ['name' => 'mc_currency', 'type' => 'string'],
124
		];
125
126
		foreach ($data_ary as $data)
127
		{
128
			$get_last_transaction_sql_ary = $this->ppde_operator_transactions->sql_last_donation_ary($data['max_txn_id']);
129
			$last_donation_data = $this->ppde_entity_transactions->get_data($this->ppde_operator_transactions->build_sql_donorlist_data($get_last_transaction_sql_ary), $last_donation_table_schema, 0, 0, true);
130
			$this->actions_currency->set_currency_data_from_iso_code($last_donation_data[0]['mc_currency']);
131
132
			$this->template->assign_block_vars('donorrow', [
133
				'PPDE_DONOR_USERNAME'       => $this->user_loader->get_username($data['user_id'], 'full', false, false, true),
134
				'PPDE_LAST_DONATED_AMOUNT'  => $this->actions_currency->format_currency((float) $last_donation_data[0]['mc_gross']),
135
				'PPDE_LAST_PAYMENT_DATE'    => $this->user->format_date($last_donation_data[0]['payment_date']),
136
				'PPDE_TOTAL_DONATED_AMOUNT' => $this->actions_currency->format_currency((float) $data['amount']),
137
			]);
138
		}
139
140
		// Send all data to the template file
141
		return $this->send_data_to_template();
142
	}
143
144
	/**
145
	 * @param array    $params_ary
146
	 * @param string[] $excluded_keys
147
	 *
148
	 * @return array
149
	 * @access private
150
	 */
151
	private function check_params($params_ary, $excluded_keys): array
152
	{
153
		$params = [];
154
155
		foreach ($params_ary as $key => $call)
156
		{
157
			if (!$this->request->is_set($key))
158
			{
159
				continue;
160
			}
161
162
			$param = call_user_func_array([$this->request, 'variable'], $call);
163
			$param = urlencode($key) . '=' . ((is_string($param)) ? urlencode($param) : $param);
164
165
			if (!in_array($key, $excluded_keys))
166
			{
167
				$params[] = $param;
168
			}
169
		}
170
171
		return $params;
172
	}
173
174
	/**
175
	 * Simply adds an url or &amp; delimiter to the url when params is empty
176
	 *
177
	 * @param $url
178
	 * @param $params
179
	 *
180
	 * @return string
181
	 * @access private
182
	 */
183
	private function set_url_delim($url, $params): string
184
	{
185
		return empty($params) ? $url . '?' : $url . '&amp;';
186
	}
187
188
	/**
189
	 * Set the sort key value
190
	 *
191
	 * @param string $sk
192
	 * @param string $sk_comp
193
	 * @param string $sd
194
	 *
195
	 * @return string
196
	 * @access private
197
	 */
198
	private function set_sort_key($sk, $sk_comp, $sd): string
199
	{
200
		return ($sk === $sk_comp && $sd === 'a') ? 'd' : 'a';
201
	}
202
203
	/**
204
	 * Send data to the template file
205
	 *
206
	 * @return \Symfony\Component\HttpFoundation\Response
207
	 * @access private
208
	 */
209
	private function send_data_to_template()
210
	{
211
		return $this->helper->render('donorlist_body.html', $this->language->lang('PPDE_DONORLIST_TITLE'));
212
	}
213
}
214