|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\Importer; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelperInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Util\DoctrineBatchHelperInterface; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Util\TagManagerTrait; |
|
14
|
|
|
use Shlinkio\Shlink\Importer\ImportedLinksProcessorInterface; |
|
15
|
|
|
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl; |
|
16
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
|
17
|
|
|
|
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
class ImportedLinksProcessor implements ImportedLinksProcessorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use TagManagerTrait; |
|
23
|
|
|
|
|
24
|
|
|
private EntityManagerInterface $em; |
|
25
|
|
|
private DomainResolverInterface $domainResolver; |
|
26
|
|
|
private ShortCodeHelperInterface $shortCodeHelper; |
|
27
|
|
|
private DoctrineBatchHelperInterface $batchHelper; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
EntityManagerInterface $em, |
|
31
|
|
|
DomainResolverInterface $domainResolver, |
|
32
|
|
|
ShortCodeHelperInterface $shortCodeHelper, |
|
33
|
|
|
DoctrineBatchHelperInterface $batchHelper |
|
34
|
|
|
) { |
|
35
|
|
|
$this->em = $em; |
|
36
|
|
|
$this->domainResolver = $domainResolver; |
|
37
|
|
|
$this->shortCodeHelper = $shortCodeHelper; |
|
38
|
|
|
$this->batchHelper = $batchHelper; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param iterable|ImportedShlinkUrl[] $shlinkUrls |
|
43
|
|
|
*/ |
|
44
|
|
|
public function process(StyleInterface $io, iterable $shlinkUrls, array $params): void |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var ShortUrlRepositoryInterface $shortUrlRepo */ |
|
47
|
|
|
$shortUrlRepo = $this->em->getRepository(ShortUrl::class); |
|
48
|
|
|
$importShortCodes = $params['import_short_codes']; |
|
49
|
|
|
$iterable = $this->batchHelper->wrapIterable($shlinkUrls, 100); |
|
50
|
|
|
|
|
51
|
|
|
/** @var ImportedShlinkUrl $url */ |
|
52
|
|
|
foreach ($iterable as $url) { |
|
53
|
|
|
$longUrl = $url->longUrl(); |
|
54
|
|
|
|
|
55
|
|
|
// Skip already imported URLs |
|
56
|
|
|
if ($shortUrlRepo->importedUrlExists($url)) { |
|
57
|
|
|
$io->text(sprintf('%s: <comment>Skipped</comment>', $longUrl)); |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$shortUrl = ShortUrl::fromImport($url, $importShortCodes, $this->domainResolver); |
|
62
|
|
|
$shortUrl->setTags($this->tagNamesToEntities($this->em, $url->tags())); |
|
63
|
|
|
|
|
64
|
|
|
if (! $this->handleShortCodeUniqueness($url, $shortUrl, $io, $importShortCodes)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->em->persist($shortUrl); |
|
69
|
|
|
$io->text(sprintf('%s: <info>Imported</info>', $longUrl)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function handleShortCodeUniqueness( |
|
74
|
|
|
ImportedShlinkUrl $url, |
|
75
|
|
|
ShortUrl $shortUrl, |
|
76
|
|
|
StyleInterface $io, |
|
77
|
|
|
bool $importShortCodes |
|
78
|
|
|
): bool { |
|
79
|
|
|
if ($this->shortCodeHelper->ensureShortCodeUniqueness($shortUrl, $importShortCodes)) { |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$longUrl = $url->longUrl(); |
|
84
|
|
|
$action = $io->choice(sprintf( |
|
85
|
|
|
'Failed to import URL "%s" because its short-code "%s" is already in use. Do you want to generate a new ' |
|
86
|
|
|
. 'one or skip it?', |
|
87
|
|
|
$longUrl, |
|
88
|
|
|
$url->shortCode(), |
|
89
|
|
|
), ['Generate new short-code', 'Skip'], 1); |
|
90
|
|
|
|
|
91
|
|
|
if ($action === 'Skip') { |
|
92
|
|
|
$io->text(sprintf('%s: <comment>Skipped</comment>', $longUrl)); |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->shortCodeHelper->ensureShortCodeUniqueness($shortUrl, false); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|