From 531a19dde936873f20b331100e7ec568078152a9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 May 2023 13:04:17 +0200 Subject: [PATCH] Refactor short URL visits deletion layers --- module/Core/src/ShortUrl/ShortUrlVisitsDeleter.php | 4 ++-- .../Visit/Repository/VisitDeleterRepository.php | 14 +++++++++----- .../Repository/VisitDeleterRepositoryInterface.php | 5 ++--- .../test/ShortUrl/ShortUrlVisitsDeleterTest.php | 8 ++++++-- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/module/Core/src/ShortUrl/ShortUrlVisitsDeleter.php b/module/Core/src/ShortUrl/ShortUrlVisitsDeleter.php index 5442af7c..c202c5c2 100644 --- a/module/Core/src/ShortUrl/ShortUrlVisitsDeleter.php +++ b/module/Core/src/ShortUrl/ShortUrlVisitsDeleter.php @@ -23,7 +23,7 @@ class ShortUrlVisitsDeleter implements ShortUrlVisitsDeleterInterface */ public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey): BulkDeleteResult { - $this->resolver->resolveShortUrl($identifier, $apiKey); - return new BulkDeleteResult($this->repository->deleteShortUrlVisits($identifier, $apiKey)); + $shortUrl = $this->resolver->resolveShortUrl($identifier, $apiKey); + return new BulkDeleteResult($this->repository->deleteShortUrlVisits($shortUrl)); } } diff --git a/module/Core/src/Visit/Repository/VisitDeleterRepository.php b/module/Core/src/Visit/Repository/VisitDeleterRepository.php index 79a91f9d..602ba576 100644 --- a/module/Core/src/Visit/Repository/VisitDeleterRepository.php +++ b/module/Core/src/Visit/Repository/VisitDeleterRepository.php @@ -5,14 +5,18 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Visit\Repository; use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository; -use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; -use Shlinkio\Shlink\Rest\Entity\ApiKey; +use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; +use Shlinkio\Shlink\Core\Visit\Entity\Visit; class VisitDeleterRepository extends EntitySpecificationRepository implements VisitDeleterRepositoryInterface { - public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey): int + public function deleteShortUrlVisits(ShortUrl $shortUrl): int { - // TODO: Implement deleteShortUrlVisits() method. - return 0; + $qb = $this->getEntityManager()->createQueryBuilder(); + $qb->delete(Visit::class, 'v') + ->where($qb->expr()->eq('v.shortUrl', ':shortUrl')) + ->setParameter('shortUrl', $shortUrl); + + return $qb->getQuery()->execute(); } } diff --git a/module/Core/src/Visit/Repository/VisitDeleterRepositoryInterface.php b/module/Core/src/Visit/Repository/VisitDeleterRepositoryInterface.php index be2df6a3..61a8af9b 100644 --- a/module/Core/src/Visit/Repository/VisitDeleterRepositoryInterface.php +++ b/module/Core/src/Visit/Repository/VisitDeleterRepositoryInterface.php @@ -4,10 +4,9 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Visit\Repository; -use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; -use Shlinkio\Shlink\Rest\Entity\ApiKey; +use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; interface VisitDeleterRepositoryInterface { - public function deleteShortUrlVisits(ShortUrlIdentifier $identifier, ?ApiKey $apiKey): int; + public function deleteShortUrlVisits(ShortUrl $shortUrl): int; } diff --git a/module/Core/test/ShortUrl/ShortUrlVisitsDeleterTest.php b/module/Core/test/ShortUrl/ShortUrlVisitsDeleterTest.php index 461fc78b..e1690a5b 100644 --- a/module/Core/test/ShortUrl/ShortUrlVisitsDeleterTest.php +++ b/module/Core/test/ShortUrl/ShortUrlVisitsDeleterTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlVisitsDeleter; @@ -31,9 +32,12 @@ class ShortUrlVisitsDeleterTest extends TestCase public function returnsDeletedVisitsFromRepo(int $visitsCount): void { $identifier = ShortUrlIdentifier::fromShortCodeAndDomain(''); + $shortUrl = ShortUrl::withLongUrl('https://example.com'); - $this->resolver->expects($this->once())->method('resolveShortUrl')->with($identifier, null); - $this->repository->expects($this->once())->method('deleteShortUrlVisits')->with($identifier, null)->willReturn( + $this->resolver->expects($this->once())->method('resolveShortUrl')->with($identifier, null)->willReturn( + $shortUrl, + ); + $this->repository->expects($this->once())->method('deleteShortUrlVisits')->with($shortUrl)->willReturn( $visitsCount, );