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) |
|
33 | { |
||
34 | 6 | return Injector::inst()->create('OrderProcessor', $order); |
|
35 | } |
||
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()) |
|
66 | { |
||
67 | //create payment |
||
68 | 1 | $payment = $this->createPayment($gateway); |
|
69 | 1 | if (!$payment) { |
|
70 | //errors have been stored. |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | // Create a purchase service, and set the user-facing success URL for redirects |
||
75 | 1 | $service = PurchaseService::create($payment) |
|
76 | 2 | ->setReturnUrl($this->getReturnUrl()); |
|
77 | |||
78 | // Process payment, get the result back |
||
79 | 1 | $response = $service->purchase($this->getGatewayData($gatewaydata)); |
|
80 | 1 | if (GatewayInfo::is_manual($gateway)) { |
|
81 | //don't complete the payment at this stage, if payment is manual |
||
82 | $this->placeOrder(); |
||
83 | } |
||
84 | |||
85 | // For an OFFSITE payment, response will now contain a redirect |
||
86 | // For an ONSITE payment, ShopPayment::onCapture will have been called, which will have called completePayment |
||
87 | |||
88 | 1 | return $response; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Map shop data to omnipay fields |
||
93 | * |
||
94 | * @param array $customData Usually user submitted data. |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | 1 | protected function getGatewayData($customData) |
|
99 | { |
||
100 | 1 | $shipping = $this->order->getShippingAddress(); |
|
101 | 1 | $billing = $this->order->getBillingAddress(); |
|
102 | |||
103 | 1 | $numPayments = Payment::get() |
|
104 | 1 | ->filter(array('OrderID' => $this->order->ID)) |
|
105 | 1 | ->count() - 1; |
|
106 | |||
107 | 1 | $transactionId = $this->order->Reference . ($numPayments > 0 ? "-$numPayments" : ''); |
|
108 | |||
109 | 1 | return array_merge( |
|
110 | 1 | $customData, |
|
111 | array( |
||
112 | 1 | 'transactionId' => $transactionId, |
|
113 | 1 | 'firstName' => $this->order->FirstName, |
|
114 | 1 | 'lastName' => $this->order->Surname, |
|
115 | 1 | 'email' => $this->order->Email, |
|
116 | 1 | 'company' => $this->order->Company, |
|
117 | 1 | 'billingAddress1' => $billing->Address, |
|
118 | 1 | 'billingAddress2' => $billing->AddressLine2, |
|
119 | 1 | 'billingCity' => $billing->City, |
|
120 | 1 | 'billingPostcode' => $billing->PostalCode, |
|
121 | 1 | 'billingState' => $billing->State, |
|
122 | 1 | 'billingCountry' => $billing->Country, |
|
123 | 1 | 'billingPhone' => $billing->Phone, |
|
124 | 1 | 'shippingAddress1' => $shipping->Address, |
|
125 | 1 | 'shippingAddress2' => $shipping->AddressLine2, |
|
126 | 1 | 'shippingCity' => $shipping->City, |
|
127 | 1 | 'shippingPostcode' => $shipping->PostalCode, |
|
128 | 1 | 'shippingState' => $shipping->State, |
|
129 | 1 | 'shippingCountry' => $shipping->Country, |
|
130 | 1 | 'shippingPhone' => $shipping->Phone, |
|
131 | ) |
||
132 | 1 | ); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Create a new payment for an order |
||
137 | */ |
||
138 | 2 | public function createPayment($gateway) |
|
139 | { |
||
140 | 2 | if (!GatewayInfo::is_supported($gateway)) { |
|
141 | $this->error( |
||
142 | _t( |
||
143 | "PaymentProcessor.INVALID_GATEWAY", |
||
144 | "`{gateway}` isn't a valid payment gateway.", |
||
145 | 'gateway is the name of the payment gateway', |
||
146 | array('gateway' => $gateway) |
||
147 | ) |
||
148 | ); |
||
149 | return false; |
||
150 | } |
||
151 | 2 | if (!$this->order->canPay(Member::currentUser())) { |
|
152 | $this->error(_t("PaymentProcessor.CANTPAY", "Order can't be paid for.")); |
||
153 | return false; |
||
154 | } |
||
155 | 2 | $payment = Payment::create() |
|
156 | 2 | ->init($gateway, $this->order->TotalOutstanding(), ShopConfig::get_base_currency()); |
|
157 | 2 | $this->order->Payments()->add($payment); |
|
158 | 2 | return $payment; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Complete payment processing |
||
163 | * - send receipt |
||
164 | * - update order status accordingling |
||
165 | * - fire event hooks |
||
166 | */ |
||
167 | public function completePayment() |
||
168 | { |
||
169 | if (!$this->order->Paid) { |
||
170 | $this->order->extend('onPayment'); //a payment has been made |
||
171 | //place the order, if not already placed |
||
172 | if ($this->canPlace($this->order)) { |
||
173 | $this->placeOrder(); |
||
174 | } else { |
||
175 | if ($this->order->Locale) { |
||
176 | ShopTools::install_locale($this->order->Locale); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | if ( |
||
181 | // Standard order |
||
182 | ($this->order->GrandTotal() > 0 && $this->order->TotalOutstanding() <= 0) |
||
183 | // Zero-dollar order (e.g. paid with loyalty points) |
||
184 | || ($this->order->GrandTotal() == 0 && Order::config()->allow_zero_order_total) |
||
185 | ) { |
||
186 | //set order as paid |
||
187 | $this->order->Status = 'Paid'; |
||
188 | $this->order->Paid = SS_Datetime::now()->Rfc2822(); |
||
189 | $this->order->write(); |
||
190 | foreach ($this->order->Items() as $item) { |
||
191 | $item->onPayment(); |
||
192 | } |
||
193 | //all payment is settled |
||
194 | $this->order->extend('onPaid'); |
||
195 | } |
||
196 | if (!$this->order->ReceiptSent) { |
||
197 | $this->notifier->sendReceipt(); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Determine if an order can be placed. |
||
204 | * |
||
205 | * @param boolean $order |
||
206 | */ |
||
207 | 4 | public function canPlace(Order $order) |
|
208 | { |
||
209 | 4 | if (!$order) { |
|
210 | $this->error(_t("OrderProcessor.NULL", "Order does not exist.")); |
||
211 | return false; |
||
212 | } |
||
213 | //order status is applicable |
||
214 | 4 | if (!$order->IsCart()) { |
|
215 | 1 | $this->error(_t("OrderProcessor.NOTCART", "Order is not a cart.")); |
|
216 | 1 | return false; |
|
217 | } |
||
218 | //order has products |
||
219 | 3 | if ($order->Items()->Count() <= 0) { |
|
220 | $this->error(_t("OrderProcessor.NOITEMS", "Order has no items.")); |
||
221 | return false; |
||
222 | } |
||
223 | |||
224 | 3 | return true; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * Takes an order from being a cart to awaiting payment. |
||
229 | * |
||
230 | * @param Member $member - assign a member to the order |
||
231 | * |
||
232 | * @return boolean - success/failure |
||
233 | */ |
||
234 | 4 | public function placeOrder() |
|
235 | { |
||
236 | 4 | if (!$this->order) { |
|
237 | $this->error(_t("OrderProcessor.NULL", "A new order has not yet been started.")); |
||
238 | return false; |
||
239 | } |
||
240 | 4 | if (!$this->canPlace($this->order)) { //final cart validation |
|
241 | 1 | return false; |
|
242 | } |
||
243 | |||
244 | 3 | if ($this->order->Locale) { |
|
245 | 3 | ShopTools::install_locale($this->order->Locale); |
|
246 | 3 | } |
|
247 | |||
248 | //remove from session |
||
249 | 3 | $cart = ShoppingCart::curr(); |
|
250 | 3 | if ($cart && $cart->ID == $this->order->ID) { |
|
251 | 3 | ShoppingCart::singleton()->clear(); |
|
252 | 3 | } |
|
253 | //update status |
||
254 | 3 | if ($this->order->TotalOutstanding()) { |
|
255 | 3 | $this->order->Status = 'Unpaid'; |
|
256 | 3 | } else { |
|
257 | $this->order->Status = 'Paid'; |
||
258 | } |
||
259 | 3 | if (!$this->order->Placed) { |
|
260 | 3 | $this->order->Placed = SS_Datetime::now()->Rfc2822(); //record placed order datetime |
|
261 | 3 | if ($request = Controller::curr()->getRequest()) { |
|
262 | 3 | $this->order->IPAddress = $request->getIP(); //record client IP |
|
263 | 3 | } |
|
264 | 3 | } |
|
265 | //re-write all attributes and modifiers to make sure they are up-to-date before they can't be changed again |
||
266 | 3 | $items = $this->order->Items(); |
|
267 | 3 | if ($items->exists()) { |
|
268 | 3 | foreach ($items as $item) { |
|
269 | 3 | $item->onPlacement(); |
|
270 | 3 | $item->write(); |
|
271 | 3 | } |
|
272 | 3 | } |
|
273 | 3 | $modifiers = $this->order->Modifiers(); |
|
274 | 3 | if ($modifiers->exists()) { |
|
275 | foreach ($modifiers as $modifier) { |
||
276 | $modifier->write(); |
||
277 | } |
||
278 | } |
||
279 | //add member to order & customers group |
||
280 | 3 | if ($member = Member::currentUser()) { |
|
281 | 2 | if (!$this->order->MemberID) { |
|
282 | $this->order->MemberID = $member->ID; |
||
283 | } |
||
284 | 2 | $cgroup = ShopConfig::current()->CustomerGroup(); |
|
285 | 2 | if ($cgroup->exists()) { |
|
286 | $member->Groups()->add($cgroup); |
||
287 | } |
||
288 | 2 | } |
|
289 | //allow decorators to do stuff when order is saved. |
||
290 | 3 | $this->order->extend('onPlaceOrder'); |
|
291 | 3 | $this->order->write(); |
|
292 | |||
293 | //send confirmation if configured and receipt hasn't been sent |
||
294 | if ( |
||
295 | 3 | self::config()->send_confirmation |
|
296 | 3 | && !$this->order->ReceiptSent |
|
297 | 3 | ) { |
|
298 | $this->notifier->sendConfirmation(); |
||
299 | } |
||
300 | |||
301 | //notify admin, if configured |
||
302 | 3 | if (self::config()->send_admin_notification) { |
|
303 | $this->notifier->sendAdminNotification(); |
||
304 | } |
||
305 | |||
306 | // Save order reference to session |
||
307 | 3 | OrderManipulation::add_session_order($this->order); |
|
308 | |||
309 | 3 | return true; //report success |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * @return Order |
||
314 | */ |
||
315 | public function getOrder() |
||
316 | { |
||
317 | return $this->order; |
||
318 | } |
||
319 | |||
320 | 1 | public function getError() |
|
324 | |||
325 | 6 | private function error($message) |
|
329 | |||
330 | 3 | public static function config() |
|
331 | { |
||
332 | 3 | return new Config_ForClass("OrderProcessor"); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * @deprecated 2.0 |
||
337 | */ |
||
338 | public function sendEmail($template, $subject, $copyToAdmin = true) |
||
343 | |||
344 | /** |
||
345 | * @deprecated 2.0 |
||
346 | */ |
||
347 | public function sendConfirmation() |
||
352 | |||
353 | /** |
||
354 | * @deprecated 2.0 |
||
355 | */ |
||
356 | public function sendReceipt() |
||
361 | |||
362 | /** |
||
363 | * @deprecated 2.0 |
||
364 | */ |
||
365 | 2 | public function sendStatusChange($title, $note = null) |
|
370 | } |
||
371 |
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.