Complex classes like ProfileSubscriptions_Controller 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ProfileSubscriptions_Controller extends Action_Controller |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Holds the the details of the subscription order |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | private $_order; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Holds all the available gateways so they can be initialized |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $_gateways; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The id of the subscription |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $_id_sub; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Default action for the controller |
||
| 44 | * |
||
| 45 | * - This is just a stub as action_subscriptions is called from a menu pick |
||
| 46 | * and not routed through this method. |
||
| 47 | * |
||
| 48 | * @see Action_Controller::action_index() |
||
| 49 | */ |
||
| 50 | public function action_index() |
||
| 51 | { |
||
| 52 | // $this->action_subscriptions(); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Method for doing all the paid subscription stuff - kinda. |
||
| 57 | */ |
||
| 58 | public function action_subscriptions() |
||
| 59 | { |
||
| 60 | global $context, $txt; |
||
| 61 | |||
| 62 | // Load the paid template anyway. |
||
| 63 | loadTemplate('ManagePaid'); |
||
| 64 | loadLanguage('ManagePaid'); |
||
| 65 | |||
| 66 | $memID = currentMemberID(); |
||
| 67 | $context['member']['id'] = $memID; |
||
| 68 | |||
| 69 | // Load all of the subscriptions in the system (loads in to $context) |
||
| 70 | require_once(SUBSDIR . '/PaidSubscriptions.subs.php'); |
||
| 71 | loadSubscriptions(); |
||
| 72 | |||
| 73 | // Remove any invalid ones, ones not properly set up |
||
| 74 | $this->_remove_invalid(); |
||
| 75 | |||
| 76 | // Work out what payment gateways are enabled. |
||
| 77 | $this->_gateways = loadPaymentGateways(); |
||
| 78 | foreach ($this->_gateways as $id => $gateway) |
||
| 79 | { |
||
| 80 | $this->_gateways[$id] = new $gateway['display_class'](); |
||
| 81 | |||
| 82 | if (!$this->_gateways[$id]->gatewayEnabled()) |
||
| 83 | unset($this->_gateways[$id]); |
||
| 84 | } |
||
| 85 | |||
| 86 | // No gateways yet, no way to pay then, blame the admin ! |
||
| 87 | if (empty($this->_gateways)) |
||
| 88 | throw new Elk_Exception($txt['paid_admin_not_setup_gateway']); |
||
| 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 ($current['status'] == 1) |
||
| 97 | $context['subscriptions'][$id]['subscribed'] = true; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Simple "done"? |
||
| 101 | if (isset($this->_req->query->done)) |
||
| 102 | $this->_orderDone($memID); |
||
| 103 | // They have selected a subscription to order. |
||
| 104 | elseif (isset($this->_req->query->confirm) && isset($this->_req->post->sub_id) && is_array($this->_req->post->sub_id)) |
||
| 105 | $this->_confirmOrder($memID); |
||
| 106 | // Show the users whats available and what they have |
||
| 107 | else |
||
| 108 | $context['sub_template'] = 'user_subscription'; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Removes any subscriptions that are found to be invalid |
||
| 113 | * |
||
| 114 | * - Invalid defined by missing cost or missing period |
||
| 115 | */ |
||
| 116 | private function _remove_invalid() |
||
| 117 | { |
||
| 118 | global $context; |
||
| 119 | |||
| 120 | foreach ($context['subscriptions'] as $id => $sub) |
||
| 121 | { |
||
| 122 | // Work out the costs. |
||
| 123 | $costs = Util::unserialize($sub['real_cost']); |
||
| 124 | |||
| 125 | $cost_array = array(); |
||
| 126 | |||
| 127 | // Flexible cost to time? |
||
| 128 | if ($sub['real_length'] === 'F') |
||
| 129 | { |
||
| 130 | foreach ($costs as $duration => $cost) |
||
| 131 | { |
||
| 132 | if ($cost != 0) |
||
| 133 | { |
||
| 134 | $cost_array[$duration] = $cost; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | else |
||
| 139 | { |
||
| 140 | $cost_array['fixed'] = $costs['fixed']; |
||
| 141 | } |
||
| 142 | |||
| 143 | // No cost associated with it, then drop it |
||
| 144 | if (empty($cost_array)) |
||
| 145 | { |
||
| 146 | unset($context['subscriptions'][$id]); |
||
| 147 | } |
||
| 148 | else |
||
| 149 | { |
||
| 150 | $context['subscriptions'][$id]['member'] = 0; |
||
| 151 | $context['subscriptions'][$id]['subscribed'] = false; |
||
| 152 | $context['subscriptions'][$id]['costs'] = $cost_array; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * When the chosen payment gateway is done and it supports a receipt link url |
||
| 159 | * it will be set to come here. |
||
| 160 | * |
||
| 161 | * - This is NOT the same as the notify processing url which will point to subscriptions.php |
||
| 162 | * - Accessed by ?action=profile;u=123;area=subscriptions;sub_id=?;done |
||
| 163 | * |
||
| 164 | * @param int $memID |
||
| 165 | */ |
||
| 166 | private function _orderDone($memID) |
||
| 167 | { |
||
| 168 | global $context; |
||
| 169 | |||
| 170 | $sub_id = (int) $this->_req->query->sub_id; |
||
| 171 | |||
| 172 | // Must exist but let's be sure... |
||
| 173 | if (isset($context['current'][$sub_id])) |
||
| 174 | { |
||
| 175 | // What are the pending details? |
||
| 176 | $current_pending = Util::unserialize($context['current'][$sub_id]['pending_details']); |
||
| 177 | |||
| 178 | // Nothing pending, nothing to do |
||
| 179 | if (!empty($current_pending)) |
||
| 180 | { |
||
| 181 | $current_pending = array_reverse($current_pending); |
||
| 182 | foreach ($current_pending as $id => $sub) |
||
| 183 | { |
||
| 184 | // Just find one and change it to payback |
||
| 185 | if ($sub[0] == $sub_id && trim($sub[3]) === 'prepay') |
||
| 186 | { |
||
| 187 | $current_pending[$id][3] = 'payback'; |
||
| 188 | break; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | // Save the details back. |
||
| 193 | $pending_details = serialize($current_pending); |
||
| 194 | updatePendingStatus($context['current'][$sub_id]['id'], $memID, $pending_details); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | // A simple thank you |
||
| 199 | $context['sub_template'] = 'paid_done'; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Called when the user selects "Order" from the subscription page |
||
| 204 | * |
||
| 205 | * - Accessed with ?action=profile;u=123;area=subscriptions;confirm |
||
| 206 | * |
||
| 207 | * @param int $memID The id of the member who is ordering |
||
| 208 | * |
||
| 209 | * @throws Elk_Exception paid_sub_not_active |
||
| 210 | */ |
||
| 211 | private function _confirmOrder($memID) |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Sets the value/cost/period/unit of the chosen order for use in templates |
||
| 300 | */ |
||
| 301 | private function _set_value_cost_context() |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Sets the required payment form fields for the various payment gateways |
||
| 329 | * |
||
| 330 | * @param int $memID The id of the member who is ordering |
||
| 331 | * @param string period xx for none or a value of time |
||
|
|
|||
| 332 | */ |
||
| 333 | private function _set_payment_gatway_context($memID, $period) |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths