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) |
|
43 | { |
||
44 | 8 | $this->order = $order; |
|
45 | 8 | $this->notifier = OrderEmailNotifier::create($order); |
|
46 | 8 | } |
|
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) |
|
132 | |||
133 | /** |
||
134 | * Create a new payment for an order |
||
135 | */ |
||
136 | 2 | public function createPayment($gateway) |
|
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) |
|
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() |
|
309 | |||
310 | /** |
||
311 | * @return Order |
||
312 | */ |
||
313 | public function getOrder() |
||
317 | |||
318 | 1 | public function getError() |
|
322 | |||
323 | 1 | private function error($message) |
|
327 | |||
328 | 5 | public static function config() |
|
332 | } |
||
333 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.