Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
55 | class Reservation extends DataObject |
||
56 | { |
||
57 | const STATUS_CART = 'CART'; |
||
58 | const STATUS_PENDING = 'PENDING'; |
||
59 | const STATUS_PAID = 'PAID'; |
||
60 | const STATUS_CANCELED = 'CANCELED'; |
||
61 | |||
62 | /** |
||
63 | * Time to wait before deleting the discarded cart |
||
64 | * Give a string that is parsable by strtotime |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private static $delete_after = '+1 hour'; |
||
69 | |||
70 | /** |
||
71 | * The address to whom the ticket notifications are sent |
||
72 | * By default the admin email is used |
||
73 | * |
||
74 | * @config |
||
75 | * @var string |
||
76 | */ |
||
77 | private static $mail_sender; |
||
78 | |||
79 | /** |
||
80 | * The address from where the ticket mails are sent |
||
81 | * By default the admin email is used |
||
82 | * |
||
83 | * @config |
||
84 | * @var string |
||
85 | */ |
||
86 | private static $mail_receiver; |
||
87 | |||
88 | /** |
||
89 | * Send the receipt mail |
||
90 | * For organisations that only do free events you can configure |
||
91 | * this to hold back the receipt and only send the tickets |
||
92 | * |
||
93 | * @config |
||
94 | * @var bool |
||
95 | */ |
||
96 | private static $send_receipt_mail = true; |
||
97 | |||
98 | /** |
||
99 | * Send the admin notification |
||
100 | * |
||
101 | * @config |
||
102 | * @var bool |
||
103 | */ |
||
104 | private static $send_admin_notification = true; |
||
105 | |||
106 | private static $db = array( |
||
107 | 'Status' => 'Enum("CART,PENDING,PAID,CANCELED","CART")', |
||
108 | 'Title' => 'Varchar(255)', |
||
109 | 'Subtotal' => 'Currency', |
||
110 | 'Total' => 'Currency', |
||
111 | 'Gateway' => 'Varchar(255)', |
||
112 | 'Comments' => 'Text', |
||
113 | 'AgreeToTermsAndConditions' => 'Boolean', |
||
114 | 'ReservationCode' => 'Varchar(255)', |
||
115 | 'SentTickets' => 'Boolean', |
||
116 | 'SentReservation' => 'Boolean', |
||
117 | 'SentNotification' => 'Boolean', |
||
118 | ); |
||
119 | |||
120 | private static $default_sort = 'Created DESC'; |
||
121 | |||
122 | private static $has_one = array( |
||
123 | 'Event' => 'CalendarEvent', |
||
124 | 'MainContact' => 'Broarm\EventTickets\Attendee' |
||
125 | ); |
||
126 | |||
127 | private static $has_many = array( |
||
128 | 'Payments' => 'Payment', |
||
129 | 'Attendees' => 'Broarm\EventTickets\Attendee.Reservation' |
||
130 | ); |
||
131 | |||
132 | private static $belongs_many_many = array( |
||
133 | 'PriceModifiers' => 'Broarm\EventTickets\PriceModifier' |
||
134 | ); |
||
135 | |||
136 | private static $indexes = array( |
||
137 | 'ReservationCode' => 'unique("ReservationCode")' |
||
138 | ); |
||
139 | |||
140 | private static $summary_fields = array( |
||
|
|||
141 | 'ReservationCode' => 'Reservation', |
||
142 | 'Title' => 'Customer', |
||
143 | 'Total.Nice' => 'Total', |
||
144 | 'State' => 'Status', |
||
145 | 'GatewayNice' => 'Payment method', |
||
146 | 'Created.Nice' => 'Date' |
||
147 | ); |
||
148 | |||
149 | /** |
||
150 | * Actions usable on the cms detail view |
||
151 | * |
||
152 | * @var array |
||
153 | */ |
||
154 | private static $better_buttons_actions = array( |
||
155 | 'send' |
||
156 | ); |
||
157 | |||
158 | public function getCMSFields() |
||
179 | |||
180 | /** |
||
181 | * Add utility actions to the reservation details view |
||
182 | * |
||
183 | * @return FieldList |
||
184 | */ |
||
185 | public function getBetterButtonsActions() |
||
193 | |||
194 | /** |
||
195 | * Generate a reservation code if it does not yet exists |
||
196 | */ |
||
197 | public function onBeforeWrite() |
||
209 | |||
210 | /** |
||
211 | * After deleting a reservation, delete the attendees and files |
||
212 | */ |
||
213 | public function onBeforeDelete() |
||
230 | |||
231 | /** |
||
232 | * Gets a nice unnamespaced name |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function singular_name() |
||
241 | |||
242 | /** |
||
243 | * Returns the nice gateway title |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getGatewayNice() |
||
251 | |||
252 | /** |
||
253 | * Check if the cart is still in cart state and the delete_after time period has been exceeded |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function isDiscarded() |
||
262 | |||
263 | /** |
||
264 | * Get the full name |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function getName() |
||
277 | |||
278 | /** |
||
279 | * Return the translated state |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function getState() |
||
287 | |||
288 | /** |
||
289 | * Get a the translated map of available states |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | private function getStates() |
||
299 | |||
300 | /** |
||
301 | * Get the total by querying the sum of attendee ticket prices |
||
302 | * |
||
303 | * @return float |
||
304 | */ |
||
305 | public function calculateTotal() |
||
321 | |||
322 | /** |
||
323 | * Safely change to a state |
||
324 | * todo check if state direction matches |
||
325 | * |
||
326 | * @param $state |
||
327 | * |
||
328 | * @return boolean |
||
329 | */ |
||
330 | public function changeState($state) |
||
341 | |||
342 | /** |
||
343 | * Complete the reservation |
||
344 | * |
||
345 | * @throws \ValidationException |
||
346 | */ |
||
347 | public function complete() |
||
354 | |||
355 | /** |
||
356 | * Set the main contact id |
||
357 | * @param $id |
||
358 | * |
||
359 | * @throws \ValidationException |
||
360 | */ |
||
361 | public function setMainContact($id) |
||
366 | |||
367 | /** |
||
368 | * Create a reservation code |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | public function createReservationCode() |
||
376 | |||
377 | /** |
||
378 | * Generate the qr codes and downloadable pdf |
||
379 | */ |
||
380 | public function createFiles() |
||
388 | |||
389 | /** |
||
390 | * Send the reservation mail |
||
391 | * |
||
392 | * @return mixed |
||
393 | */ |
||
394 | public function sendReservation() |
||
422 | |||
423 | /** |
||
424 | * Send the reserved tickets |
||
425 | * |
||
426 | * @return mixed |
||
427 | */ |
||
428 | public function sendTickets() |
||
467 | |||
468 | |||
469 | /** |
||
470 | * Send a booking notification to the ticket mail sender or the site admin |
||
471 | * @return mixed |
||
472 | */ |
||
473 | public function sendNotification() |
||
501 | |||
502 | /** |
||
503 | * Create the files and send the reservation, notification and tickets |
||
504 | */ |
||
505 | public function send() |
||
513 | |||
514 | /** |
||
515 | * Get the download link |
||
516 | * |
||
517 | * @return string|null |
||
518 | */ |
||
519 | public function getDownloadLink() |
||
532 | |||
533 | public function canView($member = null) |
||
537 | |||
538 | public function canEdit($member = null) |
||
542 | |||
543 | public function canDelete($member = null) |
||
547 | |||
548 | public function canCreate($member = null) |
||
552 | } |
||
553 |