Complex classes like ProcessTicketSelector 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 ProcessTicketSelector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ProcessTicketSelector |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var EE_Cart $cart |
||
| 39 | */ |
||
| 40 | private $cart; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var EE_Core_Config $core_config |
||
| 44 | */ |
||
| 45 | private $core_config; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Request $request |
||
| 49 | */ |
||
| 50 | private $request; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var EE_Session $session |
||
| 54 | */ |
||
| 55 | private $session; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var EEM_Ticket $ticket_model |
||
| 59 | */ |
||
| 60 | private $ticket_model; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var TicketDatetimeAvailabilityTracker $tracker |
||
| 64 | */ |
||
| 65 | private $tracker; |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * ProcessTicketSelector constructor. |
||
| 70 | * NOTE: PLZ use the Loader to instantiate this class if need be |
||
| 71 | * so that all dependencies get injected correctly (which will happen automatically) |
||
| 72 | * Null values for parameters are only for backwards compatibility but will be removed later on. |
||
| 73 | * |
||
| 74 | * @param EE_Core_Config $core_config |
||
| 75 | * @param Request $request |
||
| 76 | * @param EE_Session $session |
||
| 77 | * @param EEM_Ticket $ticket_model |
||
| 78 | * @param TicketDatetimeAvailabilityTracker $tracker |
||
| 79 | * @throws InvalidArgumentException |
||
| 80 | * @throws InvalidDataTypeException |
||
| 81 | * @throws InvalidInterfaceException |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * cancelTicketSelections |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | * @throws EE_Error |
||
| 115 | * @throws InvalidArgumentException |
||
| 116 | * @throws InvalidInterfaceException |
||
| 117 | * @throws InvalidDataTypeException |
||
| 118 | */ |
||
| 119 | public function cancelTicketSelections() |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * processTicketSelectorNonce |
||
| 142 | * |
||
| 143 | * @param string $nonce_name |
||
| 144 | * @param string $id |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | private function processTicketSelectorNonce($nonce_name, $id = '') |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * process_ticket_selections |
||
| 180 | * |
||
| 181 | * @return array|bool |
||
| 182 | * @throws EE_Error |
||
| 183 | * @throws InvalidArgumentException |
||
| 184 | * @throws InvalidDataTypeException |
||
| 185 | * @throws InvalidInterfaceException |
||
| 186 | */ |
||
| 187 | public function processTicketSelections() |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @return int |
||
| 237 | */ |
||
| 238 | private function getEventId() |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * validate_post_data |
||
| 263 | * |
||
| 264 | * @param int $id |
||
| 265 | * @return array|FALSE |
||
| 266 | */ |
||
| 267 | private function validatePostData($id = 0) |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * @param array $valid |
||
| 370 | */ |
||
| 371 | private function maxAttendeesViolation(array $valid) |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * @param array $valid |
||
| 399 | * @return int|TRUE |
||
| 400 | * @throws EE_Error |
||
| 401 | * @throws InvalidArgumentException |
||
| 402 | * @throws InvalidDataTypeException |
||
| 403 | * @throws InvalidInterfaceException |
||
| 404 | */ |
||
| 405 | private function addTicketsToCart(array $valid) |
||
| 406 | { |
||
| 407 | $tickets_added = 0; |
||
| 408 | $tickets_selected = false; |
||
| 409 | if($valid['total_tickets'] > 0){ |
||
| 410 | // load cart using factory because we don't want to do so until actually needed |
||
| 411 | $this->cart = CartFactory::getCart(); |
||
| 412 | // cycle thru the number of data rows sent from the event listing |
||
| 413 | for ($x = 0; $x < $valid['rows']; $x++) { |
||
| 414 | // does this row actually contain a ticket quantity? |
||
| 415 | if (isset($valid['qty'][ $x ]) && $valid['qty'][ $x ] > 0) { |
||
| 416 | // YES we have a ticket quantity |
||
| 417 | $tickets_selected = true; |
||
| 418 | $valid_ticket = false; |
||
| 419 | // \EEH_Debug_Tools::printr( |
||
| 420 | // $valid['ticket_id'][ $x ], |
||
| 421 | // '$valid[\'ticket_id\'][ $x ]', |
||
| 422 | // __FILE__, __LINE__ |
||
| 423 | // ); |
||
| 424 | if (isset($valid['ticket_id'][ $x ])) { |
||
| 425 | // get ticket via the ticket id we put in the form |
||
| 426 | $ticket = $this->ticket_model->get_one_by_ID($valid['ticket_id'][ $x ]); |
||
| 427 | if ($ticket instanceof EE_Ticket) { |
||
| 428 | $valid_ticket = true; |
||
| 429 | $tickets_added += $this->addTicketToCart( |
||
| 430 | $ticket, |
||
| 431 | $valid['qty'][ $x ] |
||
| 432 | ); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | if ($valid_ticket !== true) { |
||
| 436 | // nothing added to cart retrieved |
||
| 437 | EE_Error::add_error( |
||
| 438 | sprintf( |
||
| 439 | esc_html__( |
||
| 440 | 'A valid ticket could not be retrieved for the event.%sPlease click the back button on your browser and try again.', |
||
| 441 | 'event_espresso' |
||
| 442 | ), |
||
| 443 | '<br/>' |
||
| 444 | ), |
||
| 445 | __FILE__, __FUNCTION__, __LINE__ |
||
| 446 | ); |
||
| 447 | } |
||
| 448 | if (EE_Error::has_error()) { |
||
| 449 | break; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | } |
||
| 453 | } |
||
| 454 | do_action( |
||
| 455 | 'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart', |
||
| 456 | $this->cart, |
||
| 457 | $this |
||
| 458 | ); |
||
| 459 | if (! apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', $tickets_selected)) { |
||
| 460 | // no ticket quantities were selected |
||
| 461 | EE_Error::add_error( |
||
| 462 | esc_html__('You need to select a ticket quantity before you can proceed.', 'event_espresso'), |
||
| 463 | __FILE__, __FUNCTION__, __LINE__ |
||
| 464 | ); |
||
| 465 | } |
||
| 466 | return $tickets_added; |
||
| 467 | } |
||
| 468 | |||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * adds a ticket to the cart |
||
| 473 | * |
||
| 474 | * @param EE_Ticket $ticket |
||
| 475 | * @param int $qty |
||
| 476 | * @return TRUE on success, FALSE on fail |
||
| 477 | * @throws InvalidArgumentException |
||
| 478 | * @throws InvalidInterfaceException |
||
| 479 | * @throws InvalidDataTypeException |
||
| 480 | * @throws EE_Error |
||
| 481 | */ |
||
| 482 | private function addTicketToCart(EE_Ticket $ticket, $qty = 1) |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * @param $tickets_added |
||
| 515 | * @return bool |
||
| 516 | * @throws InvalidInterfaceException |
||
| 517 | * @throws InvalidDataTypeException |
||
| 518 | * @throws EE_Error |
||
| 519 | * @throws InvalidArgumentException |
||
| 520 | */ |
||
| 521 | private function processSuccessfulCart($tickets_added) |
||
| 557 | } |
||
| 558 | // End of file ProcessTicketSelector.php |
||
| 560 |