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 |
||
| 51 | class Reservation extends DataObject |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * Time to wait before deleting the discarded cart |
||
| 55 | * Give a string that is parsable by strtotime |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private static $delete_after = '+1 day'; |
||
|
1 ignored issue
–
show
|
|||
| 60 | |||
| 61 | /** |
||
| 62 | * The address to whom the ticket notifications are sent |
||
| 63 | * By default the admin email is used |
||
| 64 | * |
||
| 65 | * @config |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private static $mail_sender; |
||
|
1 ignored issue
–
show
|
|||
| 69 | |||
| 70 | /** |
||
| 71 | * The address from where the ticket mails are sent |
||
| 72 | * By default the admin email is used |
||
| 73 | * |
||
| 74 | * @config |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private static $mail_receiver; |
||
|
1 ignored issue
–
show
|
|||
| 78 | |||
| 79 | private static $db = array( |
||
|
2 ignored issues
–
show
|
|||
| 80 | 'Status' => 'Enum("CART,PENDING,PAID,CANCELED","CART")', |
||
| 81 | 'Title' => 'Varchar(255)', |
||
| 82 | 'Subtotal' => 'Currency', |
||
| 83 | 'Total' => 'Currency', |
||
| 84 | 'Gateway' => 'Varchar(255)', |
||
| 85 | 'Comments' => 'Text', |
||
| 86 | 'ReservationCode' => 'Varchar(255)' |
||
| 87 | ); |
||
| 88 | |||
| 89 | private static $has_one = array( |
||
| 90 | 'Event' => 'CalendarEvent', |
||
| 91 | 'MainContact' => 'Broarm\EventTickets\Attendee' |
||
| 92 | ); |
||
| 93 | |||
| 94 | private static $has_many = array( |
||
|
2 ignored issues
–
show
|
|||
| 95 | 'Payments' => 'Payment', |
||
| 96 | 'Attendees' => 'Broarm\EventTickets\Attendee.Reservation' |
||
| 97 | ); |
||
| 98 | |||
| 99 | private static $belongs_many_many = array( |
||
|
2 ignored issues
–
show
|
|||
| 100 | 'PriceModifiers' => 'Broarm\EventTickets\PriceModifier' |
||
| 101 | ); |
||
| 102 | |||
| 103 | private static $indexes = array( |
||
|
2 ignored issues
–
show
|
|||
| 104 | 'ReservationCode' => 'unique("ReservationCode")' |
||
| 105 | ); |
||
| 106 | |||
| 107 | private static $summary_fields = array( |
||
|
2 ignored issues
–
show
|
|||
| 108 | 'ReservationCode' => 'Reservation', |
||
| 109 | 'Title' => 'Customer', |
||
| 110 | 'Total.Nice' => 'Total', |
||
| 111 | 'State' => 'Status', |
||
| 112 | 'GatewayNice' => 'Payment method', |
||
| 113 | 'Created.Nice' => 'Date' |
||
| 114 | ); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Actions usable on the cms detail view |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private static $better_buttons_actions = array( |
||
|
1 ignored issue
–
show
|
|||
| 122 | 'send' |
||
| 123 | ); |
||
| 124 | |||
| 125 | public function getCMSFields() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Add utility actions to the reservation details view |
||
| 147 | * |
||
| 148 | * @return FieldList |
||
| 149 | */ |
||
| 150 | public function getBetterButtonsActions() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Generate a reservation code if it does not yet exists |
||
| 161 | */ |
||
| 162 | public function onBeforeWrite() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * After deleting a reservation, delete the attendees and files |
||
| 177 | */ |
||
| 178 | public function onBeforeDelete() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets a nice unnamespaced name |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public function singular_name() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the nice gateway title |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getGatewayNice() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Check if the cart is still in cart state and the delete_after time period has been exceeded |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function isDiscarded() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the full name |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getName() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return the translated state |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getState() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get a the translated map of available states |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | private function getStates() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the total by querying the sum of attendee ticket prices |
||
| 267 | * |
||
| 268 | * @return float |
||
| 269 | */ |
||
| 270 | public function calculateTotal() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Safely change to a state |
||
| 289 | * todo check if state direction matches |
||
| 290 | * |
||
| 291 | * @param $state |
||
| 292 | * |
||
| 293 | * @return boolean |
||
| 294 | */ |
||
| 295 | public function changeState($state) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set the main contact id |
||
| 309 | * |
||
| 310 | * @param $id |
||
| 311 | */ |
||
| 312 | public function setMainContact($id) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Create a reservation code |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function createReservationCode() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Create the folder for the qr code and ticket file |
||
| 330 | * |
||
| 331 | * @return Folder|DataObject|null |
||
| 332 | */ |
||
| 333 | public function fileFolder() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Generate the qr codes and downloadable pdf |
||
| 340 | */ |
||
| 341 | public function createFiles() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Send the reservation mail |
||
| 353 | */ |
||
| 354 | public function sendReservation() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Send the reserved tickets |
||
| 381 | */ |
||
| 382 | public function sendTickets() |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Send a booking notification to the ticket mail sender or the site admin |
||
| 434 | */ |
||
| 435 | public function sendNotification() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Create the files and send the reservation, notification and tickets |
||
| 462 | */ |
||
| 463 | public function send() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the download link |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function getDownloadLink() |
||
| 480 | |||
| 481 | public function canView($member = null) |
||
| 485 | |||
| 486 | public function canEdit($member = null) |
||
| 490 | |||
| 491 | public function canDelete($member = null) |
||
| 495 | |||
| 496 | public function canCreate($member = null) |
||
| 500 | } |
||
| 501 |
This check marks private properties in classes that are never used. Those properties can be removed.