Added test covering imported short URLs with visits

This commit is contained in:
Alejandro Celaya
2021-04-18 12:44:02 +02:00
parent c8b8947b1f
commit b277f431c2
4 changed files with 91 additions and 19 deletions

View File

@@ -150,6 +150,15 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this->type;
}
/**
* Needed only for ArrayCollections to be able to apply criteria filtering
* @internal
*/
public function getType(): string
{
return $this->type();
}
public function jsonSerialize(): array
{
return [

View File

@@ -49,9 +49,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
/** @var ImportedShlinkUrl $importedUrl */
foreach ($iterable as $importedUrl) {
$longUrl = $importedUrl->longUrl();
$generateNewIfDuplicated = static function () use ($io, $importedUrl): bool {
$skipOnShortCodeConflict = static function () use ($io, $importedUrl): bool {
$action = $io->choice(sprintf(
'Failed to import URL "%s" because its short-code "%s" is already in use. Do you want to generate '
. 'a new one or skip it?',
@@ -59,10 +57,11 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
$importedUrl->shortCode(),
), ['Generate new short-code', 'Skip'], 1);
return $action !== 'Skip';
return $action === 'Skip';
};
[$shortUrl, $isNew] = $this->getOrCreateShortUrl($importedUrl, $importShortCodes, $generateNewIfDuplicated);
[$shortUrl, $isNew] = $this->getOrCreateShortUrl($importedUrl, $importShortCodes, $skipOnShortCodeConflict);
$longUrl = $importedUrl->longUrl();
if ($shortUrl === null) {
$io->text(sprintf('%s: <fg=red>Error</>', $longUrl));
continue;
@@ -93,15 +92,15 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
private function getOrCreateShortUrl(
ImportedShlinkUrl $importedUrl,
bool $importShortCodes,
callable $generateNewIfDuplicated
callable $skipOnShortCodeConflict
): array {
$existingShortUrl = $this->shortUrlRepo->findOneByImportedUrl($importedUrl);
if ($existingShortUrl !== null) {
return [$existingShortUrl, false];
$alreadyImportedShortUrl = $this->shortUrlRepo->findOneByImportedUrl($importedUrl);
if ($alreadyImportedShortUrl !== null) {
return [$alreadyImportedShortUrl, false];
}
$shortUrl = ShortUrl::fromImport($importedUrl, $importShortCodes, $this->relationResolver);
if (! $this->handleShortCodeUniqueness($shortUrl, $importShortCodes, $generateNewIfDuplicated)) {
if (! $this->handleShortCodeUniqueness($shortUrl, $importShortCodes, $skipOnShortCodeConflict)) {
return [null, false];
}
@@ -112,13 +111,13 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface
private function handleShortCodeUniqueness(
ShortUrl $shortUrl,
bool $importShortCodes,
callable $generateNewIfDuplicated
callable $skipOnShortCodeConflict
): bool {
if ($this->shortCodeHelper->ensureShortCodeUniqueness($shortUrl, $importShortCodes)) {
return true;
}
if (! $generateNewIfDuplicated()) {
if ($skipOnShortCodeConflict()) {
return false;
}