Complex classes like ActiveRecordController 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 ActiveRecordController, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 64 | class ActiveRecordController extends Controller implements ControllerInterface | ||
| 65 | { | ||
| 66 | /** | ||
| 67 | * The start number for list pageination. | ||
| 68 | * | ||
| 69 | * @var int | ||
| 70 | * | ||
| 71 | * @since 2.0 | ||
| 72 | */ | ||
| 73 | protected $start = 0; | ||
| 74 | |||
| 75 | /** | ||
| 76 | * The amount of records to return during pageination. | ||
| 77 | * | ||
| 78 | * @var int | ||
| 79 | * | ||
| 80 | * @since 2.0 | ||
| 81 | */ | ||
| 82 | protected $limit; | ||
| 83 | |||
| 84 | /** | ||
| 85 | * The count of the records of this type in the database (used during pagination). | ||
| 86 | * | ||
| 87 | * @var int | ||
| 88 | * | ||
| 89 | * @since 2.0 | ||
| 90 | */ | ||
| 91 | protected $recordCount = 0; | ||
| 92 | |||
| 93 | /** | ||
| 94 | * The field name to sort the list by (optional, default is ID). | ||
| 95 | * | ||
| 96 | * @var string | ||
| 97 | * | ||
| 98 | * @since 2.0 | ||
| 99 | */ | ||
| 100 | protected $sort; | ||
| 101 | |||
| 102 | /** | ||
| 103 | * The order to sort the list by (optional, should be ASC or DESC, default is ASC). | ||
| 104 | * | ||
| 105 | * @var string | ||
| 106 | * | ||
| 107 | * @since 2.0 | ||
| 108 | */ | ||
| 109 | protected $order; | ||
| 110 | |||
| 111 | /** | ||
| 112 | * The name of the Record field to filter the list by (optional). | ||
| 113 | * | ||
| 114 | * @var string | ||
| 115 | * | ||
| 116 | * @since 2.0 | ||
| 117 | */ | ||
| 118 | protected $filterField; | ||
| 119 | |||
| 120 | /** | ||
| 121 | * The value of the filterField to filter by (optional). | ||
| 122 | * | ||
| 123 | * @var string | ||
| 124 | * | ||
| 125 | * @since 2.0 | ||
| 126 | */ | ||
| 127 | protected $filterValue; | ||
| 128 | |||
| 129 | /** | ||
| 130 | * Trace logger. | ||
| 131 | * | ||
| 132 | * @var \Alpha\Util\Logging\Logger | ||
| 133 | * | ||
| 134 | * @since 2.0 | ||
| 135 | */ | ||
| 136 | private static $logger = null; | ||
|  | |||
| 137 | |||
| 138 | /** | ||
| 139 | * Constructor to set up the object. | ||
| 140 | * | ||
| 141 | * @param string $visibility The name of the rights group that can access this controller. | ||
| 142 | * | ||
| 143 | * @since 1.0 | ||
| 144 | */ | ||
| 145 | public function __construct($visibility = 'Admin') | ||
| 155 | |||
| 156 | /** | ||
| 157 | * Handle GET requests. | ||
| 158 | * | ||
| 159 | * @param \Alpha\Util\Http\Request $request | ||
| 160 | * | ||
| 161 | * @throws \Alpha\Exception\ResourceNotFoundException | ||
| 162 | * @throws \Alpha\Exception\IllegalArguementException | ||
| 163 | * | ||
| 164 | * @return \Alpha\Util\Http\Response | ||
| 165 | * | ||
| 166 | * @since 2.0 | ||
| 167 | */ | ||
| 168 | public function doGET($request) | ||
| 169 |     { | ||
| 170 |         self::$logger->debug('>>doGET(request=['.var_export($request, true).'])'); | ||
| 171 | |||
| 172 | $params = $request->getParams(); | ||
| 173 | $accept = $request->getAccept(); | ||
| 174 | |||
| 175 | $body = ''; | ||
| 176 | |||
| 177 |         try { | ||
| 178 | // get a single record | ||
| 179 |             if (isset($params['ActiveRecordType']) && isset($params['ActiveRecordID'])) { | ||
| 180 | $body .= $this->renderRecord($params, $accept); | ||
| 181 |             } elseif (isset($params['ActiveRecordType']) && isset($params['start'])) { | ||
| 182 | // list all records of this type | ||
| 183 | $body .= $this->renderRecords($params, $accept); | ||
| 184 |             } elseif (isset($params['ActiveRecordType'])) { | ||
| 185 | // create a new record of this type | ||
| 186 | $ActiveRecordType = urldecode($params['ActiveRecordType']); | ||
| 187 | |||
| 188 |                 if (class_exists($ActiveRecordType)) { | ||
| 189 | $record = new $ActiveRecordType(); | ||
| 190 |                 } else { | ||
| 191 |                     throw new IllegalArguementException('No ActiveRecord available to create!'); | ||
| 192 | } | ||
| 193 | |||
| 194 | // set up the title and meta details | ||
| 195 |                 if (!isset($this->title)) { | ||
| 196 |                     $this->setTitle('Create a new '.$record->getFriendlyClassName()); | ||
| 197 | } | ||
| 198 |                 if (!isset($this->description)) { | ||
| 199 |                     $this->setDescription('Create a new '.$record->getFriendlyClassName().'.'); | ||
| 200 | } | ||
| 201 |                 if (!isset($this->keywords)) { | ||
| 202 |                     $this->setKeywords('create,new,'.$record->getFriendlyClassName()); | ||
| 203 | } | ||
| 204 | |||
| 205 | $view = View::getInstance($record, false, $accept); | ||
| 206 | |||
| 207 | $body .= View::displayPageHead($this); | ||
| 208 |                 $fields = array('formAction' => $this->request->getURI()); | ||
| 209 | $body .= $view->createView($fields); | ||
| 210 |             } else { | ||
| 211 |                 throw new IllegalArguementException('No ActiveRecord available to display!'); | ||
| 212 | } | ||
| 213 |         } catch (IllegalArguementException $e) { | ||
| 214 | self::$logger->warn($e->getMessage()); | ||
| 215 |             throw new ResourceNotFoundException('The record that you have requested cannot be found!'); | ||
| 216 |         } catch (RecordNotFoundException $e) { | ||
| 217 | self::$logger->warn($e->getMessage()); | ||
| 218 |             throw new ResourceNotFoundException('The record that you have requested cannot be found!'); | ||
| 219 | } | ||
| 220 | |||
| 221 | $body .= View::displayPageFoot($this); | ||
| 222 | |||
| 223 |         self::$logger->debug('<<doGET'); | ||
| 224 | |||
| 225 |         return new Response(200, $body, array('Content-Type' => ($accept == 'application/json' ? 'application/json' : 'text/html'))); | ||
| 226 | } | ||
| 227 | |||
| 228 | /** | ||
| 229 | * Method to handle POST requests. | ||
| 230 | * | ||
| 231 | * @param \Alpha\Util\Http\Request $request | ||
| 232 | * | ||
| 233 | * @throws \Alpha\Exception\IllegalArguementException | ||
| 234 | * @throws \Alpha\Exception\SecurityException | ||
| 235 | * | ||
| 236 | * @return \Alpha\Util\Http\Response | ||
| 237 | * | ||
| 238 | * @since 2.0 | ||
| 239 | */ | ||
| 240 | public function doPOST($request) | ||
| 241 |     { | ||
| 242 |         self::$logger->debug('>>doDPOST(request=['.var_export($request, true).'])'); | ||
| 243 | |||
| 244 | $config = ConfigProvider::getInstance(); | ||
| 245 | |||
| 246 | $params = $request->getParams(); | ||
| 247 | $accept = $request->getAccept(); | ||
| 248 | |||
| 249 |         try { | ||
| 250 |             if (isset($params['ActiveRecordType'])) { | ||
| 251 | $ActiveRecordType = urldecode($params['ActiveRecordType']); | ||
| 252 |             } else { | ||
| 253 |                 throw new IllegalArguementException('No ActiveRecord available to create!'); | ||
| 254 | } | ||
| 255 | |||
| 256 |             if (class_exists($ActiveRecordType)) { | ||
| 257 | $record = new $ActiveRecordType(); | ||
| 258 |             } else { | ||
| 259 |                 throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to create!'); | ||
| 260 | } | ||
| 261 | |||
| 262 | // check the hidden security fields before accepting the form POST data | ||
| 263 |             if (!$this->checkSecurityFields()) { | ||
| 264 |                 throw new SecurityException('This page cannot accept post data from remote servers!'); | ||
| 265 | } | ||
| 266 | |||
| 267 | $record->populateFromArray($params); | ||
| 268 | |||
| 269 |             try { | ||
| 270 | $record->save(); | ||
| 271 |             } catch (ValidationException $e) { | ||
| 272 | self::$logger->warn($e->getMessage()); | ||
| 273 | $this->setStatusMessage(View::displayErrorMessage($e->getMessage())); | ||
| 274 | } | ||
| 275 | |||
| 276 |             self::$logger->action('Created new '.$ActiveRecordType.' instance with ID '.$record->getID()); | ||
| 277 | |||
| 278 |             if (isset($params['statusMessage'])) { | ||
| 279 | $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage'])); | ||
| 280 |             } else { | ||
| 281 |                 $this->setStatusMessage(View::displayUpdateMessage('Created')); | ||
| 282 | } | ||
| 283 | |||
| 284 | ActiveRecord::disconnect(); | ||
| 285 | |||
| 286 |             if ($accept == 'application/json') { | ||
| 287 | $view = View::getInstance($record, false, $accept); | ||
| 288 | $body = $view->detailedView(); | ||
| 289 | $response = new Response(201); | ||
| 290 |                 $response->setHeader('Content-Type', 'application/json'); | ||
| 291 |                 $response->setHeader('Location', $config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getID()); | ||
| 292 | $response->setBody($body); | ||
| 293 | |||
| 294 |                 self::$logger->debug('<<doPOST'); | ||
| 295 | |||
| 296 | return $response; | ||
| 297 |             } else { | ||
| 298 | $response = new Response(301); | ||
| 299 | |||
| 300 |                 if ($this->getNextJob() != '') { | ||
| 301 | $response->redirect($this->getNextJob()); | ||
| 302 |                 } else { | ||
| 303 |                     if ($this->request->isSecureURI()) { | ||
| 304 |                         $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.$ActiveRecordType.'&ActiveRecordID='.$record->getID())); | ||
| 305 |                     } else { | ||
| 306 |                         $response->redirect($config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getID()); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 |                 self::$logger->debug('<<doPOST'); | ||
| 311 | |||
| 312 | return $response; | ||
| 313 | } | ||
| 314 |         } catch (SecurityException $e) { | ||
| 315 | self::$logger->warn($e->getMessage()); | ||
| 316 | throw new ResourceNotAllowedException($e->getMessage()); | ||
| 317 |         } catch (IllegalArguementException $e) { | ||
| 318 | self::$logger->warn($e->getMessage()); | ||
| 319 |             throw new ResourceNotFoundException('The record that you have requested cannot be found!'); | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | /** | ||
| 324 | * Method to handle PUT requests. | ||
| 325 | * | ||
| 326 | * @param \Alpha\Util\Http\Request $request | ||
| 327 | * | ||
| 328 | * @throws \Alpha\Exception\IllegalArguementException | ||
| 329 | * @throws \Alpha\Exception\SecurityException | ||
| 330 | * | ||
| 331 | * @return \Alpha\Util\Http\Response | ||
| 332 | * | ||
| 333 | * @since 2.0 | ||
| 334 | */ | ||
| 335 | public function doPUT($request) | ||
| 417 | |||
| 418 | /** | ||
| 419 | * Method to handle DELETE requests. | ||
| 420 | * | ||
| 421 | * @param \Alpha\Util\Http\Request $request | ||
| 422 | * | ||
| 423 | * @throws \Alpha\Exception\IllegalArguementException | ||
| 424 | * @throws \Alpha\Exception\SecurityException | ||
| 425 | * @throws \Alpha\Exception\ResourceNotAllowedException | ||
| 426 | * | ||
| 427 | * @return \Alpha\Util\Http\Response | ||
| 428 | * | ||
| 429 | * @since 2.0 | ||
| 430 | */ | ||
| 431 | public function doDELETE($request) | ||
| 511 | |||
| 512 | /** | ||
| 513 | * Sets up the pagination start point and limit. | ||
| 514 | * | ||
| 515 | * @since 2.0 | ||
| 516 | */ | ||
| 517 | public function after_displayPageHead_callback() | ||
| 544 | |||
| 545 | /** | ||
| 546 | * Method to display the page footer with pageination links. | ||
| 547 | * | ||
| 548 | * @return string | ||
| 549 | * | ||
| 550 | * @since 2.0 | ||
| 551 | */ | ||
| 552 | public function before_displayPageFoot_callback() | ||
| 569 | |||
| 570 | /** | ||
| 571 | * Load the requested record and render the HTML or JSON for it. | ||
| 572 | * | ||
| 573 | * @param array $params The request params | ||
| 574 | * @param string $accept The HTTP accept heard value | ||
| 575 | * | ||
| 576 | * @throws \Alpha\Exception\ResourceNotFoundException | ||
| 577 | * @throws \Alpha\Exception\IllegalArguementException | ||
| 578 | * | ||
| 579 | * @return string The display data for the requested record | ||
| 580 | * | ||
| 581 | * @since 3.0 | ||
| 582 | */ | ||
| 583 | private function renderRecord($params, $accept) | ||
| 643 | |||
| 644 | /** | ||
| 645 | * Load all records of the type requested and render the HTML or JSON for them. | ||
| 646 | * | ||
| 647 | * @param array $params The request params | ||
| 648 | * @param string $accept The HTTP accept heard value | ||
| 649 | * | ||
| 650 | * @throws \Alpha\Exception\ResourceNotFoundException | ||
| 651 | * @throws \Alpha\Exception\IllegalArguementException | ||
| 652 | * | ||
| 653 | * @return string The display data for the requested records | ||
| 654 | * | ||
| 655 | * @since 3.0 | ||
| 656 | */ | ||
| 657 | private function renderRecords($params, $accept) | ||
| 722 | |||
| 723 | /** | ||
| 724 | * Get the pagination start point | ||
| 725 | * | ||
| 726 | * @return int | ||
| 727 | * | ||
| 728 | * @since 3.0 | ||
| 729 | */ | ||
| 730 | public function getStart() | ||
| 734 | |||
| 735 | /** | ||
| 736 | * Get the pagination record count | ||
| 737 | * | ||
| 738 | * @return int | ||
| 739 | * | ||
| 740 | * @since 3.0 | ||
| 741 | */ | ||
| 742 | public function getRecordCount() | ||
| 746 | |||
| 747 | /** | ||
| 748 | * Get the pagination limit | ||
| 749 | * | ||
| 750 | * @return int | ||
| 751 | * | ||
| 752 | * @since 3.0 | ||
| 753 | */ | ||
| 754 | public function getLimit() | ||
| 758 | } | ||
| 759 |