/**
  * Create a user chosen $alias pointing to a resource in $languageCode.
  *
  * This method does not handle location resources - if a user enters a location target
  * the createCustomUrlAlias method has to be used.
  * This method runs URL filters and and transformers before storing them.
  * Hence the path returned in the URLAlias Value may differ from the given.
  *
  * $alwaysAvailable makes the alias available in all languages.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given
  *         language or if resource is not valid
  *
  * @param string $resource
  * @param string $path
  * @param string $languageCode
  * @param bool $forwarding
  * @param bool $alwaysAvailable
  *
  * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
  */
 public function createGlobalUrlAlias($resource, $path, $languageCode, $forwarding = false, $alwaysAvailable = false)
 {
     if (!preg_match('#^([a-zA-Z0-9_]+):(.+)$#', $resource, $matches)) {
         throw new InvalidArgumentException('$resource', 'argument is not valid');
     }
     $path = $this->cleanUrl($path);
     if ($matches[1] === 'eznode' || 0 === strpos($resource, 'module:content/view/full/')) {
         if ($matches[1] === 'eznode') {
             $locationId = $matches[2];
         } else {
             $resourcePath = explode('/', $matches[2]);
             $locationId = end($resourcePath);
         }
         return $this->createUrlAlias($this->repository->getLocationService()->loadLocation($locationId), $path, $languageCode, $forwarding, $alwaysAvailable);
     }
     $this->repository->beginTransaction();
     try {
         $spiUrlAlias = $this->urlAliasHandler->createGlobalUrlAlias($matches[1] . ':' . $this->cleanUrl($matches[2]), $path, $forwarding, $languageCode, $alwaysAvailable);
         $this->repository->commit();
     } catch (ForbiddenException $e) {
         $this->repository->rollback();
         throw new InvalidArgumentException('$path', $e->getMessage(), $e);
     } catch (Exception $e) {
         $this->repository->rollback();
         throw $e;
     }
     return $this->buildUrlAliasDomainObject($spiUrlAlias, $path);
 }
 /**
  * Restores global URL aliases from the backup table.
  *
  * @see \eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\RegenerateUrlAliasesCommand::restoreGlobalAliases()
  */
 protected function doRestoreGlobalAliases()
 {
     $totalCount = $this->getTotalBackupCount(static::GLOBAL_ALIAS_BACKUP_TABLE);
     $passCount = ceil($totalCount / $this->bulkCount);
     $createdAliasCount = 0;
     $conflictCount = 0;
     if ($totalCount === 0) {
         $this->output->writeln('Could not find any backed up global URL aliases, nothing to restore.');
         $this->output->writeln('');
         return;
     }
     $queryBuilder = $this->connection->createQueryBuilder();
     $queryBuilder->select('*')->from(static::GLOBAL_ALIAS_BACKUP_TABLE)->orderBy('id', 'ASC');
     $this->output->writeln("Restoring {$totalCount} custom URL alias(es).");
     $progressBar = $this->getProgressBar($totalCount);
     $progressBar->start();
     for ($pass = 0; $pass <= $passCount; ++$pass) {
         $rows = $this->loadPassData($queryBuilder, $pass);
         foreach ($rows as $row) {
             try {
                 $this->setMigrationTable();
                 $this->urlAliasHandler->createGlobalUrlAlias($row['resource'], $row['path'], (bool) $row['forwarding'], $row['language_code'], (bool) $row['always_available']);
                 $createdAliasCount += 1;
                 $this->setDefaultTable();
             } catch (ForbiddenException $e) {
                 $conflictCount += 1;
             } catch (Exception $e) {
                 $this->setDefaultTable();
                 throw $e;
             }
         }
         $progressBar->advance(count($rows));
     }
     $progressBar->finish();
     $this->output->writeln('');
     $this->output->writeln("Done. Restored {$createdAliasCount} custom URL alias(es) " . "with {$conflictCount} conflict(s).");
     $this->output->writeln('');
 }