Skip to content

Commit 5e23f58

Browse files
committed
Filter unpopular mirrors
1 parent 0a74204 commit 5e23f58

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎api/src/Repository/MirrorRepository.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class MirrorRepository extends ServiceEntityRepository
1616
{
17+
public const int MIN_POPULARITY = 5;
18+
1719
public function __construct(ManagerRegistry $registry)
1820
{
1921
parent::__construct($registry, Mirror::class);
@@ -29,15 +31,19 @@ public function getCountByUrlAndRange(string $url, int $startMonth, int $endMont
2931
$queryBuilder
3032
->select('mirror.count')
3133
->andWhere('mirror.month = :month')
32-
->setParameter('month', $startMonth);
34+
->setParameter('month', $startMonth)
35+
->andWhere('mirror.count >= :minPopularity')
36+
->setParameter('minPopularity', self::MIN_POPULARITY);
3337
} else {
3438
$queryBuilder
35-
->select('SUM(mirror.count)')
39+
->select('SUM(mirror.count) AS mirror_count')
3640
->andWhere('mirror.month >= :startMonth')
3741
->andWhere('mirror.month <= :endMonth')
3842
->groupBy('mirror.url')
3943
->setParameter('startMonth', $startMonth)
40-
->setParameter('endMonth', $endMonth);
44+
->setParameter('endMonth', $endMonth)
45+
->having('mirror_count >= :minPopularity')
46+
->setParameter('minPopularity', self::MIN_POPULARITY);
4147
}
4248

4349
try {

‎api/tests/Controller/ApiMirrorsControllerTest.php‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Controller\ApiMirrorsController;
66
use App\Entity\Mirror;
7+
use App\Repository\MirrorRepository;
78
use PHPUnit\Framework\Attributes\CoversClass;
89
use PHPUnit\Framework\Attributes\DataProvider;
910
use Symfony\Component\HttpFoundation\Response;
@@ -16,8 +17,7 @@ class ApiMirrorsControllerTest extends DatabaseTestCase
1617
public function testFetchAllMirrors(string $mirrorUrl): void
1718
{
1819
$entityManager = $this->getEntityManager();
19-
$mirror = new Mirror($mirrorUrl)
20-
->setMonth((int)new \DateTime()->format('Ym'));
20+
$mirror = $this->createPopularMirror($mirrorUrl, (int)new \DateTime()->format('Ym'));
2121
$entityManager->persist($mirror);
2222
$entityManager->flush();
2323

@@ -217,6 +217,17 @@ public function testMirrorsSeries(string $mirrorUrl): void
217217
$this->assertEquals($mirrorUrl, $popularityList['mirrorPopularities'][0]['url']);
218218
}
219219

220+
private function createPopularMirror(string $url, int $month): Mirror
221+
{
222+
$mirror = new Mirror($url)
223+
->setMonth($month);
224+
for ($i = 1; $i < MirrorRepository::MIN_POPULARITY; $i++) {
225+
$mirror->incrementCount();
226+
}
227+
228+
return $mirror;
229+
}
230+
220231
/**
221232
* @return list<string[]>
222233
*/

0 commit comments

Comments
 (0)