mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Extracted method to find crawlable short codes to its own query object
This commit is contained in:
@@ -4,20 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Crawling;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\CrawlableShortCodesQueryInterface;
|
||||
|
||||
class CrawlingHelper implements CrawlingHelperInterface
|
||||
{
|
||||
public function __construct(private EntityManagerInterface $em)
|
||||
public function __construct(private readonly CrawlableShortCodesQueryInterface $query)
|
||||
{
|
||||
}
|
||||
|
||||
public function listCrawlableShortCodes(): iterable
|
||||
{
|
||||
/** @var ShortUrlRepositoryInterface $repo */
|
||||
$repo = $this->em->getRepository(ShortUrl::class);
|
||||
yield from $repo->findCrawlableShortCodes();
|
||||
yield from ($this->query)();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
|
||||
|
||||
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
|
||||
class CrawlableShortCodesQuery extends EntitySpecificationRepository implements CrawlableShortCodesQueryInterface
|
||||
{
|
||||
/**
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function __invoke(): iterable
|
||||
{
|
||||
$blockSize = 1000;
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->select('DISTINCT s.shortCode')
|
||||
->from(ShortUrl::class, 's')
|
||||
->where($qb->expr()->eq('s.crawlable', ':crawlable'))
|
||||
->setParameter('crawlable', true)
|
||||
->setMaxResults($blockSize);
|
||||
|
||||
$page = 0;
|
||||
do {
|
||||
$qbClone = (clone $qb)->setFirstResult($blockSize * $page);
|
||||
$iterator = $qbClone->getQuery()->toIterable();
|
||||
$resultsFound = false;
|
||||
$page++;
|
||||
|
||||
foreach ($iterator as ['shortCode' => $shortCode]) {
|
||||
$resultsFound = true;
|
||||
yield $shortCode;
|
||||
}
|
||||
} while ($resultsFound);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
|
||||
|
||||
interface CrawlableShortCodesQueryInterface
|
||||
{
|
||||
/**
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function __invoke(): iterable;
|
||||
}
|
||||
@@ -190,28 +190,4 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU
|
||||
$qb->andWhere($qb->expr()->isNull('s.domain'));
|
||||
}
|
||||
}
|
||||
|
||||
public function findCrawlableShortCodes(): iterable
|
||||
{
|
||||
$blockSize = 1000;
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->select('DISTINCT s.shortCode')
|
||||
->from(ShortUrl::class, 's')
|
||||
->where($qb->expr()->eq('s.crawlable', ':crawlable'))
|
||||
->setParameter('crawlable', true)
|
||||
->setMaxResults($blockSize);
|
||||
|
||||
$page = 0;
|
||||
do {
|
||||
$qbClone = (clone $qb)->setFirstResult($blockSize * $page);
|
||||
$iterator = $qbClone->getQuery()->toIterable();
|
||||
$resultsFound = false;
|
||||
$page++;
|
||||
|
||||
foreach ($iterator as ['shortCode' => $shortCode]) {
|
||||
$resultsFound = true;
|
||||
yield $shortCode;
|
||||
}
|
||||
} while ($resultsFound);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,4 @@ interface ShortUrlRepositoryInterface extends ObjectRepository, EntitySpecificat
|
||||
public function findOneMatching(ShortUrlCreation $meta): ?ShortUrl;
|
||||
|
||||
public function findOneByImportedUrl(ImportedShlinkUrl $url): ?ShortUrl;
|
||||
|
||||
public function findCrawlableShortCodes(): iterable;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user