| Conditions | 3 |
| Paths | 4 |
| Total Lines | 55 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 98 | public function actionImportCsv(string $siteHandle = null): Response |
||
| 99 | { |
||
| 100 | $variables = []; |
||
| 101 | PermissionHelper::controllerPermissionCheck('retour:redirects'); |
||
| 102 | // Get the site to edit |
||
| 103 | $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
||
| 104 | $pluginName = Retour::$settings->pluginName; |
||
| 105 | $templateTitle = Craft::t('retour', 'Import CSV File'); |
||
| 106 | $view = Craft::$app->getView(); |
||
| 107 | // Asset bundle |
||
| 108 | try { |
||
| 109 | $view->registerAssetBundle(RetourImportAsset::class); |
||
| 110 | } catch (InvalidConfigException $e) { |
||
| 111 | Craft::error($e->getMessage(), __METHOD__); |
||
| 112 | } |
||
| 113 | $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
||
| 114 | '@nystudio107/retour/assetbundles/retour/dist', |
||
| 115 | true |
||
| 116 | ); |
||
| 117 | // Enabled sites |
||
| 118 | MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
||
| 119 | $variables['controllerHandle'] = 'file'; |
||
| 120 | |||
| 121 | // Basic variables |
||
| 122 | $variables['fullPageForm'] = true; |
||
| 123 | $variables['docsUrl'] = self::DOCUMENTATION_URL; |
||
| 124 | $variables['pluginName'] = $pluginName; |
||
| 125 | $variables['title'] = $templateTitle; |
||
| 126 | $variables['crumbs'] = [ |
||
| 127 | [ |
||
| 128 | 'label' => $pluginName, |
||
| 129 | 'url' => UrlHelper::cpUrl('retour'), |
||
| 130 | ], |
||
| 131 | [ |
||
| 132 | 'label' => 'Redirects', |
||
| 133 | 'url' => UrlHelper::cpUrl('retour/redirects'), |
||
| 134 | ], |
||
| 135 | ]; |
||
| 136 | $variables['docTitle'] = "{$pluginName} - Redirects - {$templateTitle}"; |
||
| 137 | $variables['selectedSubnavItem'] = 'redirects'; |
||
| 138 | |||
| 139 | // The CSV file |
||
| 140 | $file = UploadedFile::getInstanceByName('file'); |
||
| 141 | if ($file !== null) { |
||
| 142 | $filename = Craft::$app->getPath()->getTempPath() . DIRECTORY_SEPARATOR . uniqid($file->name, true); |
||
| 143 | $file->saveAs($filename, false); |
||
| 144 | $csv = Reader::createFromPath($file->tempName); |
||
| 145 | $headers = $csv->fetchOne(0); |
||
| 146 | Craft::info(print_r($headers, true), __METHOD__); |
||
| 147 | $variables['headers'] = $headers; |
||
| 148 | $variables['filename'] = $filename; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Render the template |
||
| 152 | return $this->renderTemplate('retour/import/index', $variables); |
||
| 153 | } |
||
| 155 |