| Total Complexity | 48 |
| Total Lines | 337 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UploadExtensionFileController 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.
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 UploadExtensionFileController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class UploadExtensionFileController extends AbstractController |
||
| 36 | { |
||
| 37 | use BlockSerializationTrait; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ExtensionRepository |
||
| 41 | */ |
||
| 42 | protected $extensionRepository; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var FileHandlingUtility |
||
| 46 | */ |
||
| 47 | protected $fileHandlingUtility; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var ExtensionManagementService |
||
| 51 | */ |
||
| 52 | protected $managementService; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $extensionBackupPath = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $removeFromOriginalPath = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param ExtensionRepository $extensionRepository |
||
| 66 | */ |
||
| 67 | public function injectExtensionRepository(ExtensionRepository $extensionRepository) |
||
| 68 | { |
||
| 69 | $this->extensionRepository = $extensionRepository; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param FileHandlingUtility $fileHandlingUtility |
||
| 74 | */ |
||
| 75 | public function injectFileHandlingUtility(FileHandlingUtility $fileHandlingUtility) |
||
| 76 | { |
||
| 77 | $this->fileHandlingUtility = $fileHandlingUtility; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param ExtensionManagementService $managementService |
||
| 82 | */ |
||
| 83 | public function injectManagementService(ExtensionManagementService $managementService) |
||
| 84 | { |
||
| 85 | $this->managementService = $managementService; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Remove backup folder before destruction |
||
| 90 | */ |
||
| 91 | public function __destruct() |
||
| 92 | { |
||
| 93 | $this->removeBackupFolder(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Render upload extension form |
||
| 98 | */ |
||
| 99 | public function formAction() |
||
| 100 | { |
||
| 101 | if (Environment::isComposerMode()) { |
||
| 102 | throw new ExtensionManagerException( |
||
| 103 | 'Composer mode is active. You are not allowed to upload any extension file.', |
||
| 104 | 1444725828 |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Extract an uploaded file and install the matching extension |
||
| 111 | * |
||
| 112 | * @param bool $overwrite Overwrite existing extension if TRUE |
||
| 113 | * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException |
||
| 114 | */ |
||
| 115 | public function extractAction($overwrite = false) |
||
| 116 | { |
||
| 117 | if (Environment::isComposerMode()) { |
||
| 118 | throw new ExtensionManagerException( |
||
| 119 | 'Composer mode is active. You are not allowed to upload any extension file.', |
||
| 120 | 1444725853 |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager']; |
||
| 124 | $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME); |
||
| 125 | try { |
||
| 126 | // If the file name isn't valid an error will be thrown |
||
| 127 | $this->checkFileName($fileName); |
||
| 128 | if (!empty($file['tmp_name']['extensionFile'])) { |
||
| 129 | $tempFile = GeneralUtility::upload_to_tempfile($file['tmp_name']['extensionFile']); |
||
| 130 | } else { |
||
| 131 | throw new ExtensionManagerException( |
||
| 132 | 'Creating temporary file failed. Check your upload_max_filesize and post_max_size limits.', |
||
| 133 | 1342864339 |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | $extensionData = $this->extractExtensionFromFile($tempFile, $fileName, $overwrite); |
||
| 137 | $isAutomaticInstallationEnabled = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('extensionmanager', 'automaticInstallation'); |
||
| 138 | if (!$isAutomaticInstallationEnabled) { |
||
| 139 | $this->addFlashMessage( |
||
| 140 | $this->translate('extensionList.uploadFlashMessage.message', [$extensionData['extKey']]), |
||
| 141 | $this->translate('extensionList.uploadFlashMessage.title'), |
||
| 142 | FlashMessage::OK |
||
| 143 | ); |
||
| 144 | } else { |
||
| 145 | if ($this->activateExtension($extensionData['extKey'])) { |
||
| 146 | $this->addFlashMessage( |
||
| 147 | $this->translate('extensionList.installedFlashMessage.message', [$extensionData['extKey']]), |
||
| 148 | '', |
||
| 149 | FlashMessage::OK |
||
| 150 | ); |
||
| 151 | } else { |
||
| 152 | $this->redirect('unresolvedDependencies', 'List', null, ['extensionKey' => $extensionData['extKey']]); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } catch (StopActionException $exception) { |
||
| 156 | throw $exception; |
||
| 157 | } catch (DependencyConfigurationNotFoundException $exception) { |
||
| 158 | $this->addFlashMessage($exception->getMessage(), '', FlashMessage::ERROR); |
||
| 159 | } catch (\Exception $exception) { |
||
| 160 | $this->removeExtensionAndRestoreFromBackup($fileName); |
||
| 161 | $this->addFlashMessage($exception->getMessage(), '', FlashMessage::ERROR); |
||
| 162 | } |
||
| 163 | $this->redirect('index', 'List', null, [ |
||
| 164 | self::TRIGGER_RefreshModuleMenu => true, |
||
| 165 | self::TRIGGER_RefreshTopbar => true |
||
| 166 | ]); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Validate the filename of an uploaded file |
||
| 171 | * |
||
| 172 | * @param string $fileName |
||
| 173 | * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException |
||
| 174 | */ |
||
| 175 | public function checkFileName($fileName) |
||
| 176 | { |
||
| 177 | $extension = pathinfo($fileName, PATHINFO_EXTENSION); |
||
| 178 | if (empty($fileName)) { |
||
| 179 | throw new ExtensionManagerException('No file given.', 1342858852); |
||
| 180 | } |
||
| 181 | if ($extension !== 't3x' && $extension !== 'zip') { |
||
| 182 | throw new ExtensionManagerException('Wrong file format "' . $extension . '" given. Allowed formats are t3x and zip.', 1342858853); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Extract a given t3x or zip file |
||
| 188 | * |
||
| 189 | * @param string $uploadPath Path to existing extension file |
||
| 190 | * @param string $fileName Filename of the uploaded file |
||
| 191 | * @param bool $overwrite If true, extension will be replaced |
||
| 192 | * @return array Extension data |
||
| 193 | * @throws ExtensionManagerException |
||
| 194 | * @throws DependencyConfigurationNotFoundException |
||
| 195 | */ |
||
| 196 | public function extractExtensionFromFile($uploadPath, $fileName, $overwrite) |
||
| 197 | { |
||
| 198 | $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION); |
||
| 199 | if ($fileExtension === 't3x') { |
||
| 200 | $extensionData = $this->getExtensionFromT3xFile($uploadPath, $overwrite); |
||
| 201 | } else { |
||
| 202 | $extensionData = $this->getExtensionFromZipFile($uploadPath, $fileName, $overwrite); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $extensionData; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $extensionKey |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function activateExtension($extensionKey) |
||
| 213 | { |
||
| 214 | $this->managementService->reloadPackageInformation($extensionKey); |
||
| 215 | $extension = $this->managementService->getExtension($extensionKey); |
||
| 216 | return is_array($this->managementService->installExtension($extension)); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Extracts a given t3x file and installs the extension |
||
| 221 | * |
||
| 222 | * @param string $file Path to uploaded file |
||
| 223 | * @param bool $overwrite Overwrite existing extension if TRUE |
||
| 224 | * @throws ExtensionManagerException |
||
| 225 | * @throws DependencyConfigurationNotFoundException |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | protected function getExtensionFromT3xFile($file, $overwrite = false) |
||
| 229 | { |
||
| 230 | $fileContent = file_get_contents($file); |
||
| 231 | if (!$fileContent) { |
||
| 232 | throw new ExtensionManagerException('File had no or wrong content.', 1342859339); |
||
| 233 | } |
||
| 234 | $extensionData = $this->decodeExchangeData($fileContent); |
||
| 235 | if (empty($extensionData['extKey'])) { |
||
| 236 | throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309); |
||
| 237 | } |
||
| 238 | $isExtensionAvailable = $this->managementService->isAvailable($extensionData['extKey']); |
||
| 239 | if (!$overwrite && $isExtensionAvailable) { |
||
| 240 | throw new ExtensionManagerException($this->translate('extensionList.overwritingDisabled'), 1342864310); |
||
| 241 | } |
||
| 242 | if ($isExtensionAvailable) { |
||
| 243 | $this->copyExtensionFolderToTempFolder($extensionData['extKey']); |
||
| 244 | } |
||
| 245 | $this->removeFromOriginalPath = true; |
||
| 246 | $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionData['extKey'], $extensionData['EM_CONF']['version']); |
||
| 247 | $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData, $extension); |
||
|
|
|||
| 248 | |||
| 249 | if (empty($extension) |
||
| 250 | && empty($extensionData['EM_CONF']['constraints']) |
||
| 251 | && !isset($extensionData['FILES']['ext_emconf.php']) |
||
| 252 | && !isset($extensionData['FILES']['/ext_emconf.php']) |
||
| 253 | ) { |
||
| 254 | throw new DependencyConfigurationNotFoundException('Extension cannot be installed automatically because no dependencies could be found! Please check dependencies manually (on typo3.org) before installing the extension.', 1439587168); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $extensionData; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Extracts a given zip file and installs the extension |
||
| 262 | * As there is no information about the extension key in the zip |
||
| 263 | * we have to use the file name to get that information |
||
| 264 | * filename format is expected to be extensionkey_version.zip |
||
| 265 | * |
||
| 266 | * @param string $file Path to uploaded file |
||
| 267 | * @param string $fileName Filename (basename) of uploaded file |
||
| 268 | * @param bool $overwrite Overwrite existing extension if TRUE |
||
| 269 | * @return array |
||
| 270 | * @throws ExtensionManagerException |
||
| 271 | */ |
||
| 272 | protected function getExtensionFromZipFile($file, $fileName, $overwrite = false) |
||
| 273 | { |
||
| 274 | // Remove version and extension from filename to determine the extension key |
||
| 275 | $extensionKey = $this->getExtensionKeyFromFileName($fileName); |
||
| 276 | $isExtensionAvailable = $this->managementService->isAvailable($extensionKey); |
||
| 277 | if (!$overwrite && $isExtensionAvailable) { |
||
| 278 | throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311); |
||
| 279 | } |
||
| 280 | if ($isExtensionAvailable) { |
||
| 281 | $this->copyExtensionFolderToTempFolder($extensionKey); |
||
| 282 | } |
||
| 283 | $this->removeFromOriginalPath = true; |
||
| 284 | $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey); |
||
| 285 | |||
| 286 | return ['extKey' => $extensionKey]; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Removes version and file extension from filename to determine extension key |
||
| 291 | * |
||
| 292 | * @param string $fileName |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | protected function getExtensionKeyFromFileName($fileName) |
||
| 296 | { |
||
| 297 | return preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+).*/i', '', strtolower(substr($fileName, 0, -4))); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Copies current extension folder to typo3temp directory as backup |
||
| 302 | * |
||
| 303 | * @param string $extensionKey |
||
| 304 | * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException |
||
| 305 | */ |
||
| 306 | protected function copyExtensionFolderToTempFolder($extensionKey) |
||
| 307 | { |
||
| 308 | $this->extensionBackupPath = Environment::getVarPath() . '/transient/' . $extensionKey . substr(sha1($extensionKey . microtime()), 0, 7) . '/'; |
||
| 309 | GeneralUtility::mkdir($this->extensionBackupPath); |
||
| 310 | GeneralUtility::copyDirectory( |
||
| 311 | $this->fileHandlingUtility->getExtensionDir($extensionKey), |
||
| 312 | $this->extensionBackupPath |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Removes the extension directory and restores the extension from the backup directory |
||
| 318 | * |
||
| 319 | * @param string $fileName |
||
| 320 | * @see UploadExtensionFileController::extractAction |
||
| 321 | */ |
||
| 322 | protected function removeExtensionAndRestoreFromBackup($fileName) |
||
| 323 | { |
||
| 324 | $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->getExtensionKeyFromFileName($fileName)); |
||
| 325 | if ($this->removeFromOriginalPath && is_dir($extDirPath)) { |
||
| 326 | GeneralUtility::rmdir($extDirPath, true); |
||
| 327 | } |
||
| 328 | if (!empty($this->extensionBackupPath)) { |
||
| 329 | GeneralUtility::mkdir($extDirPath); |
||
| 330 | GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath); |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Removes the backup folder in typo3temp |
||
| 336 | */ |
||
| 337 | protected function removeBackupFolder() |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Decodes extension upload array. |
||
| 347 | * This kind of data is when an extension is uploaded to TER |
||
| 348 | * |
||
| 349 | * @param string $stream Data stream |
||
| 350 | * @throws ExtensionManagerException |
||
| 351 | * @return array Array with result on success, otherwise an error string. |
||
| 352 | */ |
||
| 353 | protected function decodeExchangeData(string $stream): array |
||
| 372 | } |
||
| 373 | } |
||
| 374 |