| Conditions | 7 |
| Paths | 32 |
| Total Lines | 63 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 3 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public function actionDownload() |
||
| 22 | { |
||
| 23 | // Get criteria |
||
| 24 | $criteria = craft()->elements->getCriteria('AuditLog', craft()->request->getParam('criteria')); |
||
| 25 | |||
| 26 | // Get element type |
||
| 27 | $elementType = craft()->elements->getElementType('AuditLog'); |
||
| 28 | |||
| 29 | // Get order and sort |
||
| 30 | $viewState = craft()->request->getParam('viewState', array( |
||
| 31 | 'order' => 'id', |
||
| 32 | 'sort' => 'desc', |
||
| 33 | )); |
||
| 34 | |||
| 35 | // Set sort on criteria |
||
| 36 | $criteria->order = $viewState['order'] == 'score' ? 'id' : $viewState['order'].' '.$viewState['sort']; |
||
| 37 | |||
| 38 | // Did we search? |
||
| 39 | $criteria->search = craft()->request->getParam('search'); |
||
| 40 | |||
| 41 | // Get source |
||
| 42 | $sources = $elementType->getSources(); |
||
| 43 | $source = craft()->request->getParam('source', '*'); |
||
| 44 | |||
| 45 | // Set type |
||
| 46 | $criteria->type = $source != '*' ? $sources[$source]['criteria']['type'] : null; |
||
| 47 | |||
| 48 | // Get data |
||
| 49 | $log = craft()->auditLog->log($criteria); |
||
| 50 | |||
| 51 | // Set status attribute |
||
| 52 | $attributes = array('status' => Craft::t('Status')); |
||
| 53 | |||
| 54 | // Get nice attributes |
||
| 55 | $availableAttributes = $elementType->defineAvailableTableAttributes(); |
||
| 56 | |||
| 57 | // Make 'em fit |
||
| 58 | foreach ($availableAttributes as $key => $result) { |
||
| 59 | $attributes[$key] = $result['label']; |
||
| 60 | } |
||
| 61 | |||
| 62 | // Ditch the changes button |
||
| 63 | unset($attributes['changes']); |
||
| 64 | |||
| 65 | // Re-order data |
||
| 66 | $data = StringHelper::convertToUTF8('"'.implode('","', $attributes)."\"\r\n"); |
||
| 67 | foreach ($log as $element) { |
||
| 68 | |||
| 69 | // Gather parsed fields |
||
| 70 | $fields = array(); |
||
| 71 | |||
| 72 | // Parse fields |
||
| 73 | foreach ($attributes as $handle => $field) { |
||
| 74 | $fields[] = $handle == 'status' ? $elementType->getStatuses()[$element->$handle] : strip_tags($elementType->getTableAttributeHtml($element, $handle)); |
||
| 75 | } |
||
| 76 | |||
| 77 | // Set data |
||
| 78 | $data .= StringHelper::convertToUTF8('"'.implode('","', $fields)."\"\r\n"); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Download the file |
||
| 82 | craft()->request->sendFile('auditlog.csv', $data, array('forceDownload' => true, 'mimeType' => 'text/csv')); |
||
| 83 | } |
||
| 84 | } |
||
| 85 |