diff --git a/composer.json b/composer.json index 922057d8..c56d584a 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,7 @@ "shlinkio/shlink-common": "^3.2.0", "shlinkio/shlink-config": "^1.0", "shlinkio/shlink-event-dispatcher": "^1.4", - "shlinkio/shlink-importer": "^1.0.1", + "shlinkio/shlink-importer": "^2.0", "shlinkio/shlink-installer": "^5.1.0", "shlinkio/shlink-ip-geolocation": "^1.5", "symfony/console": "^5.1", diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index ec752ba9..aba0235f 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -59,7 +59,6 @@ class ShortUrl extends AbstractEntity public static function fromImport( ImportedShlinkUrl $url, - string $source, bool $importShortCode, ?DomainResolverInterface $domainResolver = null ): self { @@ -72,7 +71,7 @@ class ShortUrl extends AbstractEntity } $instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $domainResolver); - $instance->importSource = $source; + $instance->importSource = $url->source(); $instance->dateCreated = Chronos::instance($url->createdAt()); return $instance; diff --git a/module/Core/src/Importer/ImportedLinksProcessor.php b/module/Core/src/Importer/ImportedLinksProcessor.php index cb10ad15..81b8e923 100644 --- a/module/Core/src/Importer/ImportedLinksProcessor.php +++ b/module/Core/src/Importer/ImportedLinksProcessor.php @@ -12,6 +12,8 @@ use Shlinkio\Shlink\Core\Util\DoctrineBatchIterator; use Shlinkio\Shlink\Core\Util\TagManagerTrait; use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface; use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; +use Symfony\Component\Console\Style\StyleInterface; +use function sprintf; class ImportedLinksProcessor implements ImportedLinksProcessorInterface { @@ -29,7 +31,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface /** * @param iterable|ImportedShlinkUrl[] $shlinkUrls */ - public function process(iterable $shlinkUrls, string $source, array $params): void + public function process(StyleInterface $io, iterable $shlinkUrls, array $params): void { /** @var ShortUrlRepositoryInterface $shortUrlRepo */ $shortUrlRepo = $this->em->getRepository(ShortUrl::class); @@ -39,16 +41,20 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface /** @var ImportedShlinkUrl $url */ foreach ($iterable as $url) { // Skip already imported URLs - if ($shortUrlRepo->importedUrlExists($url, $source, $importShortCodes)) { + if ($shortUrlRepo->importedUrlExists($url, $importShortCodes)) { + $io->text(sprintf('%s: Skipped', $url->longUrl())); continue; } - $shortUrl = ShortUrl::fromImport($url, $source, $importShortCodes, $this->domainResolver); + $shortUrl = ShortUrl::fromImport($url, $importShortCodes, $this->domainResolver); $shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags())); + // TODO Handle errors while creating short URLs, to avoid making the whole process fail // * Duplicated short code $this->em->persist($shortUrl); + + $io->text(sprintf('%s: Imported', $url->longUrl())); } } } diff --git a/module/Core/src/Repository/ShortUrlRepository.php b/module/Core/src/Repository/ShortUrlRepository.php index 5cf7d997..39e15d79 100644 --- a/module/Core/src/Repository/ShortUrlRepository.php +++ b/module/Core/src/Repository/ShortUrlRepository.php @@ -256,9 +256,9 @@ DQL; return $qb->getQuery()->getOneOrNullResult(); } - public function importedUrlExists(ImportedShlinkUrl $url, string $source, bool $importShortCodes): bool + public function importedUrlExists(ImportedShlinkUrl $url, bool $importShortCodes): bool { - $findConditions = ['importSource' => $source]; + $findConditions = ['importSource' => $url->source()]; if ($importShortCodes) { $findConditions['shortCode'] = $url->shortCode(); } else { diff --git a/module/Core/src/Repository/ShortUrlRepositoryInterface.php b/module/Core/src/Repository/ShortUrlRepositoryInterface.php index fac50980..b6790543 100644 --- a/module/Core/src/Repository/ShortUrlRepositoryInterface.php +++ b/module/Core/src/Repository/ShortUrlRepositoryInterface.php @@ -32,5 +32,5 @@ interface ShortUrlRepositoryInterface extends ObjectRepository public function findOneMatching(string $url, array $tags, ShortUrlMeta $meta): ?ShortUrl; - public function importedUrlExists(ImportedShlinkUrl $url, string $source, bool $importShortCodes): bool; + public function importedUrlExists(ImportedShlinkUrl $url, bool $importShortCodes): bool; }