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') |
||
146 | { |
||
147 | self::$logger = new Logger('ActiveRecordController'); |
||
148 | self::$logger->debug('>>__construct()'); |
||
149 | |||
150 | // ensure that the super class constructor is called, indicating the rights group |
||
151 | parent::__construct($visibility); |
||
152 | |||
153 | self::$logger->debug('<<__construct'); |
||
154 | } |
||
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) |
||
418 | |||
419 | /** |
||
420 | * Method to handle DELETE requests. |
||
421 | * |
||
422 | * @param \Alpha\Util\Http\Request $request |
||
423 | * |
||
424 | * @throws \Alpha\Exception\IllegalArguementException |
||
425 | * @throws \Alpha\Exception\SecurityException |
||
426 | * @throws \Alpha\Exception\ResourceNotAllowedException |
||
427 | * |
||
428 | * @return \Alpha\Util\Http\Response |
||
429 | * |
||
430 | * @since 2.0 |
||
431 | */ |
||
432 | public function doDELETE($request) |
||
512 | |||
513 | /** |
||
514 | * Sets up the pagination start point and limit. |
||
515 | * |
||
516 | * @since 2.0 |
||
517 | */ |
||
518 | public function after_displayPageHead_callback() |
||
545 | |||
546 | /** |
||
547 | * Method to display the page footer with pageination links. |
||
548 | * |
||
549 | * @return string |
||
550 | * |
||
551 | * @since 2.0 |
||
552 | */ |
||
553 | public function before_displayPageFoot_callback() |
||
570 | |||
571 | /** |
||
572 | * Load the requested record and render the HTML or JSON for it. |
||
573 | * |
||
574 | * @param array $params The request params |
||
575 | * @param string $accept The HTTP accept heard value |
||
576 | * |
||
577 | * @throws \Alpha\Exception\ResourceNotFoundException |
||
578 | * @throws \Alpha\Exception\IllegalArguementException |
||
579 | * |
||
580 | * @return string The display data for the requested record |
||
581 | * |
||
582 | * @since 3.0 |
||
583 | */ |
||
584 | private function renderRecord($params, $accept) |
||
585 | { |
||
586 | if (!Validator::isInteger($params['ActiveRecordID'])) { |
||
587 | throw new IllegalArguementException('Invalid oid ['.$params['ActiveRecordID'].'] provided on the request!'); |
||
588 | } |
||
589 | |||
590 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
591 | |||
592 | if (class_exists($ActiveRecordType)) { |
||
593 | $record = new $ActiveRecordType(); |
||
594 | } else { |
||
595 | throw new IllegalArguementException('No ActiveRecord available to view!'); |
||
596 | } |
||
597 | |||
598 | // set up the title and meta details |
||
599 | if (isset($params['view']) && $params['view'] == 'edit') { |
||
600 | if (!isset($this->title)) { |
||
601 | $this->setTitle('Editing a '.$record->getFriendlyClassName()); |
||
602 | } |
||
603 | if (!isset($this->description)) { |
||
604 | $this->setDescription('Page to edit a '.$record->getFriendlyClassName().'.'); |
||
605 | } |
||
606 | if (!isset($this->keywords)) { |
||
607 | $this->setKeywords('edit,'.$record->getFriendlyClassName()); |
||
608 | } |
||
609 | } else { |
||
610 | if (!isset($this->title)) { |
||
611 | $this->setTitle('Viewing a '.$record->getFriendlyClassName()); |
||
612 | } |
||
613 | if (!isset($this->description)) { |
||
614 | $this->setDescription('Page to view a '.$record->getFriendlyClassName().'.'); |
||
615 | } |
||
616 | if (!isset($this->keywords)) { |
||
617 | $this->setKeywords('view,'.$record->getFriendlyClassName()); |
||
618 | } |
||
619 | } |
||
620 | |||
621 | $record->load($params['ActiveRecordID']); |
||
622 | ActiveRecord::disconnect(); |
||
623 | |||
624 | $view = View::getInstance($record, false, $accept); |
||
625 | |||
626 | $body = View::displayPageHead($this); |
||
627 | |||
628 | $message = $this->getStatusMessage(); |
||
629 | if (!empty($message)) { |
||
630 | $body .= $message; |
||
631 | } |
||
632 | |||
633 | $body .= View::renderDeleteForm($this->request->getURI()); |
||
634 | |||
635 | if (isset($params['view']) && $params['view'] == 'edit') { |
||
636 | $fields = array('formAction' => $this->request->getURI()); |
||
637 | $body .= $view->editView($fields); |
||
638 | } else { |
||
639 | $body .= $view->detailedView(); |
||
640 | } |
||
641 | |||
642 | return $body; |
||
643 | } |
||
644 | |||
645 | /** |
||
646 | * Load all records of the type requested and render the HTML or JSON for them. |
||
647 | * |
||
648 | * @param array $params The request params |
||
649 | * @param string $accept The HTTP accept heard value |
||
650 | * |
||
651 | * @throws \Alpha\Exception\ResourceNotFoundException |
||
652 | * @throws \Alpha\Exception\IllegalArguementException |
||
653 | * |
||
654 | * @return string The display data for the requested records |
||
655 | * |
||
656 | * @since 3.0 |
||
657 | */ |
||
658 | private function renderRecords($params, $accept) |
||
659 | { |
||
660 | $ActiveRecordType = urldecode($params['ActiveRecordType']); |
||
661 | |||
662 | if (class_exists($ActiveRecordType)) { |
||
663 | $record = new $ActiveRecordType(); |
||
664 | } else { |
||
665 | throw new IllegalArguementException('No ActiveRecord available to view!'); |
||
666 | } |
||
667 | |||
668 | // set up the title and meta details |
||
669 | if (!isset($this->title)) { |
||
670 | $this->setTitle('Listing all '.$record->getFriendlyClassName()); |
||
671 | } |
||
672 | if (!isset($this->description)) { |
||
673 | $this->setDescription('Listing all '.$record->getFriendlyClassName()); |
||
674 | } |
||
675 | if (!isset($this->keywords)) { |
||
676 | $this->setKeywords('list,all,'.$record->getFriendlyClassName()); |
||
677 | } |
||
678 | |||
679 | if (isset($this->filterField) && isset($this->filterValue)) { |
||
680 | if (isset($this->sort) && isset($this->order)) { |
||
681 | $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit'], |
||
682 | $this->sort, $this->order); |
||
683 | } else { |
||
684 | $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit']); |
||
685 | } |
||
686 | |||
687 | $this->recordCount = $record->getCount(array($this->filterField), array($this->filterValue)); |
||
688 | } else { |
||
689 | if (isset($this->sort) && isset($this->order)) { |
||
690 | $records = $record->loadAll($params['start'], $params['limit'], $this->sort, $this->order); |
||
691 | } else { |
||
692 | $records = $record->loadAll($params['start'], $params['limit']); |
||
693 | } |
||
694 | |||
695 | $this->recordCount = $record->getCount(); |
||
696 | } |
||
697 | |||
698 | ActiveRecord::disconnect(); |
||
699 | |||
700 | $view = View::getInstance($record, false, $accept); |
||
701 | |||
702 | $body = View::displayPageHead($this); |
||
703 | |||
704 | $message = $this->getStatusMessage(); |
||
705 | if (!empty($message)) { |
||
706 | $body .= $message; |
||
707 | } |
||
708 | |||
709 | $body .= View::renderDeleteForm($this->request->getURI()); |
||
710 | |||
711 | foreach ($records as $record) { |
||
712 | $view = View::getInstance($record, false, $accept); |
||
713 | $fields = array('formAction' => $this->request->getURI()); |
||
714 | $body .= $view->listView($fields); |
||
715 | } |
||
716 | |||
717 | if ($accept == 'application/json') { |
||
718 | $body = rtrim($body, ','); |
||
719 | } |
||
720 | |||
721 | return $body; |
||
722 | } |
||
723 | |||
724 | /** |
||
725 | * Get the pagination start point |
||
726 | * |
||
727 | * @return int |
||
728 | * |
||
729 | * @since 3.0 |
||
730 | */ |
||
731 | public function getStart() |
||
735 | |||
736 | /** |
||
737 | * Get the pagination record count |
||
738 | * |
||
739 | * @return int |
||
740 | * |
||
741 | * @since 3.0 |
||
742 | */ |
||
743 | public function getRecordCount() |
||
747 | |||
748 | /** |
||
749 | * Get the pagination limit |
||
750 | * |
||
751 | * @return int |
||
752 | * |
||
753 | * @since 3.0 |
||
754 | */ |
||
755 | public function getLimit() |
||
759 | } |
||
760 |