Total Complexity | 40 |
Total Lines | 484 |
Duplicated Lines | 0 % |
Changes | 24 | ||
Bugs | 0 | Features | 0 |
Complex classes like transactions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use transactions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class transactions |
||
16 | { |
||
17 | protected $db; |
||
18 | protected $ppde_transactions_log_table; |
||
19 | |||
20 | /** |
||
21 | * Constructor |
||
22 | * |
||
23 | * @param driver_interface $db Database connection |
||
24 | * @param string $ppde_transactions_log_table Table name |
||
25 | */ |
||
26 | public function __construct(driver_interface $db, string $ppde_transactions_log_table) |
||
27 | { |
||
28 | $this->db = $db; |
||
29 | $this->ppde_transactions_log_table = $ppde_transactions_log_table; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Builds SQL Query to return Transaction log data |
||
34 | * |
||
35 | * @param int $transaction_id ID of the transaction to fetch (0 for all transactions) |
||
36 | * @return string SQL query string |
||
37 | */ |
||
38 | public function build_sql_data(int $transaction_id = 0): string |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Builds SQL Query array for donor list |
||
59 | * |
||
60 | * @param bool $detailed Whether to include detailed information |
||
61 | * @param string $order_by SQL ORDER BY clause |
||
62 | * @return array SQL query array |
||
63 | */ |
||
64 | public function sql_donorlist_ary(bool $detailed = false, string $order_by = ''): array |
||
65 | { |
||
66 | // Build sql request |
||
67 | $sql_donorslist_ary = [ |
||
68 | 'SELECT' => 'txn.user_id, txn.mc_currency', |
||
69 | 'FROM' => [$this->ppde_transactions_log_table => 'txn'], |
||
70 | 'WHERE' => 'txn.user_id <> ' . ANONYMOUS . " |
||
71 | AND txn.payment_status = 'Completed' |
||
72 | AND txn.test_ipn = 0", |
||
73 | 'GROUP_BY' => 'txn.user_id, txn.mc_currency', |
||
74 | 'ORDER_BY' => $order_by, |
||
75 | ]; |
||
76 | |||
77 | if ($detailed) |
||
78 | { |
||
79 | $sql_donorslist_ary['SELECT'] = 'txn.user_id, txn.mc_currency, MAX(txn.transaction_id) AS max_txn_id, SUM(txn.mc_gross) AS amount, MAX(u.username)'; |
||
80 | $sql_donorslist_ary['LEFT_JOIN'] = [ |
||
81 | [ |
||
82 | 'FROM' => [USERS_TABLE => 'u'], |
||
83 | 'ON' => 'u.user_id = txn.user_id', |
||
84 | ]]; |
||
85 | } |
||
86 | |||
87 | return $sql_donorslist_ary; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Builds SQL Query array for the last donation of a donor |
||
92 | * |
||
93 | * @param int $transaction_id ID of the transaction |
||
94 | * @return array SQL query array |
||
95 | */ |
||
96 | public function sql_last_donation_ary(int $transaction_id): array |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Builds SQL Query to return the donors list |
||
107 | * |
||
108 | * @param array $sql_donorlist_ary SQL query array |
||
109 | * @return string SQL query string |
||
110 | */ |
||
111 | public function build_sql_donorlist_data(array $sql_donorlist_ary): string |
||
112 | { |
||
113 | return $this->db->sql_build_query('SELECT', $sql_donorlist_ary); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Executes a COUNT query and returns the result |
||
118 | * |
||
119 | * @param array $count_sql_ary SQL query array |
||
120 | * @param string $selected_field Field to count |
||
121 | * @return int Count result |
||
122 | */ |
||
123 | public function query_sql_count(array $count_sql_ary, string $selected_field): int |
||
124 | { |
||
125 | $count_sql_ary['SELECT'] = 'COUNT(' . $selected_field . ') AS total_entries'; |
||
126 | |||
127 | if (array_key_exists('GROUP_BY', $count_sql_ary)) |
||
128 | { |
||
129 | $count_sql_ary['SELECT'] = 'COUNT(DISTINCT ' . $count_sql_ary['GROUP_BY'] . ') AS total_entries'; |
||
130 | } |
||
131 | unset($count_sql_ary['ORDER_BY'], $count_sql_ary['GROUP_BY']); |
||
132 | |||
133 | $sql = $this->db->sql_build_query('SELECT', $count_sql_ary); |
||
134 | $result = $this->db->sql_query($sql); |
||
135 | $count = (int) $this->db->sql_fetchfield('total_entries'); |
||
136 | $this->db->sql_freeresult($result); |
||
137 | |||
138 | return $count; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Builds SQL Query array for displaying simple transactions details |
||
143 | * |
||
144 | * @param string $keywords Search keywords |
||
145 | * @param string $sort_by SQL ORDER BY clause |
||
146 | * @param int $log_time Timestamp to filter logs |
||
147 | * @return array SQL query array |
||
148 | */ |
||
149 | public function get_logs_sql_ary(string $keywords, string $sort_by, int $log_time): array |
||
150 | { |
||
151 | $sql_keywords = $this->generate_sql_keyword($keywords); |
||
152 | |||
153 | $sql_ary = [ |
||
154 | 'SELECT' => 'txn.transaction_id, txn.txn_id, txn.test_ipn, txn.confirmed, txn.txn_errors, txn.payment_date, txn.payment_status, txn.user_id, u.username, u.user_colour', |
||
155 | 'FROM' => [ |
||
156 | $this->ppde_transactions_log_table => 'txn', |
||
157 | USERS_TABLE => 'u', |
||
158 | ], |
||
159 | 'WHERE' => 'txn.user_id = u.user_id ' . $sql_keywords, |
||
160 | 'ORDER_BY' => $sort_by, |
||
161 | ]; |
||
162 | |||
163 | if ($log_time) |
||
164 | { |
||
165 | $sql_ary['WHERE'] = 'txn.payment_date >= ' . (int) $log_time . ' AND ' . $sql_ary['WHERE']; |
||
166 | } |
||
167 | |||
168 | return $sql_ary; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Generates SQL condition for the specified keywords |
||
173 | * |
||
174 | * @param string $keywords The keywords the user specified to search for |
||
175 | * @param string $statement_operator SQL operator to use ('AND' by default) |
||
176 | * @return string SQL condition string |
||
177 | */ |
||
178 | private function generate_sql_keyword(string $keywords, string $statement_operator = 'AND'): string |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Retrieves user information based on the donor ID or email |
||
212 | * |
||
213 | * @param string $type Type of identifier ('user', 'username', or 'email') |
||
214 | * @param int|string $arg Identifier value |
||
215 | * @return array User data |
||
216 | */ |
||
217 | public function query_donor_user_data(string $type = 'user', $arg = 1): array |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Builds SQL WHERE clause for donor query |
||
225 | * |
||
226 | * @param string $type Type of identifier |
||
227 | * @param mixed $arg Identifier value |
||
228 | * @return array SQL WHERE clause |
||
229 | */ |
||
230 | private function build_donor_where_clause(string $type, $arg): array |
||
231 | { |
||
232 | switch ($type) |
||
233 | { |
||
234 | case 'user': |
||
235 | return ['WHERE' => 'u.user_id = ' . (int) $arg]; |
||
236 | case 'username': |
||
237 | return ['WHERE' => "u.username_clean = '" . $this->db->sql_escape(utf8_clean_string($arg)) . "'"]; |
||
238 | case 'email': |
||
239 | return ['WHERE' => "u.user_email = '" . $this->db->sql_escape(strtolower($arg)) . "'"]; |
||
240 | default: |
||
241 | return []; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Fetches donor data from the database |
||
247 | * |
||
248 | * @param array $sql_where SQL WHERE clause |
||
249 | * @return array Donor data |
||
250 | */ |
||
251 | private function fetch_donor_data(array $sql_where): array |
||
252 | { |
||
253 | $sql_ary = [ |
||
254 | 'SELECT' => 'u.user_id, u.username, u.user_ppde_donated_amount', |
||
255 | 'FROM' => [USERS_TABLE => 'u'], |
||
256 | ]; |
||
257 | $sql_ary = array_merge($sql_where, $sql_ary); |
||
258 | |||
259 | $sql = $this->db->sql_build_query('SELECT', $sql_ary); |
||
260 | $result = $this->db->sql_query($sql); |
||
261 | $row = $this->db->sql_fetchrow($result); |
||
262 | $this->db->sql_freeresult($result); |
||
263 | |||
264 | return $row ?: []; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Builds log entries for PayPal transactions |
||
269 | * |
||
270 | * @param array $get_logs_sql_ary SQL query array |
||
271 | * @param array $url_ary Array of URLs for building links |
||
272 | * @param int $limit Maximum number of entries to return |
||
273 | * @param int $last_page_offset Offset for pagination |
||
274 | * @return array Log entries |
||
275 | */ |
||
276 | public function build_log_entries(array $get_logs_sql_ary, array $url_ary, int $limit = 0, int $last_page_offset = 0): array |
||
277 | { |
||
278 | $sql = $this->db->sql_build_query('SELECT', $get_logs_sql_ary); |
||
279 | $result = $this->db->sql_query_limit($sql, $limit, $last_page_offset); |
||
280 | |||
281 | $log_entries = []; |
||
282 | |||
283 | while ($row = $this->db->sql_fetchrow($result)) |
||
284 | { |
||
285 | $log_entries[] = $this->build_log_entry($row, $url_ary); |
||
286 | } |
||
287 | $this->db->sql_freeresult($result); |
||
288 | |||
289 | return $log_entries; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Builds a single log entry |
||
294 | * |
||
295 | * @param array $row Database row data |
||
296 | * @param array $url_ary Array of URLs for building links |
||
297 | * @return array Formatted log entry |
||
298 | */ |
||
299 | private function build_log_entry(array $row, array $url_ary): array |
||
310 | ]; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Builds transaction URL for templates |
||
315 | * |
||
316 | * @param int $id Transaction ID |
||
317 | * @param string $txn_id PayPal transaction ID |
||
318 | * @param string $custom_url Custom URL (optional) |
||
319 | * @param bool $colour Whether to apply color to the URL |
||
320 | * @return string Formatted transaction URL or plain transaction ID |
||
321 | */ |
||
322 | private function build_transaction_url(int $id, string $txn_id, string $custom_url = '', bool $colour = false): string |
||
323 | { |
||
324 | if (empty($custom_url)) |
||
325 | { |
||
326 | return $txn_id; |
||
327 | } |
||
328 | |||
329 | $txn_url = $custom_url . '&action=view&id=' . $id; |
||
330 | return $this->format_transaction_link($txn_url, $txn_id, $colour); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Formats the transaction link |
||
335 | * |
||
336 | * @param string $txn_url Transaction URL |
||
337 | * @param string $txn_id PayPal transaction ID |
||
338 | * @param bool $colour Whether to apply color to the URL |
||
339 | * @return string Formatted transaction link |
||
340 | */ |
||
341 | private function format_transaction_link(string $txn_url, string $txn_id, bool $colour): string |
||
342 | { |
||
343 | $style = $colour ? '' : ' style="color: #ff0000;"'; |
||
344 | return sprintf('<a href="%s"%s>%s</a>', $txn_url, $style, $txn_id); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Builds SQL WHERE clause for marked transactions |
||
349 | * |
||
350 | * @param array $marked Array of marked transaction IDs |
||
351 | * @return string SQL WHERE clause |
||
352 | */ |
||
353 | public function build_marked_where_sql(array $marked): string |
||
354 | { |
||
355 | if (empty($marked)) |
||
356 | { |
||
357 | return ''; |
||
358 | } |
||
359 | |||
360 | return ' WHERE ' . $this->db->sql_in_set('transaction_id', array_map('intval', $marked)); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Executes a query to count results for updating stats |
||
365 | * |
||
366 | * @param string $type Type of count query |
||
367 | * @param bool $test_ipn Whether to include test IPNs |
||
368 | * @return int Count result |
||
369 | */ |
||
370 | public function sql_query_count_result(string $type, bool $test_ipn): int |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Builds base SQL query array for stats calculation |
||
388 | * |
||
389 | * @param string $field_name Name of the field to count |
||
390 | * @return array SQL query array |
||
391 | */ |
||
392 | private function sql_select_stats_main(string $field_name): array |
||
393 | { |
||
394 | return [ |
||
395 | 'SELECT' => 'COUNT(DISTINCT txn.' . $field_name . ') AS count_result', |
||
396 | 'FROM' => [$this->ppde_transactions_log_table => 'txn'], |
||
397 | ]; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Adds WHERE clause to the SQL query array for stats calculation |
||
402 | * |
||
403 | * @param array &$sql_ary SQL query array (passed by reference) |
||
404 | * @param string $type Type of count query |
||
405 | * @param int $test_ipn_str Test IPN flag (as integer) |
||
406 | */ |
||
407 | private function add_where_clause(array &$sql_ary, string $type, int $test_ipn_str): void |
||
408 | { |
||
409 | if (strpos($type, 'transactions_count') !== false) |
||
410 | { |
||
411 | $sql_ary['WHERE'] = "confirmed = 1 AND payment_status = 'Completed' AND txn.test_ipn = " . $test_ipn_str; |
||
412 | } |
||
413 | else if (strpos($type, 'known_donors_count') !== false) |
||
414 | { |
||
415 | $sql_ary['LEFT_JOIN'] = [ |
||
416 | [ |
||
417 | 'FROM' => [USERS_TABLE => 'u'], |
||
418 | 'ON' => 'txn.user_id = u.user_id', |
||
419 | ], |
||
420 | ]; |
||
421 | $sql_ary['WHERE'] = '(u.user_type = ' . USER_NORMAL . ' OR u.user_type = ' . USER_FOUNDER . ') AND txn.test_ipn = ' . $test_ipn_str; |
||
422 | } |
||
423 | else if (strpos($type, 'anonymous_donors_count') !== false) |
||
424 | { |
||
425 | $sql_ary['WHERE'] = 'txn.user_id = ' . ANONYMOUS . ' AND txn.test_ipn = ' . $test_ipn_str; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Updates the user's donated amount |
||
431 | * |
||
432 | * @param int $user_id User ID |
||
433 | * @param float $value New donated amount |
||
434 | */ |
||
435 | public function sql_update_user_stats(int $user_id, float $value): void |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Get sort options for transactions |
||
445 | * |
||
446 | * @return array An associative array of sort keys and their corresponding SQL column names |
||
447 | */ |
||
448 | public function get_sort_options(): array |
||
457 | ]; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Prepares transaction data array for entity |
||
462 | * |
||
463 | * @param array $data Raw transaction data |
||
464 | * @return array Formatted transaction data |
||
465 | */ |
||
466 | public function build_transaction_data_ary(array $data): array |
||
499 | ]; |
||
500 | } |
||
502 |