Refactor short URL visits deletion layers

This commit is contained in:
Alejandro Celaya 2023-05-14 13:04:17 +02:00
parent 69ff7de481
commit 531a19dde9
4 changed files with 19 additions and 12 deletions

View File

@ -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));
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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,
);