Complex classes like OrderProcessor 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 OrderProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class OrderProcessor |
||
|
|||
10 | { |
||
11 | /** |
||
12 | * @var Order |
||
13 | */ |
||
14 | protected $order; |
||
15 | |||
16 | /** |
||
17 | * @var OrderEmailNotifier |
||
18 | */ |
||
19 | protected $notifier; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $error; |
||
25 | |||
26 | /** |
||
27 | * Static way to create the order processor. |
||
28 | * Makes creating a processor easier. |
||
29 | * |
||
30 | * @param Order $order |
||
31 | */ |
||
32 | 6 | public static function create(Order $order) |
|
36 | |||
37 | /** |
||
38 | * Assign the order to a local variable |
||
39 | * |
||
40 | * @param Order $order |
||
41 | */ |
||
42 | 8 | public function __construct(Order $order) |
|
47 | |||
48 | /** |
||
49 | * URL to display success message to the user. |
||
50 | * Happens after any potential offsite gateway redirects. |
||
51 | * |
||
52 | * @return String Relative URL |
||
53 | */ |
||
54 | 4 | public function getReturnUrl() |
|
58 | |||
59 | /** |
||
60 | * Create a payment model, and provide link to redirect to external gateway, |
||
61 | * or redirect to order link. |
||
62 | * |
||
63 | * @return string - url for redirection after payment has been made |
||
64 | */ |
||
65 | 2 | public function makePayment($gateway, $gatewaydata = array()) |
|
88 | |||
89 | /** |
||
90 | * Map shop data to omnipay fields |
||
91 | * |
||
92 | * @param array $customData Usually user submitted data. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | 1 | protected function getGatewayData($customData) |
|
97 | { |
||
98 | 1 | $shipping = $this->order->getShippingAddress(); |
|
99 | 1 | $billing = $this->order->getBillingAddress(); |
|
100 | |||
101 | 1 | $numPayments = Payment::get() |
|
102 | 1 | ->filter(array('OrderID' => $this->order->ID)) |
|
103 | 1 | ->count() - 1; |
|
104 | |||
105 | 1 | $transactionId = $this->order->Reference . ($numPayments > 0 ? "-$numPayments" : ''); |
|
106 | |||
107 | 1 | return array_merge( |
|
108 | 1 | $customData, |
|
109 | array( |
||
110 | 1 | 'transactionId' => $transactionId, |
|
111 | 1 | 'firstName' => $this->order->FirstName, |
|
112 | 1 | 'lastName' => $this->order->Surname, |
|
113 | 1 | 'email' => $this->order->Email, |
|
114 | 1 | 'company' => $this->order->Company, |
|
115 | 1 | 'billingAddress1' => $billing->Address, |
|
116 | 1 | 'billingAddress2' => $billing->AddressLine2, |
|
117 | 1 | 'billingCity' => $billing->City, |
|
118 | 1 | 'billingPostcode' => $billing->PostalCode, |
|
119 | 1 | 'billingState' => $billing->State, |
|
120 | 1 | 'billingCountry' => $billing->Country, |
|
121 | 1 | 'billingPhone' => $billing->Phone, |
|
122 | 1 | 'shippingAddress1' => $shipping->Address, |
|
123 | 1 | 'shippingAddress2' => $shipping->AddressLine2, |
|
124 | 1 | 'shippingCity' => $shipping->City, |
|
125 | 1 | 'shippingPostcode' => $shipping->PostalCode, |
|
126 | 1 | 'shippingState' => $shipping->State, |
|
127 | 1 | 'shippingCountry' => $shipping->Country, |
|
128 | 1 | 'shippingPhone' => $shipping->Phone, |
|
129 | ) |
||
130 | 1 | ); |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Create a new payment for an order |
||
135 | */ |
||
136 | 2 | public function createPayment($gateway) |
|
137 | { |
||
138 | 2 | if (!GatewayInfo::is_supported($gateway)) { |
|
139 | $this->error( |
||
140 | _t( |
||
141 | "PaymentProcessor.INVALID_GATEWAY", |
||
142 | "`{gateway}` isn't a valid payment gateway.", |
||
143 | 'gateway is the name of the payment gateway', |
||
144 | array('gateway' => $gateway) |
||
145 | ) |
||
146 | ); |
||
147 | return false; |
||
148 | } |
||
149 | 2 | if (!$this->order->canPay(Member::currentUser())) { |
|
150 | $this->error(_t("PaymentProcessor.CANTPAY", "Order can't be paid for.")); |
||
151 | return false; |
||
152 | } |
||
153 | 2 | $payment = Payment::create() |
|
154 | 2 | ->init($gateway, $this->order->TotalOutstanding(), ShopConfig::get_base_currency()); |
|
155 | 2 | $this->order->Payments()->add($payment); |
|
156 | 2 | return $payment; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * Complete payment processing |
||
161 | * - send receipt |
||
162 | * - update order status accordingling |
||
163 | * - fire event hooks |
||
164 | */ |
||
165 | public function completePayment() |
||
166 | { |
||
167 | if (!$this->order->Paid) { |
||
168 | $this->order->extend('onPayment'); //a payment has been made |
||
169 | //place the order, if not already placed |
||
170 | if ($this->canPlace($this->order)) { |
||
171 | $this->placeOrder(); |
||
172 | } else { |
||
173 | if ($this->order->Locale) { |
||
174 | ShopTools::install_locale($this->order->Locale); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | if ( |
||
179 | // Standard order |
||
180 | ($this->order->GrandTotal() > 0 && $this->order->TotalOutstanding() <= 0) |
||
181 | // Zero-dollar order (e.g. paid with loyalty points) |
||
182 | || ($this->order->GrandTotal() == 0 && Order::config()->allow_zero_order_total) |
||
183 | ) { |
||
184 | //set order as paid |
||
185 | $this->order->Status = 'Paid'; |
||
186 | $this->order->Paid = SS_Datetime::now()->Rfc2822(); |
||
187 | $this->order->write(); |
||
188 | foreach ($this->order->Items() as $item) { |
||
189 | $item->onPayment(); |
||
190 | } |
||
191 | //all payment is settled |
||
192 | $this->order->extend('onPaid'); |
||
193 | } |
||
194 | if (!$this->order->ReceiptSent) { |
||
195 | $this->notifier->sendReceipt(); |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Determine if an order can be placed. |
||
202 | * |
||
203 | * @param boolean $order |
||
204 | */ |
||
205 | 4 | public function canPlace(Order $order) |
|
206 | { |
||
207 | 4 | if (!$order) { |
|
208 | $this->error(_t("OrderProcessor.NULL", "Order does not exist.")); |
||
209 | return false; |
||
210 | } |
||
211 | //order status is applicable |
||
212 | 4 | if (!$order->IsCart()) { |
|
213 | 1 | $this->error(_t("OrderProcessor.NOTCART", "Order is not a cart.")); |
|
214 | 1 | return false; |
|
215 | } |
||
216 | //order has products |
||
217 | 3 | if ($order->Items()->Count() <= 0) { |
|
218 | $this->error(_t("OrderProcessor.NOITEMS", "Order has no items.")); |
||
219 | return false; |
||
220 | } |
||
221 | |||
222 | 3 | return true; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * Takes an order from being a cart to awaiting payment. |
||
227 | * |
||
228 | * @param Member $member - assign a member to the order |
||
229 | * |
||
230 | * @return boolean - success/failure |
||
231 | */ |
||
232 | 4 | public function placeOrder() |
|
233 | { |
||
234 | 4 | if (!$this->order) { |
|
235 | $this->error(_t("OrderProcessor.NULL", "A new order has not yet been started.")); |
||
236 | return false; |
||
237 | } |
||
238 | 4 | if (!$this->canPlace($this->order)) { //final cart validation |
|
239 | 1 | return false; |
|
240 | } |
||
241 | |||
242 | 3 | if ($this->order->Locale) { |
|
243 | 3 | ShopTools::install_locale($this->order->Locale); |
|
244 | 3 | } |
|
245 | |||
246 | //remove from session |
||
247 | 3 | $cart = ShoppingCart::curr(); |
|
248 | 3 | if ($cart && $cart->ID == $this->order->ID) { |
|
249 | 3 | ShoppingCart::singleton()->clear(); |
|
250 | 3 | } |
|
251 | //update status |
||
252 | 3 | if ($this->order->TotalOutstanding()) { |
|
253 | 3 | $this->order->Status = 'Unpaid'; |
|
254 | 3 | } else { |
|
255 | $this->order->Status = 'Paid'; |
||
256 | } |
||
257 | 3 | if (!$this->order->Placed) { |
|
258 | 3 | $this->order->Placed = SS_Datetime::now()->Rfc2822(); //record placed order datetime |
|
259 | 3 | if ($request = Controller::curr()->getRequest()) { |
|
260 | 3 | $this->order->IPAddress = $request->getIP(); //record client IP |
|
261 | 3 | } |
|
262 | 3 | } |
|
263 | //re-write all attributes and modifiers to make sure they are up-to-date before they can't be changed again |
||
264 | 3 | $items = $this->order->Items(); |
|
265 | 3 | if ($items->exists()) { |
|
266 | 3 | foreach ($items as $item) { |
|
267 | 3 | $item->onPlacement(); |
|
268 | 3 | $item->write(); |
|
269 | 3 | } |
|
270 | 3 | } |
|
271 | 3 | $modifiers = $this->order->Modifiers(); |
|
272 | 3 | if ($modifiers->exists()) { |
|
273 | foreach ($modifiers as $modifier) { |
||
274 | $modifier->write(); |
||
275 | } |
||
276 | } |
||
277 | //add member to order & customers group |
||
278 | 3 | if ($member = Member::currentUser()) { |
|
279 | 2 | if (!$this->order->MemberID) { |
|
280 | $this->order->MemberID = $member->ID; |
||
281 | } |
||
282 | 2 | $cgroup = ShopConfig::current()->CustomerGroup(); |
|
283 | 2 | if ($cgroup->exists()) { |
|
284 | $member->Groups()->add($cgroup); |
||
285 | } |
||
286 | 2 | } |
|
287 | //allow decorators to do stuff when order is saved. |
||
288 | 3 | $this->order->extend('onPlaceOrder'); |
|
289 | 3 | $this->order->write(); |
|
290 | |||
291 | //send confirmation if configured and receipt hasn't been sent |
||
292 | if ( |
||
293 | 3 | self::config()->send_confirmation |
|
294 | 3 | && !$this->order->ReceiptSent |
|
295 | 3 | ) { |
|
296 | $this->notifier->sendConfirmation(); |
||
297 | } |
||
298 | |||
299 | //notify admin, if configured |
||
300 | 3 | if (self::config()->send_admin_notification) { |
|
301 | $this->notifier->sendAdminNotification(); |
||
302 | } |
||
303 | |||
304 | // Save order reference to session |
||
305 | 3 | OrderManipulation::add_session_order($this->order); |
|
306 | |||
307 | 3 | return true; //report success |
|
308 | } |
||
309 | |||
310 | /** |
||
311 | * @return Order |
||
312 | */ |
||
313 | public function getOrder() |
||
314 | { |
||
315 | return $this->order; |
||
316 | } |
||
317 | |||
318 | 1 | public function getError() |
|
322 | |||
323 | 6 | private function error($message) |
|
324 | { |
||
325 | 6 | $this->error = $message; |
|
326 | 1 | } |
|
327 | |||
328 | 3 | public static function config() |
|
329 | { |
||
332 | |||
333 | /** |
||
334 | * @deprecated 2.0 |
||
335 | */ |
||
336 | 1 | public function sendEmail($template, $subject, $copyToAdmin = true) |
|
341 | |||
342 | /** |
||
343 | * @deprecated 2.0 |
||
344 | */ |
||
345 | public function sendConfirmation() |
||
350 | |||
351 | /** |
||
352 | * @deprecated 2.0 |
||
353 | */ |
||
354 | public function sendReceipt() |
||
359 | |||
360 | /** |
||
361 | * @deprecated 2.0 |
||
362 | */ |
||
363 | public function sendStatusChange($title, $note = null) |
||
368 | } |
||
369 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.