Skip to content

Commit 017c094

Browse files
committed
Split DashboardPackagesController
1 parent 93b4d4e commit 017c094

File tree

5 files changed

+180
-122
lines changed

5 files changed

+180
-122
lines changed

src/Controller/Dashboard/DashboardPackagesController.php

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
use CodedMonkey\Dirigent\Form\PackageFormType;
1313
use CodedMonkey\Dirigent\Message\UpdatePackage;
1414
use CodedMonkey\Dirigent\Package\PackageMetadataResolver;
15-
use Composer\Semver\VersionParser;
1615
use Doctrine\ORM\EntityManagerInterface;
17-
use Doctrine\ORM\QueryBuilder;
18-
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityPaginatorInterface;
19-
use EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDto;
2016
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2117
use Symfony\Component\HttpFoundation\Request;
2218
use Symfony\Component\HttpFoundation\Response;
@@ -46,7 +42,7 @@ public function list(Request $request): Response
4642
$queryBuilder->setParameter('query', "%{$query}%");
4743
}
4844

49-
$paginator = $this->createPackagePaginator($request, $queryBuilder);
45+
$paginator = PackagePaginator::fromRequest($request, $queryBuilder, $this->container->get('router'));
5046
$packages = $paginator->getResults();
5147

5248
return $this->render('dashboard/packages/list.html.twig', [
@@ -55,89 +51,6 @@ public function list(Request $request): Response
5551
]);
5652
}
5753

58-
#[Route('/packages/{packageName}', name: 'dashboard_packages_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
59-
#[IsGrantedAccess]
60-
public function info(string $packageName): Response
61-
{
62-
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
63-
$version = $package->getLatestVersion();
64-
65-
return $this->render('dashboard/packages/package_info.html.twig', [
66-
'package' => $package,
67-
'version' => $version,
68-
]);
69-
}
70-
71-
#[Route('/packages/{packageName}/v/{packageVersion}', name: 'dashboard_packages_version_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
72-
#[IsGrantedAccess]
73-
public function versionInfo(string $packageName, string $packageVersion): Response
74-
{
75-
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
76-
$version = $package->getVersion((new VersionParser())->normalize($packageVersion));
77-
78-
return $this->render('dashboard/packages/package_info.html.twig', [
79-
'package' => $package,
80-
'version' => $version,
81-
]);
82-
}
83-
84-
#[Route('/packages/{packageName}/versions', name: 'dashboard_packages_versions', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
85-
#[IsGrantedAccess]
86-
public function versions(string $packageName): Response
87-
{
88-
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
89-
$versions = $package->getVersions()->toArray();
90-
91-
usort($versions, Package::class . '::sortVersions');
92-
93-
return $this->render('dashboard/packages/package_versions.html.twig', [
94-
'package' => $package,
95-
'versions' => $versions,
96-
]);
97-
}
98-
99-
#[Route('/packages/{packageName}/statistics', name: 'dashboard_packages_statistics', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
100-
#[IsGrantedAccess]
101-
public function statistics(string $packageName): Response
102-
{
103-
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
104-
105-
$versionInstallationsData = [];
106-
107-
foreach ($package->getVersions() as $version) {
108-
$majorVersion = $version->getMajorVersion();
109-
110-
$versionInstallationsData[$majorVersion] ??= [];
111-
112-
foreach ($version->getInstallations()->getData() as $key => $installations) {
113-
$versionInstallationsData[$majorVersion][$key] ??= 0;
114-
$versionInstallationsData[$majorVersion][$key] += $installations;
115-
}
116-
}
117-
118-
$today = new \DateTimeImmutable();
119-
$todayKey = $today->format('Ymd');
120-
$installationsToday = $package->getInstallations()->getData()[$todayKey] ?? 0;
121-
122-
$installationsLast30Days = 0;
123-
$date = new \DateTimeImmutable('-30 days');
124-
125-
while ($date <= $today) {
126-
$dateKey = $date->format('Ymd');
127-
$installationsLast30Days += $package->getInstallations()->getData()[$dateKey] ?? 0;
128-
129-
$date = $date->modify('+1 day');
130-
}
131-
132-
return $this->render('dashboard/packages/package_statistics.html.twig', [
133-
'package' => $package,
134-
'versionInstallationsData' => $versionInstallationsData,
135-
'installationsTotal' => $package->getInstallations()->getTotal(),
136-
'installationsLast30Days' => $installationsLast30Days,
137-
'installationsToday' => $installationsToday,
138-
]);
139-
}
140-
14154
#[Route('/packages/add-mirroring', name: 'dashboard_packages_add_mirroring')]
14255
#[IsGranted('ROLE_ADMIN')]
14356
public function addMirroring(Request $request): Response
@@ -291,18 +204,4 @@ public function delete(string $packageName): Response
291204

292205
return $this->redirectToRoute('dashboard_packages');
293206
}
294-
295-
private function createPackagePaginator(Request $request, QueryBuilder $queryBuilder): EntityPaginatorInterface
296-
{
297-
$paginatorDto = new PaginatorDto(20, 3, 1, true, null);
298-
$paginatorDto->setPageNumber($request->query->getInt('page', 1));
299-
300-
$paginator = new PackagePaginator(
301-
$this->container->get('router'),
302-
$request->attributes->get('_route'),
303-
$request->attributes->get('_route_params'),
304-
);
305-
306-
return $paginator->paginate($paginatorDto, $queryBuilder);
307-
}
308207
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Controller\Dashboard;
4+
5+
use CodedMonkey\Dirigent\Attribute\IsGrantedAccess;
6+
use CodedMonkey\Dirigent\Doctrine\Entity\Package;
7+
use CodedMonkey\Dirigent\Doctrine\Repository\PackageRepository;
8+
use Composer\Semver\VersionParser;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\Routing\Attribute\Route;
12+
13+
class DashboardPackagesInfoController extends AbstractController
14+
{
15+
public function __construct(
16+
private readonly PackageRepository $packageRepository,
17+
) {
18+
}
19+
20+
#[Route('/packages/{packageName}', name: 'dashboard_packages_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
21+
#[IsGrantedAccess]
22+
public function info(string $packageName): Response
23+
{
24+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
25+
$version = $package->getLatestVersion();
26+
27+
return $this->render('dashboard/packages/package_info.html.twig', [
28+
'package' => $package,
29+
'version' => $version,
30+
]);
31+
}
32+
33+
#[Route('/packages/{packageName}/v/{packageVersion}', name: 'dashboard_packages_version_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
34+
#[IsGrantedAccess]
35+
public function versionInfo(string $packageName, string $packageVersion): Response
36+
{
37+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
38+
$version = $package->getVersion((new VersionParser())->normalize($packageVersion));
39+
40+
return $this->render('dashboard/packages/package_info.html.twig', [
41+
'package' => $package,
42+
'version' => $version,
43+
]);
44+
}
45+
46+
#[Route('/packages/{packageName}/versions', name: 'dashboard_packages_versions', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
47+
#[IsGrantedAccess]
48+
public function versions(string $packageName): Response
49+
{
50+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
51+
$versions = $package->getVersions()->toArray();
52+
53+
usort($versions, Package::class . '::sortVersions');
54+
55+
return $this->render('dashboard/packages/package_versions.html.twig', [
56+
'package' => $package,
57+
'versions' => $versions,
58+
]);
59+
}
60+
61+
#[Route('/packages/{packageName}/statistics', name: 'dashboard_packages_statistics', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
62+
#[IsGrantedAccess]
63+
public function statistics(string $packageName): Response
64+
{
65+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
66+
67+
$versionInstallationsData = [];
68+
69+
foreach ($package->getVersions() as $version) {
70+
$majorVersion = $version->getMajorVersion();
71+
72+
$versionInstallationsData[$majorVersion] ??= [];
73+
74+
foreach ($version->getInstallations()->getData() as $key => $installations) {
75+
$versionInstallationsData[$majorVersion][$key] ??= 0;
76+
$versionInstallationsData[$majorVersion][$key] += $installations;
77+
}
78+
}
79+
80+
$today = new \DateTimeImmutable();
81+
$todayKey = $today->format('Ymd');
82+
$installationsToday = $package->getInstallations()->getData()[$todayKey] ?? 0;
83+
84+
$installationsLast30Days = 0;
85+
$date = new \DateTimeImmutable('-30 days');
86+
87+
while ($date <= $today) {
88+
$dateKey = $date->format('Ymd');
89+
$installationsLast30Days += $package->getInstallations()->getData()[$dateKey] ?? 0;
90+
91+
$date = $date->modify('+1 day');
92+
}
93+
94+
return $this->render('dashboard/packages/package_statistics.html.twig', [
95+
'package' => $package,
96+
'versionInstallationsData' => $versionInstallationsData,
97+
'installationsTotal' => $package->getInstallations()->getTotal(),
98+
'installationsLast30Days' => $installationsLast30Days,
99+
'installationsToday' => $installationsToday,
100+
]);
101+
}
102+
}

src/EasyAdmin/PackagePaginator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\ORM\Tools\Pagination\Paginator;
77
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityPaginatorInterface;
88
use EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDto;
9+
use Symfony\Component\HttpFoundation\Request;
910
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1011

1112
class PackagePaginator implements EntityPaginatorInterface
@@ -52,6 +53,20 @@ public function paginate(PaginatorDto $paginatorDto, QueryBuilder $queryBuilder)
5253
return $this;
5354
}
5455

56+
public static function fromRequest(Request $request, QueryBuilder $queryBuilder, UrlGeneratorInterface $router): EntityPaginatorInterface
57+
{
58+
$paginatorDto = new PaginatorDto(20, 3, 1, true, null);
59+
$paginatorDto->setPageNumber($request->query->getInt('page', 1));
60+
61+
$paginator = new self(
62+
$router,
63+
$request->attributes->get('_route'),
64+
$request->attributes->get('_route_params'),
65+
);
66+
67+
return $paginator->paginate($paginatorDto, $queryBuilder);
68+
}
69+
5570
public function generateUrlForPage(int $page): string
5671
{
5772
return $this->router->generate($this->routeName, [...$this->routeParameters, 'page' => $page]);

tests/FunctionalTests/Controller/Dashboard/DashboardPackagesControllerTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,6 @@ class DashboardPackagesControllerTest extends WebTestCase
1111
{
1212
use WebTestCaseTrait;
1313

14-
public function testStatistics(): void
15-
{
16-
$client = static::createClient();
17-
$this->loginUser();
18-
19-
$client->request('GET', '/packages/psr/log/statistics');
20-
21-
$this->assertResponseStatusCodeSame(200);
22-
23-
/** @var PackageRepository $packageRepository */
24-
$packageRepository = $client->getContainer()->get(PackageRepository::class);
25-
26-
$package = $packageRepository->findOneByName('psr/log');
27-
28-
$this->assertAnySelectorTextSame('#total_all .display-6', number_format($package->getInstallations()->getTotal(), thousands_separator: ' '));
29-
30-
$todayKey = (new \DateTimeImmutable())->format('Ymd');
31-
$this->assertAnySelectorTextSame('#total_today .display-6', number_format($package->getInstallations()->getData()[$todayKey] ?? 0, thousands_separator: ' '));
32-
}
33-
3414
public function testAddMirroring(): void
3515
{
3616
$client = static::createClient();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace CodedMonkey\Dirigent\Tests\FunctionalTests\Controller\Dashboard;
4+
5+
use CodedMonkey\Dirigent\Doctrine\Repository\PackageRepository;
6+
use CodedMonkey\Dirigent\Tests\FunctionalTests\WebTestCaseTrait;
7+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8+
9+
class DashboardPackagesInfoControllerTest extends WebTestCase
10+
{
11+
use WebTestCaseTrait;
12+
13+
public function testInfo(): void
14+
{
15+
$client = static::createClient();
16+
$this->loginUser();
17+
18+
$client->request('GET', '/packages/psr/log');
19+
20+
$this->assertResponseStatusCodeSame(200);
21+
}
22+
23+
public function testPackageInfo(): void
24+
{
25+
$client = static::createClient();
26+
$this->loginUser();
27+
28+
$client->request('GET', '/packages/psr/log/v/1.0.0');
29+
30+
$this->assertResponseStatusCodeSame(200);
31+
}
32+
33+
public function testVersions(): void
34+
{
35+
$client = static::createClient();
36+
$this->loginUser();
37+
38+
$client->request('GET', '/packages/psr/log/versions');
39+
40+
$this->assertResponseStatusCodeSame(200);
41+
}
42+
43+
public function testStatistics(): void
44+
{
45+
$client = static::createClient();
46+
$this->loginUser();
47+
48+
$client->request('GET', '/packages/psr/log/statistics');
49+
50+
$this->assertResponseStatusCodeSame(200);
51+
52+
/** @var PackageRepository $packageRepository */
53+
$packageRepository = $client->getContainer()->get(PackageRepository::class);
54+
55+
$package = $packageRepository->findOneByName('psr/log');
56+
57+
$this->assertAnySelectorTextSame('#total_all .display-6', number_format($package->getInstallations()->getTotal(), thousands_separator: ' '));
58+
59+
$todayKey = (new \DateTimeImmutable())->format('Ymd');
60+
$this->assertAnySelectorTextSame('#total_today .display-6', number_format($package->getInstallations()->getData()[$todayKey] ?? 0, thousands_separator: ' '));
61+
}
62+
}

0 commit comments

Comments
 (0)