Complex classes like Reservation 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 Reservation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class Reservation extends DataObject |
||
53 | { |
||
54 | /** |
||
55 | * Time to wait before deleting the discarded cart |
||
56 | * Give a string that is parsable by strtotime |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private static $delete_after = '+1 hour'; |
||
61 | |||
62 | /** |
||
63 | * The address to whom the ticket notifications are sent |
||
64 | * By default the admin email is used |
||
65 | * |
||
66 | * @config |
||
67 | * @var string |
||
68 | */ |
||
69 | private static $mail_sender; |
||
70 | |||
71 | /** |
||
72 | * The address from where the ticket mails are sent |
||
73 | * By default the admin email is used |
||
74 | * |
||
75 | * @config |
||
76 | * @var string |
||
77 | */ |
||
78 | private static $mail_receiver; |
||
79 | |||
80 | private static $db = array( |
||
81 | 'Status' => 'Enum("CART,PENDING,PAID,CANCELED","CART")', |
||
82 | 'Title' => 'Varchar(255)', |
||
83 | 'Subtotal' => 'Currency', |
||
84 | 'Total' => 'Currency', |
||
85 | 'Gateway' => 'Varchar(255)', |
||
86 | 'Comments' => 'Text', |
||
87 | 'AgreeToTermsAndConditions' => 'Boolean', |
||
88 | 'ReservationCode' => 'Varchar(255)' |
||
89 | ); |
||
90 | |||
91 | private static $default_sort = 'Created DESC'; |
||
92 | |||
93 | private static $has_one = array( |
||
94 | 'Event' => 'CalendarEvent', |
||
95 | 'MainContact' => 'Broarm\EventTickets\Attendee' |
||
96 | ); |
||
97 | |||
98 | private static $has_many = array( |
||
99 | 'Payments' => 'Payment', |
||
100 | 'Attendees' => 'Broarm\EventTickets\Attendee.Reservation' |
||
101 | ); |
||
102 | |||
103 | private static $belongs_many_many = array( |
||
104 | 'PriceModifiers' => 'Broarm\EventTickets\PriceModifier' |
||
105 | ); |
||
106 | |||
107 | private static $indexes = array( |
||
108 | 'ReservationCode' => 'unique("ReservationCode")' |
||
109 | ); |
||
110 | |||
111 | private static $summary_fields = array( |
||
112 | 'ReservationCode' => 'Reservation', |
||
113 | 'Title' => 'Customer', |
||
114 | 'Total.Nice' => 'Total', |
||
115 | 'State' => 'Status', |
||
116 | 'GatewayNice' => 'Payment method', |
||
117 | 'Created.Nice' => 'Date' |
||
118 | ); |
||
119 | |||
120 | /** |
||
121 | * Actions usable on the cms detail view |
||
122 | * |
||
123 | * @var array |
||
124 | */ |
||
125 | private static $better_buttons_actions = array( |
||
126 | 'send' |
||
127 | ); |
||
128 | |||
129 | public function getCMSFields() |
||
149 | |||
150 | /** |
||
151 | * Add utility actions to the reservation details view |
||
152 | * |
||
153 | * @return FieldList |
||
154 | */ |
||
155 | public function getBetterButtonsActions() |
||
163 | |||
164 | /** |
||
165 | * Generate a reservation code if it does not yet exists |
||
166 | */ |
||
167 | public function onBeforeWrite() |
||
179 | |||
180 | /** |
||
181 | * After deleting a reservation, delete the attendees and files |
||
182 | */ |
||
183 | public function onBeforeDelete() |
||
200 | |||
201 | /** |
||
202 | * Gets a nice unnamespaced name |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function singular_name() |
||
211 | |||
212 | /** |
||
213 | * Returns the nice gateway title |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getGatewayNice() |
||
221 | |||
222 | /** |
||
223 | * Check if the cart is still in cart state and the delete_after time period has been exceeded |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function isDiscarded() |
||
232 | |||
233 | /** |
||
234 | * Get the full name |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getName() |
||
239 | { |
||
240 | /** @var Attendee $attendee */ |
||
241 | if (($mainContact = $this->MainContact()) && $mainContact->exists() && $name = $mainContact->getName()) { |
||
242 | return $name; |
||
243 | } else { |
||
244 | return 'new reservation'; |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return the translated state |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getState() |
||
257 | |||
258 | /** |
||
259 | * Get a the translated map of available states |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | private function getStates() |
||
269 | |||
270 | /** |
||
271 | * Get the total by querying the sum of attendee ticket prices |
||
272 | * |
||
273 | * @return float |
||
274 | */ |
||
275 | public function calculateTotal() |
||
291 | |||
292 | /** |
||
293 | * Safely change to a state |
||
294 | * todo check if state direction matches |
||
295 | * |
||
296 | * @param $state |
||
297 | * |
||
298 | * @return boolean |
||
299 | */ |
||
300 | public function changeState($state) |
||
311 | |||
312 | /** |
||
313 | * Set the main contact id |
||
314 | * |
||
315 | * @param $id |
||
316 | */ |
||
317 | public function setMainContact($id) |
||
322 | |||
323 | /** |
||
324 | * Create a reservation code |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function createReservationCode() |
||
332 | |||
333 | /** |
||
334 | * Create the folder for the qr code and ticket file |
||
335 | * |
||
336 | * @return Folder|DataObject|null |
||
337 | */ |
||
338 | public function fileFolder() |
||
342 | |||
343 | /** |
||
344 | * Generate the qr codes and downloadable pdf |
||
345 | */ |
||
346 | public function createFiles() |
||
355 | |||
356 | /** |
||
357 | * Send the reservation mail |
||
358 | */ |
||
359 | public function sendReservation() |
||
383 | |||
384 | /** |
||
385 | * Send the reserved tickets |
||
386 | */ |
||
387 | public function sendTickets() |
||
435 | |||
436 | |||
437 | /** |
||
438 | * Send a booking notification to the ticket mail sender or the site admin |
||
439 | */ |
||
440 | public function sendNotification() |
||
464 | |||
465 | /** |
||
466 | * Create the files and send the reservation, notification and tickets |
||
467 | */ |
||
468 | public function send() |
||
475 | |||
476 | /** |
||
477 | * Get the download link |
||
478 | * |
||
479 | * @return string|null |
||
480 | */ |
||
481 | public function getDownloadLink() |
||
495 | |||
496 | public function canView($member = null) |
||
500 | |||
501 | public function canEdit($member = null) |
||
505 | |||
506 | public function canDelete($member = null) |
||
510 | |||
511 | public function canCreate($member = null) |
||
515 | } |
||
516 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.