/**
  * Removes the extension directory and restores the extension from the backup directory
  *
  * @param string $fileName
  * @see UploadExtensionFileController::extractAction
  * @return void
  */
 protected function removeExtensionAndRestoreFromBackup($fileName)
 {
     $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->getExtensionKeyFromFileName($fileName));
     if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
         GeneralUtility::rmdir($extDirPath, true);
     }
     if (!empty($this->extensionBackupPath)) {
         GeneralUtility::mkdir($extDirPath);
         GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
     }
 }
 /**
  * Fetch an extension from TER.
  *
  * @param string $extensionKey     The extension key
  * @param string $location         Where to import the extension. System = typo3/sysext, Global = typo3/ext, Local = typo3conf/ext
  * @param bool   $overwrite        Overwrite the extension if it already exists
  * @param int    $mirror           The mirror to fetch the extension from
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return array
  */
 public function fetchExtension($extensionKey, $version = '', $location = 'Local', $overwrite = FALSE, $mirror = -1)
 {
     if (!is_numeric($mirror)) {
         throw new InvalidArgumentException('Option --mirror must be a number. Run the command extensionapi:listmirrors to get the list of all available repositories');
     }
     if ($version === '') {
         $extension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
         if ($extension === NULL) {
             throw new InvalidArgumentException(sprintf('Extension "%s" was not found on TER', $extensionKey));
         }
     } else {
         $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
         if ($extension === NULL) {
             throw new InvalidArgumentException(sprintf('Version %s of extension "%s" does not exist', $version, $extensionKey));
         }
     }
     if (!$overwrite) {
         $comingExtPath = $this->fileHandlingUtility->getExtensionDir($extensionKey, $location);
         if (@is_dir($comingExtPath)) {
             throw new InvalidArgumentException(sprintf('Extension "%s" already exists at "%s"!', $extensionKey, $comingExtPath));
         }
     }
     $mirrors = $this->repositoryHelper->getMirrors();
     if ($mirrors === NULL) {
         throw new RuntimeException('No mirrors found!');
     }
     if ($mirror === -1) {
         $mirrors->setSelect();
     } elseif ($mirror > 0 && $mirror <= count($mirrors->getMirrors())) {
         $mirrors->setSelect($mirror);
     } else {
         throw new InvalidArgumentException(sprintf('Mirror "%s" does not exist', $mirror));
     }
     /**
      * @var \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility $downloadUtility
      */
     $downloadUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\DownloadUtility');
     $downloadUtility->setDownloadPath($location);
     $this->extensionManagementService->downloadMainExtension($extension);
     $return = array();
     $extensionDir = $this->fileHandlingUtility->getExtensionDir($extensionKey, $location);
     if (is_dir($extensionDir)) {
         $return['main']['extKey'] = $extension->getExtensionKey();
         $return['main']['version'] = $extension->getVersion();
     } else {
         throw new RuntimeException(sprintf('Extension "%s" version %s could not installed!', $extensionKey, $extension->getVersion()));
     }
     return $return;
 }