diff --git a/module/Core/src/Repository/VisitRepository.php b/module/Core/src/Repository/VisitRepository.php index b8b4688c..6c48a318 100644 --- a/module/Core/src/Repository/VisitRepository.php +++ b/module/Core/src/Repository/VisitRepository.php @@ -16,7 +16,6 @@ use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering; use Shlinkio\Shlink\Core\Visit\Spec\CountOfOrphanVisits; use Shlinkio\Shlink\Core\Visit\Spec\CountOfShortUrlVisits; -use Shlinkio\Shlink\Rest\Entity\ApiKey; 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 Rename to countNonOrphanVisits - public function countVisits(?ApiKey $apiKey = null): int + public function countNonOrphanVisits(VisitsCountFiltering $filtering): int { - return (int) $this->matchSingleScalarResult(new CountOfShortUrlVisits($apiKey)); + return (int) $this->matchSingleScalarResult(new CountOfShortUrlVisits($filtering->apiKey())); } private function createAllVisitsQueryBuilder(VisitsListFiltering $filtering): QueryBuilder diff --git a/module/Core/src/Repository/VisitRepositoryInterface.php b/module/Core/src/Repository/VisitRepositoryInterface.php index d541e20e..3d480c01 100644 --- a/module/Core/src/Repository/VisitRepositoryInterface.php +++ b/module/Core/src/Repository/VisitRepositoryInterface.php @@ -10,7 +10,6 @@ use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsCountFiltering; use Shlinkio\Shlink\Core\Visit\Persistence\VisitsListFiltering; -use Shlinkio\Shlink\Rest\Entity\ApiKey; // TODO Split into VisitsListsRepository and VisitsLocationRepository interface VisitRepositoryInterface extends ObjectRepository, EntitySpecificationRepositoryInterface @@ -58,5 +57,5 @@ interface VisitRepositoryInterface extends ObjectRepository, EntitySpecification */ public function findNonOrphanVisits(VisitsListFiltering $filtering): array; - public function countVisits(?ApiKey $apiKey = null): int; + public function countNonOrphanVisits(VisitsCountFiltering $filtering): int; } diff --git a/module/Core/src/Visit/Persistence/VisitsCountFiltering.php b/module/Core/src/Visit/Persistence/VisitsCountFiltering.php index c0389ef6..140ec9b9 100644 --- a/module/Core/src/Visit/Persistence/VisitsCountFiltering.php +++ b/module/Core/src/Visit/Persistence/VisitsCountFiltering.php @@ -16,6 +16,11 @@ class VisitsCountFiltering ) { } + public static function withApiKey(?ApiKey $apiKey): self + { + return new self(null, false, $apiKey); + } + public function dateRange(): ?DateRange { return $this->dateRange; diff --git a/module/Core/src/Visit/VisitsStatsHelper.php b/module/Core/src/Visit/VisitsStatsHelper.php index 91f4d4fa..25f5c82e 100644 --- a/module/Core/src/Visit/VisitsStatsHelper.php +++ b/module/Core/src/Visit/VisitsStatsHelper.php @@ -37,7 +37,7 @@ class VisitsStatsHelper implements VisitsStatsHelperInterface $visitsRepo = $this->em->getRepository(Visit::class); return new VisitsStats( - $visitsRepo->countVisits($apiKey), + $visitsRepo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($apiKey)), $visitsRepo->countOrphanVisits(new VisitsCountFiltering()), ); } diff --git a/module/Core/test-db/Repository/VisitRepositoryTest.php b/module/Core/test-db/Repository/VisitRepositoryTest.php index 390b1707..a7981ea1 100644 --- a/module/Core/test-db/Repository/VisitRepositoryTest.php +++ b/module/Core/test-db/Repository/VisitRepositoryTest.php @@ -297,10 +297,10 @@ class VisitRepositoryTest extends DatabaseTestCase $this->getEntityManager()->flush(); - self::assertEquals(4 + 5 + 7, $this->repo->countVisits()); - self::assertEquals(4, $this->repo->countVisits($apiKey1)); - self::assertEquals(5 + 7, $this->repo->countVisits($apiKey2)); - self::assertEquals(4 + 7, $this->repo->countVisits($domainApiKey)); + self::assertEquals(4 + 5 + 7, $this->repo->countNonOrphanVisits(new VisitsCountFiltering())); + self::assertEquals(4, $this->repo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($apiKey1))); + self::assertEquals(5 + 7, $this->repo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($apiKey2))); + self::assertEquals(4 + 7, $this->repo->countNonOrphanVisits(VisitsCountFiltering::withApiKey($domainApiKey))); self::assertEquals(4, $this->repo->countOrphanVisits(new VisitsCountFiltering())); self::assertEquals(3, $this->repo->countOrphanVisits(new VisitsCountFiltering(null, true))); } diff --git a/module/Core/test/Visit/VisitsStatsHelperTest.php b/module/Core/test/Visit/VisitsStatsHelperTest.php index ab76bbf1..7d9a06b9 100644 --- a/module/Core/test/Visit/VisitsStatsHelperTest.php +++ b/module/Core/test/Visit/VisitsStatsHelperTest.php @@ -53,7 +53,7 @@ class VisitsStatsHelperTest extends TestCase public function returnsExpectedVisitsStats(int $expectedCount): void { $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( $expectedCount, );