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'; |
||
1 ignored issue
–
show
|
|||
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; |
||
1 ignored issue
–
show
|
|||
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; |
||
1 ignored issue
–
show
|
|||
79 | |||
80 | private static $db = array( |
||
2 ignored issues
–
show
|
|||
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 $has_one = array( |
||
92 | 'Event' => 'CalendarEvent', |
||
93 | 'MainContact' => 'Broarm\EventTickets\Attendee' |
||
94 | ); |
||
95 | |||
96 | private static $has_many = array( |
||
2 ignored issues
–
show
|
|||
97 | 'Payments' => 'Payment', |
||
98 | 'Attendees' => 'Broarm\EventTickets\Attendee.Reservation' |
||
99 | ); |
||
100 | |||
101 | private static $belongs_many_many = array( |
||
2 ignored issues
–
show
|
|||
102 | 'PriceModifiers' => 'Broarm\EventTickets\PriceModifier' |
||
103 | ); |
||
104 | |||
105 | private static $indexes = array( |
||
2 ignored issues
–
show
|
|||
106 | 'ReservationCode' => 'unique("ReservationCode")' |
||
107 | ); |
||
108 | |||
109 | private static $summary_fields = array( |
||
2 ignored issues
–
show
|
|||
110 | 'ReservationCode' => 'Reservation', |
||
111 | 'Title' => 'Customer', |
||
112 | 'Total.Nice' => 'Total', |
||
113 | 'State' => 'Status', |
||
114 | 'GatewayNice' => 'Payment method', |
||
115 | 'Created.Nice' => 'Date' |
||
116 | ); |
||
117 | |||
118 | /** |
||
119 | * Actions usable on the cms detail view |
||
120 | * |
||
121 | * @var array |
||
122 | */ |
||
123 | private static $better_buttons_actions = array( |
||
1 ignored issue
–
show
|
|||
124 | 'send' |
||
125 | ); |
||
126 | |||
127 | public function getCMSFields() |
||
147 | |||
148 | /** |
||
149 | * Add utility actions to the reservation details view |
||
150 | * |
||
151 | * @return FieldList |
||
152 | */ |
||
153 | public function getBetterButtonsActions() |
||
161 | |||
162 | /** |
||
163 | * Generate a reservation code if it does not yet exists |
||
164 | */ |
||
165 | public function onBeforeWrite() |
||
177 | |||
178 | /** |
||
179 | * After deleting a reservation, delete the attendees and files |
||
180 | */ |
||
181 | public function onBeforeDelete() |
||
182 | { |
||
183 | // If a reservation is deleted remove the names from the guest list |
||
184 | foreach ($this->Attendees() as $attendee) { |
||
185 | /** @var Attendee $attendee */ |
||
186 | if ($attendee->exists()) { |
||
187 | $attendee->delete(); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // Remove the folder |
||
192 | if (($folder = Folder::get()->find('Name', $this->ReservationCode)) && $folder->exists() && $folder->isEmpty()) { |
||
193 | $folder->delete(); |
||
194 | } |
||
195 | |||
196 | parent::onBeforeDelete(); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Gets a nice unnamespaced name |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | public function singular_name() |
||
209 | |||
210 | /** |
||
211 | * Returns the nice gateway title |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getGatewayNice() |
||
219 | |||
220 | /** |
||
221 | * Check if the cart is still in cart state and the delete_after time period has been exceeded |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isDiscarded() |
||
230 | |||
231 | /** |
||
232 | * Get the full name |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getName() |
||
245 | |||
246 | /** |
||
247 | * Return the translated state |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getState() |
||
255 | |||
256 | /** |
||
257 | * Get a the translated map of available states |
||
258 | * |
||
259 | * @return array |
||
260 | */ |
||
261 | private function getStates() |
||
267 | |||
268 | /** |
||
269 | * Get the total by querying the sum of attendee ticket prices |
||
270 | * |
||
271 | * @return float |
||
272 | */ |
||
273 | public function calculateTotal() |
||
289 | |||
290 | /** |
||
291 | * Safely change to a state |
||
292 | * todo check if state direction matches |
||
293 | * |
||
294 | * @param $state |
||
295 | * |
||
296 | * @return boolean |
||
297 | */ |
||
298 | public function changeState($state) |
||
309 | |||
310 | /** |
||
311 | * Set the main contact id |
||
312 | * |
||
313 | * @param $id |
||
314 | */ |
||
315 | public function setMainContact($id) |
||
320 | |||
321 | /** |
||
322 | * Create a reservation code |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function createReservationCode() |
||
330 | |||
331 | /** |
||
332 | * Create the folder for the qr code and ticket file |
||
333 | * |
||
334 | * @return Folder|DataObject|null |
||
335 | */ |
||
336 | public function fileFolder() |
||
340 | |||
341 | /** |
||
342 | * Generate the qr codes and downloadable pdf |
||
343 | */ |
||
344 | public function createFiles() |
||
353 | |||
354 | /** |
||
355 | * Send the reservation mail |
||
356 | */ |
||
357 | public function sendReservation() |
||
381 | |||
382 | /** |
||
383 | * Send the reserved tickets |
||
384 | */ |
||
385 | public function sendTickets() |
||
433 | |||
434 | |||
435 | /** |
||
436 | * Send a booking notification to the ticket mail sender or the site admin |
||
437 | */ |
||
438 | public function sendNotification() |
||
462 | |||
463 | /** |
||
464 | * Create the files and send the reservation, notification and tickets |
||
465 | */ |
||
466 | public function send() |
||
473 | |||
474 | /** |
||
475 | * Get the download link |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | public function getDownloadLink() |
||
483 | |||
484 | public function canView($member = null) |
||
488 | |||
489 | public function canEdit($member = null) |
||
493 | |||
494 | public function canDelete($member = null) |
||
498 | |||
499 | public function canCreate($member = null) |
||
503 | } |
||
504 |
This check marks private properties in classes that are never used. Those properties can be removed.