Renamed countVisits to countNonOrphanVisits, and updated its signature to expect a VisitsCountFiltering DTO

This commit is contained in:
Alejandro Celaya 2022-01-16 11:15:39 +01:00
parent 60c0ca3ae5
commit 61618250ec
6 changed files with 14 additions and 12 deletions

View file

@ -16,7 +16,6 @@ use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering;
use Shlinkio\Shlink\Core\Visit\Spec\CountOfOrphanVisits; use Shlinkio\Shlink\Core\Visit\Spec\CountOfOrphanVisits;
use Shlinkio\Shlink\Core\Visit\Spec\CountOfShortUrlVisits; use Shlinkio\Shlink\Core\Visit\Spec\CountOfShortUrlVisits;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use const PHP_INT_MAX; use const PHP_INT_MAX;
@ -181,10 +180,9 @@ class VisitRepository extends EntitySpecificationRepository implements VisitRepo
} }
// TODO This should support counting in a date range or excluding bots // TODO This should support counting in a date range or excluding bots
// TODO Rename to countNonOrphanVisits public function countNonOrphanVisits(VisitsCountFiltering $filtering): int
public function countVisits(?ApiKey $apiKey = null): int
{ {
return (int) $this->matchSingleScalarResult(new CountOfShortUrlVisits($apiKey)); return (int) $this->matchSingleScalarResult(new CountOfShortUrlVisits($filtering->apiKey()));
} }
private function createAllVisitsQueryBuilder(VisitsListFiltering $filtering): QueryBuilder private function createAllVisitsQueryBuilder(VisitsListFiltering $filtering): QueryBuilder

View file

@ -10,7 +10,6 @@ use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering;
use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
// TODO Split into VisitsListsRepository and VisitsLocationRepository // TODO Split into VisitsListsRepository and VisitsLocationRepository
interface VisitRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface interface VisitRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface
@ -58,5 +57,5 @@ interface VisitRepositoryInterface extends ObjectRepository, EntitySpecification
*/ */
public function findNonOrphanVisits(VisitsListFiltering $filtering): array; public function findNonOrphanVisits(VisitsListFiltering $filtering): array;
public function countVisits(?ApiKey $apiKey = null): int; public function countNonOrphanVisits(VisitsCountFiltering $filtering): int;
} }

View file

@ -16,6 +16,11 @@ class VisitsCountFiltering
) { ) {
} }
public static function withApiKey(?ApiKey $apiKey): self
{
return new self(null, false, $apiKey);
}
public function dateRange(): ?DateRange public function dateRange(): ?DateRange
{ {
return $this->dateRange; return $this->dateRange;

View file

@ -37,7 +37,7 @@ class VisitsStatsHelper implements VisitsStatsHelperInterface
$visitsRepo = $this->em->getRepository(Visit::class); $visitsRepo = $this->em->getRepository(Visit::class);
return new VisitsStats( return new VisitsStats(
$visitsRepo->countVisits($apiKey), $visitsRepo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($apiKey)),
$visitsRepo->countOrphanVisits(new VisitsCountFiltering()), $visitsRepo->countOrphanVisits(new VisitsCountFiltering()),
); );
} }

View file

@ -297,10 +297,10 @@ class VisitRepositoryTest extends DatabaseTestCase
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
self::assertEquals(4 + 5 + 7, $this->repo->countVisits()); self::assertEquals(4 + 5 + 7, $this->repo->countNonOrphanVisits(new VisitsCountFiltering()));
self::assertEquals(4, $this->repo->countVisits($apiKey1)); self::assertEquals(4, $this->repo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($apiKey1)));
self::assertEquals(5 + 7, $this->repo->countVisits($apiKey2)); self::assertEquals(5 + 7, $this->repo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($apiKey2)));
self::assertEquals(4 + 7, $this->repo->countVisits($domainApiKey)); self::assertEquals(4 + 7, $this->repo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($domainApiKey)));
self::assertEquals(4, $this->repo->countOrphanVisits(new VisitsCountFiltering())); self::assertEquals(4, $this->repo->countOrphanVisits(new VisitsCountFiltering()));
self::assertEquals(3, $this->repo->countOrphanVisits(new VisitsCountFiltering(null, true))); self::assertEquals(3, $this->repo->countOrphanVisits(new VisitsCountFiltering(null, true)));
} }

View file

@ -53,7 +53,7 @@ class VisitsStatsHelperTest extends TestCase
public function returnsExpectedVisitsStats(int $expectedCount): void public function returnsExpectedVisitsStats(int $expectedCount): void
{ {
$repo = $this->prophesize(VisitRepository::class); $repo = $this->prophesize(VisitRepository::class);
$count = $repo->countVisits(null)->willReturn($expectedCount * 3); $count = $repo->countNonOrphanVisits(new VisitsCountFiltering())->willReturn($expectedCount * 3);
$countOrphan = $repo->countOrphanVisits(Argument::type(VisitsCountFiltering::class))->willReturn( $countOrphan = $repo->countOrphanVisits(Argument::type(VisitsCountFiltering::class))->willReturn(
$expectedCount, $expectedCount,
); );