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 EEM_Registration 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 EEM_Registration, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
14 | class EEM_Registration extends EEM_Soft_Delete_Base { |
||
15 | |||
16 | // private instance of the Registration object |
||
17 | protected static $_instance = NULL; |
||
18 | |||
19 | /** |
||
20 | * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
||
21 | * are status codes (eg, approved, cancelled, etc) |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $_reg_status; |
||
25 | |||
26 | /** |
||
27 | * The value of REG_count for a primary registrant |
||
28 | */ |
||
29 | const PRIMARY_REGISTRANT_COUNT = 1; |
||
30 | |||
31 | /** |
||
32 | * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration. |
||
33 | * Initial status for registrations when they are first created |
||
34 | * Payments are NOT allowed. |
||
35 | * Automatically toggled to whatever the default Event registration status is upon completion of the attendee information reg step |
||
36 | * NO space reserved. |
||
37 | * Registration is NOT active |
||
38 | */ |
||
39 | const status_id_incomplete = 'RIC'; |
||
40 | |||
41 | /** |
||
42 | * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration. |
||
43 | * Payments are NOT allowed. |
||
44 | * Event Admin must manually toggle STS_ID for it to change |
||
45 | * No space reserved. |
||
46 | * Registration is active |
||
47 | */ |
||
48 | const status_id_not_approved = 'RNA'; |
||
49 | |||
50 | /** |
||
51 | * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT . |
||
52 | * Payments are allowed. |
||
53 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
54 | * No space reserved. |
||
55 | * Registration is active |
||
56 | */ |
||
57 | const status_id_pending_payment = 'RPP'; |
||
58 | |||
59 | /** |
||
60 | * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST . |
||
61 | * Payments are allowed. |
||
62 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
63 | * No space reserved. |
||
64 | * Registration is active |
||
65 | */ |
||
66 | const status_id_wait_list = 'RWL'; |
||
67 | |||
68 | /** |
||
69 | * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration. |
||
70 | * the TXN may or may not be completed ( paid in full ) |
||
71 | * Payments are allowed. |
||
72 | * A space IS reserved. |
||
73 | * Registration is active |
||
74 | */ |
||
75 | const status_id_approved = 'RAP'; |
||
76 | |||
77 | /** |
||
78 | * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee. |
||
79 | * Payments are NOT allowed. |
||
80 | * NO space reserved. |
||
81 | * Registration is NOT active |
||
82 | */ |
||
83 | const status_id_cancelled = 'RCN'; |
||
84 | |||
85 | /** |
||
86 | * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin |
||
87 | * Payments are NOT allowed. |
||
88 | * No space reserved. |
||
89 | * Registration is NOT active |
||
90 | */ |
||
91 | const status_id_declined = 'RDC'; |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * private constructor to prevent direct creation |
||
97 | * |
||
98 | * @Constructor |
||
99 | * @access protected |
||
100 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved). |
||
101 | * Note this just sends the timezone info to the date time model field objects. Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option) |
||
102 | * @return \EEM_Registration |
||
|
|||
103 | */ |
||
104 | protected function __construct( $timezone = null ) { |
||
105 | $this->singular_item = __('Registration','event_espresso'); |
||
106 | $this->plural_item = __('Registrations','event_espresso'); |
||
107 | |||
108 | $this->_tables = array( |
||
109 | 'Registration'=>new EE_Primary_Table('esp_registration','REG_ID') |
||
110 | ); |
||
111 | $this->_fields = array( |
||
112 | 'Registration'=>array( |
||
113 | 'REG_ID'=>new EE_Primary_Key_Int_Field('REG_ID', __('Registration ID','event_espresso')), |
||
114 | 'EVT_ID'=>new EE_Foreign_Key_Int_Field('EVT_ID', __('Event ID','event_espresso'), false, 0, 'Event'), |
||
115 | 'ATT_ID'=>new EE_Foreign_Key_Int_Field('ATT_ID', __('Attendee ID','event_espresso'), false, 0, 'Attendee'), |
||
116 | 'TXN_ID'=>new EE_Foreign_Key_Int_Field('TXN_ID', __('Transaction ID','event_espresso'), false, 0, 'Transaction'), |
||
117 | 'TKT_ID'=>new EE_Foreign_Key_Int_Field('TKT_ID', __('Ticket ID','event_espresso'), false, 0, 'Ticket'), |
||
118 | 'STS_ID'=>new EE_Foreign_Key_String_Field('STS_ID', __('Status ID','event_espresso'), false, EEM_Registration::status_id_incomplete, 'Status'), |
||
119 | 'REG_date'=>new EE_Datetime_Field('REG_date', __('Time registration occurred','event_espresso'), false, time(), $timezone ), |
||
120 | 'REG_final_price'=>new EE_Money_Field('REG_final_price', __('Registration\'s share of the transaction total','event_espresso'), false, 0), |
||
121 | 'REG_paid'=>new EE_Money_Field('REG_paid', __('Amount paid to date towards registration','event_espresso'), false, 0), |
||
122 | 'REG_session'=>new EE_Plain_Text_Field('REG_session', __('Session ID of registration','event_espresso'), false, ''), |
||
123 | 'REG_code'=>new EE_Plain_Text_Field('REG_code', __('Unique Code for this registration','event_espresso'), false, ''), |
||
124 | 'REG_url_link'=>new EE_Plain_Text_Field('REG_url_link', __('String to be used in URL for identifying registration','event_espresso'), false, ''), |
||
125 | 'REG_count'=>new EE_Integer_Field('REG_count', __('Count of this registration in the group registration ','event_espresso'), true, 1), |
||
126 | 'REG_group_size'=>new EE_Integer_Field('REG_group_size', __('Number of registrations on this group','event_espresso'), false, 1), |
||
127 | 'REG_att_is_going'=>new EE_Boolean_Field('REG_att_is_going', __('Flag indicating the registrant plans on attending','event_espresso'), false, false), |
||
128 | 'REG_deleted' => new EE_Trashed_Flag_Field('REG_deleted', __('Flag indicating if registration has been archived or not.', 'event_espresso'), false, false ) |
||
129 | ) |
||
130 | ); |
||
131 | $this->_model_relations = array( |
||
132 | 'Event'=>new EE_Belongs_To_Relation(), |
||
133 | 'Attendee'=>new EE_Belongs_To_Relation(), |
||
134 | 'Transaction'=>new EE_Belongs_To_Relation(), |
||
135 | 'Ticket'=>new EE_Belongs_To_Relation(), |
||
136 | 'Status'=>new EE_Belongs_To_Relation(), |
||
137 | 'Answer'=>new EE_Has_Many_Relation(), |
||
138 | 'Checkin'=>new EE_Has_Many_Relation(), |
||
139 | 'Registration_Payment' => new EE_Has_Many_Relation(), |
||
140 | 'Payment'=>new EE_HABTM_Relation( 'Registration_Payment' ), |
||
141 | 'Message' => new EE_Has_Many_Any_Relation( false ) //allow deletes even if there are messages in the queue related |
||
142 | ); |
||
143 | $this->_model_chain_to_wp_user = 'Event'; |
||
144 | |||
145 | parent::__construct( $timezone ); |
||
146 | } |
||
147 | |||
148 | |||
149 | |||
150 | /** |
||
151 | * reg_statuses_that_allow_payment |
||
152 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
153 | * |
||
154 | * @access public |
||
155 | * @return array |
||
156 | */ |
||
157 | public static function reg_statuses_that_allow_payment() { |
||
158 | return apply_filters( |
||
159 | 'FHEE__EEM_Registration__reg_statuses_that_allow_payment', |
||
160 | array( |
||
161 | EEM_Registration::status_id_approved, |
||
162 | EEM_Registration::status_id_pending_payment, |
||
163 | EEM_Registration::status_id_wait_list, |
||
164 | ) |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | |||
169 | |||
170 | /** |
||
171 | * inactive_reg_statuses |
||
172 | * a filterable list of registration statuses that are not considered active |
||
173 | * |
||
174 | * @access public |
||
175 | * @return array |
||
176 | */ |
||
177 | public static function active_reg_statuses() { |
||
178 | return apply_filters( |
||
179 | 'FHEE__EEM_Registration__reg_statuses_that_allow_payment', |
||
180 | array( |
||
181 | EEM_Registration::status_id_approved, |
||
182 | EEM_Registration::status_id_pending_payment, |
||
183 | EEM_Registration::status_id_wait_list, |
||
184 | EEM_Registration::status_id_not_approved, |
||
185 | ) |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | |||
190 | |||
191 | /** |
||
192 | * inactive_reg_statuses |
||
193 | * a filterable list of registration statuses that are not considered active |
||
194 | * |
||
195 | * @access public |
||
196 | * @return array |
||
197 | */ |
||
198 | public static function inactive_reg_statuses() { |
||
199 | return apply_filters( |
||
200 | 'FHEE__EEM_Registration__reg_statuses_that_allow_payment', |
||
201 | array( |
||
202 | EEM_Registration::status_id_incomplete, |
||
203 | EEM_Registration::status_id_cancelled, |
||
204 | EEM_Registration::status_id_declined, |
||
205 | ) |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | |||
210 | |||
211 | /** |
||
212 | * get list of registration statuses |
||
213 | * |
||
214 | * |
||
215 | * @access public |
||
216 | * @param array $exclude The status ids to exclude from the returned results |
||
217 | * @param bool $translated If true will return the values as singular localized strings |
||
218 | * @return array |
||
219 | */ |
||
220 | public static function reg_status_array( $exclude = array(), $translated = FALSE ) { |
||
224 | |||
225 | |||
226 | |||
227 | /** |
||
228 | * get list of registration statuses |
||
229 | * @access private |
||
230 | * @param array $exclude |
||
231 | * @return array |
||
232 | */ |
||
233 | private function _get_registration_status_array( $exclude = array() ) { |
||
250 | |||
251 | |||
252 | |||
253 | |||
254 | /** |
||
255 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params and grouped by month and year. |
||
256 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
257 | * @return wpdb results array |
||
258 | */ |
||
259 | public function get_reg_months_and_years( $where_params ) { |
||
269 | |||
270 | |||
271 | |||
272 | |||
273 | /** |
||
274 | * retrieve ALL registrations for a particular Attendee from db |
||
275 | * @access public |
||
276 | * @param int $ATT_ID |
||
277 | * @return EE_Registration[] |
||
278 | */ |
||
279 | public function get_all_registrations_for_attendee( $ATT_ID = 0 ) { |
||
285 | |||
286 | |||
287 | |||
288 | /** |
||
289 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
290 | * be passed via a GET parameter. |
||
291 | * @param string $REG_url_link |
||
292 | * @return EE_Registration |
||
293 | */ |
||
294 | public function get_registration_for_reg_url_link($REG_url_link){ |
||
300 | |||
301 | |||
302 | |||
303 | |||
304 | /** |
||
305 | * retrieve registration for a specific transaction attendee from db |
||
306 | * |
||
307 | * @access public |
||
308 | * @param int $TXN_ID |
||
309 | * @param int $ATT_ID |
||
310 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the attendee number is required |
||
311 | * @return mixed array on success, FALSE on fail |
||
312 | */ |
||
313 | public function get_registration_for_transaction_attendee( $TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0 ) { |
||
322 | |||
323 | |||
324 | /** |
||
325 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
326 | * (doesn't utilize models because it's a fairly specialized query) |
||
327 | * @access public |
||
328 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
329 | * @return stdClass[] with properties regDate and total |
||
330 | */ |
||
331 | public function get_registrations_per_day_report( $period = '-1 month' ) { |
||
355 | |||
356 | |||
357 | /** |
||
358 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
359 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
360 | * |
||
361 | * @param string $period |
||
362 | * |
||
363 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
364 | * (i.e. RAP) |
||
365 | */ |
||
366 | View Code Duplication | public function get_registrations_per_day_and_per_status_report( $period = '-1 month' ) { |
|
418 | |||
419 | |||
420 | |||
421 | |||
422 | |||
423 | |||
424 | /** |
||
425 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
426 | * @access public |
||
427 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
428 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
429 | */ |
||
430 | public function get_registrations_per_event_report( $period = '-1 month' ) { |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Get the number of registrations per event grouped by registration status. |
||
457 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
458 | * |
||
459 | * @param string $period |
||
460 | * |
||
461 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
462 | * (i.e. RAP) |
||
463 | */ |
||
464 | View Code Duplication | public function get_registrations_per_event_and_per_status_report( $period = '-1 month' ) { |
|
513 | |||
514 | |||
515 | /** |
||
516 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
517 | * @param int $TXN_ID |
||
518 | * @return EE_Registration |
||
519 | */ |
||
520 | public function get_primary_registration_for_transaction_ID( $TXN_ID = 0){ |
||
526 | |||
527 | |||
528 | /** |
||
529 | * get_event_registration_count |
||
530 | * |
||
531 | * @access public |
||
532 | * @param int $EVT_ID |
||
533 | * @param boolean $for_incomplete_payments |
||
534 | * @return int |
||
535 | */ |
||
536 | public function get_event_registration_count ( $EVT_ID, $for_incomplete_payments = FALSE ) { |
||
545 | |||
546 | /** |
||
547 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
548 | * and so it uses wpdb directly |
||
549 | * @global WPDB $wpdb |
||
550 | * @return int number deleted |
||
551 | */ |
||
552 | public function delete_registrations_with_no_transaction() { |
||
558 | |||
559 | /** |
||
560 | * Count registrations checked into (or out of) a datetime |
||
561 | * |
||
562 | * @param int $DTT_ID datetime ID |
||
563 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
564 | * @return int |
||
565 | */ |
||
566 | public function count_registrations_checked_into_datetime( $DTT_ID, $checked_in = true) { |
||
589 | |||
590 | /** |
||
591 | * Count registrations checked into (or out of) an event. |
||
592 | * |
||
593 | * @param int $EVT_ID event ID |
||
594 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
595 | * @return int |
||
596 | */ |
||
597 | public function count_registrations_checked_into_event( $EVT_ID, $checked_in = true ) { |
||
622 | |||
623 | |||
624 | |||
625 | |||
626 | |||
627 | /** |
||
628 | * The purpose of this method is to retrieve an array of |
||
629 | * EE_Registration objects that represent the latest registration |
||
630 | * for each ATT_ID given in the function argument. |
||
631 | * |
||
632 | * @param array $attendee_ids |
||
633 | * @return EE_Registration[] |
||
634 | */ |
||
635 | public function get_latest_registration_for_each_of_given_contacts( $attendee_ids = array() ) { |
||
687 | |||
688 | |||
689 | |||
690 | } |
||
691 | // End of file EEM_Registration.model.php |
||
693 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.