| Conditions | 13 |
| Paths | 67 |
| Total Lines | 61 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 65 | public function import($data, $form, $request) |
||
| 66 | { |
||
| 67 | if (!$this->showImportForm |
||
| 68 | || (is_array($this->showImportForm) && !in_array($this->modelClass, $this->showImportForm)) |
||
| 69 | ) { |
||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | $importers = $this->getModelImporters(); |
||
| 74 | $loader = $importers[$this->modelClass]; |
||
| 75 | |||
| 76 | $fileContents = !empty($data['_CsvFile']['tmp_name']) ? file_get_contents($data['_CsvFile']['tmp_name']) : ''; |
||
| 77 | // File wasn't properly uploaded, show a reminder to the user |
||
| 78 | if (!$fileContents) { |
||
| 79 | $form->sessionMessage( |
||
| 80 | _t('SilverStripe\\Admin\\ModelAdmin.NOCSVFILE', 'Please browse for a CSV file to import'), |
||
| 81 | 'bad' |
||
| 82 | ); |
||
| 83 | $this->redirectBack(); |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!empty($data['EmptyBeforeImport']) && $data['EmptyBeforeImport']) { //clear database before import |
||
| 88 | $loader->deleteExistingRecords = true; |
||
| 89 | } |
||
| 90 | |||
| 91 | $results = $loader->load($data['_CsvFile']['tmp_name']); |
||
| 92 | |||
| 93 | // copy the uploaded file into the export path |
||
| 94 | RegistryImportFeed::singleton() |
||
| 95 | ->getAssetHandler() |
||
| 96 | ->setContent($this->getCsvImportFilename(), $fileContents); |
||
| 97 | |||
| 98 | $message = ''; |
||
| 99 | if ($results->CreatedCount()) { |
||
| 100 | $message .= _t( |
||
| 101 | 'SilverStripe\\Admin\\ModelAdmin.IMPORTEDRECORDS', |
||
| 102 | "Imported {count} records.", |
||
| 103 | ['count' => $results->CreatedCount()] |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | if ($results->UpdatedCount()) { |
||
| 107 | $message .= _t( |
||
| 108 | 'SilverStripe\\Admin\\ModelAdmin.UPDATEDRECORDS', |
||
| 109 | "Updated {count} records.", |
||
| 110 | ['count' => $results->UpdatedCount()] |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | if ($results->DeletedCount()) { |
||
| 114 | $message .= _t( |
||
| 115 | 'SilverStripe\\Admin\\ModelAdmin.DELETEDRECORDS', |
||
| 116 | "Deleted {count} records.", |
||
| 117 | ['count' => $results->DeletedCount()] |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | if (!$results->CreatedCount() && !$results->UpdatedCount()) { |
||
| 121 | $message .= _t('SilverStripe\\Admin\\ModelAdmin.NOIMPORT', "Nothing to import"); |
||
| 122 | } |
||
| 123 | |||
| 124 | $form->sessionMessage($message, 'good'); |
||
| 125 | $this->redirectBack(); |
||
| 126 | } |
||
| 128 |