| Total Complexity | 43 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ProfileSubscriptions 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 ProfileSubscriptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ProfileSubscriptions extends AbstractController |
||
| 28 | { |
||
| 29 | /** @var array Holds the details of the subscription order */ |
||
| 30 | private $_order; |
||
| 31 | |||
| 32 | /** @var array Holds all the available gateways so they can be initialized */ |
||
| 33 | private $_gateways; |
||
| 34 | |||
| 35 | /** @var int The id of the subscription */ |
||
| 36 | private $_id_sub; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Default action for the controller |
||
| 40 | * |
||
| 41 | * - This is just a stub as action_subscriptions is called from a menu pick |
||
| 42 | * and not routed through this method. |
||
| 43 | * |
||
| 44 | * @see AbstractController::action_index() |
||
| 45 | */ |
||
| 46 | public function action_index() |
||
| 48 | // $this->action_subscriptions(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Method for doing all the paid subscription stuff - kinda. |
||
| 53 | */ |
||
| 54 | public function action_subscriptions(): void |
||
| 55 | { |
||
| 56 | global $context, $txt; |
||
| 57 | |||
| 58 | // Load the paid template anyway. |
||
| 59 | theme()->getTemplates()->load('ManagePaid'); |
||
| 60 | Txt::load('ManagePaid'); |
||
| 61 | |||
| 62 | $memID = currentMemberID(); |
||
| 63 | $context['member']['id'] = $memID; |
||
| 64 | |||
| 65 | // Load all the subscriptions in the system (loads in to $context) |
||
| 66 | require_once(SUBSDIR . '/PaidSubscriptions.subs.php'); |
||
| 67 | loadSubscriptions(); |
||
| 68 | |||
| 69 | // Remove any invalid ones, ones not properly set up |
||
| 70 | $this->_remove_invalid(); |
||
| 71 | |||
| 72 | // Work out what payment gateways are enabled. |
||
| 73 | $this->_gateways = loadPaymentGateways(); |
||
| 74 | foreach ($this->_gateways as $id => $gateway) |
||
| 75 | { |
||
| 76 | $this->_gateways[$id] = new $gateway['display_class'](); |
||
| 77 | |||
| 78 | if (!$this->_gateways[$id]->gatewayEnabled()) |
||
| 79 | { |
||
| 80 | unset($this->_gateways[$id]); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | // No gateways yet, no way to pay then, blame the admin! |
||
| 85 | if (empty($this->_gateways)) |
||
| 86 | { |
||
| 87 | throw new Exception($txt['paid_admin_not_setup_gateway']); |
||
| 88 | } |
||
| 89 | |||
| 90 | // Get the members' current subscriptions. |
||
| 91 | $context['current'] = loadMemberSubscriptions($memID, $context['subscriptions']); |
||
| 92 | |||
| 93 | // Find the active subscribed ones |
||
| 94 | foreach ($context['current'] as $id => $current) |
||
| 95 | { |
||
| 96 | if ((int) $current['status'] === 1) |
||
| 97 | { |
||
| 98 | $context['subscriptions'][$id]['subscribed'] = true; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // Simple "done"? |
||
| 103 | if (isset($this->_req->query->done)) |
||
| 104 | { |
||
| 105 | $this->_orderDone($memID); |
||
| 106 | } |
||
| 107 | // They have selected a subscription to order. |
||
| 108 | elseif (isset($this->_req->query->confirm, $this->_req->post->sub_id) && is_array($this->_req->post->sub_id)) |
||
| 109 | { |
||
| 110 | $this->_confirmOrder($memID); |
||
| 111 | } |
||
| 112 | // Show the users what's available and what they have |
||
| 113 | else |
||
| 114 | { |
||
| 115 | $context['sub_template'] = 'user_subscription'; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Removes any subscriptions that are found to be invalid |
||
| 121 | * |
||
| 122 | * - Invalid defined by missing cost or missing period |
||
| 123 | */ |
||
| 124 | private function _remove_invalid(): void |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * When the chosen payment gateway is done, and it supports a receipt link url, |
||
| 167 | * it will be set to come here. |
||
| 168 | * |
||
| 169 | * - This is different from the notification processing url which will point to subscriptions.php |
||
| 170 | * - Accessed by ?action=profile;u=123;area=subscriptions;sub_id=?;done |
||
| 171 | * |
||
| 172 | * @param int $memID |
||
| 173 | */ |
||
| 174 | private function _orderDone($memID): void |
||
| 175 | { |
||
| 176 | global $context; |
||
| 177 | |||
| 178 | $sub_id = (int) $this->_req->query->sub_id; |
||
| 179 | |||
| 180 | // Must exist, but let's be sure... |
||
| 181 | if (isset($context['current'][$sub_id])) |
||
| 182 | { |
||
| 183 | // What are the pending details? |
||
| 184 | $current_pending = Util::unserialize($context['current'][$sub_id]['pending_details']); |
||
| 185 | |||
| 186 | // Nothing pending, nothing to do |
||
| 187 | if (!empty($current_pending)) |
||
| 188 | { |
||
| 189 | $current_pending = array_reverse($current_pending); |
||
| 190 | foreach ($current_pending as $id => $sub) |
||
| 191 | { |
||
| 192 | // Just find one and change it to payback |
||
| 193 | if ($sub[0] == $sub_id && trim($sub[3]) === 'prepay') |
||
| 194 | { |
||
| 195 | $current_pending[$id][3] = 'payback'; |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | // Save the details back. |
||
| 201 | $pending_details = serialize($current_pending); |
||
| 202 | updatePendingStatus($context['current'][$sub_id]['id'], $memID, $pending_details); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | // Say thank you |
||
| 207 | $context['sub_template'] = 'paid_done'; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Called when the user selects "Order" from the subscription page |
||
| 212 | * |
||
| 213 | * - Accessed with ?action=profile;u=123;area=subscriptions;confirm |
||
| 214 | * |
||
| 215 | * @param int $memID The id of the member who is ordering |
||
| 216 | * |
||
| 217 | * @throws Exception paid_sub_not_active |
||
| 218 | */ |
||
| 219 | private function _confirmOrder($memID): void |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Sets the value/cost/period/unit of the chosen order for use in templates |
||
| 319 | */ |
||
| 320 | private function _set_value_cost_context(): void |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Sets the required payment form fields for the various payment gateways |
||
| 348 | * |
||
| 349 | * @param int $memID The id of the member who is ordering |
||
| 350 | * @param string $period xx for none or a value of time |
||
| 351 | */ |
||
| 352 | private function _set_payment_gateway_context($memID, $period): void |
||
| 369 | } |
||
| 370 | } |
||
| 374 |