Added feedback to ImportedLinksProcessor

This commit is contained in:
Alejandro Celaya 2020-10-24 15:08:34 +02:00
parent ec3e7212b2
commit 2256f6a9e7
5 changed files with 14 additions and 9 deletions

View File

@ -53,7 +53,7 @@
"shlinkio/shlink-common": "^3.2.0", "shlinkio/shlink-common": "^3.2.0",
"shlinkio/shlink-config": "^1.0", "shlinkio/shlink-config": "^1.0",
"shlinkio/shlink-event-dispatcher": "^1.4", "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-installer": "^5.1.0",
"shlinkio/shlink-ip-geolocation": "^1.5", "shlinkio/shlink-ip-geolocation": "^1.5",
"symfony/console": "^5.1", "symfony/console": "^5.1",

View File

@ -59,7 +59,6 @@ class ShortUrl extends AbstractEntity
public static function fromImport( public static function fromImport(
ImportedShlinkUrl $url, ImportedShlinkUrl $url,
string $source,
bool $importShortCode, bool $importShortCode,
?DomainResolverInterface $domainResolver = null ?DomainResolverInterface $domainResolver = null
): self { ): self {
@ -72,7 +71,7 @@ class ShortUrl extends AbstractEntity
} }
$instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $domainResolver); $instance = new self($url->longUrl(), ShortUrlMeta::fromRawData($meta), $domainResolver);
$instance->importSource = $source; $instance->importSource = $url->source();
$instance->dateCreated = Chronos::instance($url->createdAt()); $instance->dateCreated = Chronos::instance($url->createdAt());
return $instance; return $instance;

View File

@ -12,6 +12,8 @@ use Shlinkio\Shlink\Core\Util\DoctrineBatchIterator;
use Shlinkio\Shlink\Core\Util\TagManagerTrait; use Shlinkio\Shlink\Core\Util\TagManagerTrait;
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface; use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Symfony\Component\Console\Style\StyleInterface;
use function sprintf;
class ImportedLinksProcessor implements ImportedLinksProcessorInterface class ImportedLinksProcessor implements ImportedLinksProcessorInterface
{ {
@ -29,7 +31,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
/** /**
* @param iterable|ImportedShlinkUrl[] $shlinkUrls * @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 */ /** @var ShortUrlRepositoryInterface $shortUrlRepo */
$shortUrlRepo = $this->em->getRepository(ShortUrl::class); $shortUrlRepo = $this->em->getRepository(ShortUrl::class);
@ -39,16 +41,20 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
/** @var ImportedShlinkUrl $url */ /** @var ImportedShlinkUrl $url */
foreach ($iterable as $url) { foreach ($iterable as $url) {
// Skip already imported URLs // Skip already imported URLs
if ($shortUrlRepo->importedUrlExists($url, $source, $importShortCodes)) { if ($shortUrlRepo->importedUrlExists($url, $importShortCodes)) {
$io->text(sprintf('%s: <comment>Skipped</comment>', $url->longUrl()));
continue; continue;
} }
$shortUrl = ShortUrl::fromImport($url, $source, $importShortCodes, $this->domainResolver); $shortUrl = ShortUrl::fromImport($url, $importShortCodes, $this->domainResolver);
$shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags())); $shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags()));
// TODO Handle errors while creating short URLs, to avoid making the whole process fail // TODO Handle errors while creating short URLs, to avoid making the whole process fail
// * Duplicated short code // * Duplicated short code
$this->em->persist($shortUrl); $this->em->persist($shortUrl);
$io->text(sprintf('%s: <info>Imported</info>', $url->longUrl()));
} }
} }
} }

View File

@ -256,9 +256,9 @@ DQL;
return $qb->getQuery()->getOneOrNullResult(); 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) { if ($importShortCodes) {
$findConditions['shortCode'] = $url->shortCode(); $findConditions['shortCode'] = $url->shortCode();
} else { } else {

View File

@ -32,5 +32,5 @@ interface ShortUrlRepositoryInterface extends ObjectRepository
public function findOneMatching(string $url, array $tags, ShortUrlMeta $meta): ?ShortUrl; 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;
} }