Skip to content

Commit af082ac

Browse files
committed
Split DashboardPackagesController
1 parent 93b4d4e commit af082ac

5 files changed

Lines changed: 148 additions & 122 deletions

File tree

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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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('/dashboard/packages/info/{packageName}/{packageVersion}', name: 'dashboard_packages_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+', 'packageVersion' => '.+'])]
21+
#[IsGrantedAccess]
22+
public function info(string $packageName, ?string $packageVersion = null): Response
23+
{
24+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
25+
26+
$versions = $package->getVersions()->toArray();
27+
$latestVersion = $package->getDefaultVersion();
28+
29+
usort($versions, Package::class . '::sortVersions');
30+
31+
if (null !== $packageVersion) {
32+
$version = $package->getVersion((new VersionParser())->normalize($packageVersion));
33+
} else {
34+
$version = $package->getLatestVersion();
35+
}
36+
37+
return $this->render('dashboard/packages/package_info.html.twig', [
38+
'package' => $package,
39+
'latestVersion' => $latestVersion,
40+
'version' => $version,
41+
]);
42+
}
43+
44+
#[Route('/dashboard/packages/versions/{packageName}', name: 'dashboard_packages_versions', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
45+
#[IsGrantedAccess]
46+
public function versions(string $packageName): Response
47+
{
48+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
49+
$versions = $package->getVersions()->toArray();
50+
51+
usort($versions, Package::class . '::sortVersions');
52+
53+
return $this->render('dashboard/packages/package_versions.html.twig', [
54+
'package' => $package,
55+
'versions' => $versions,
56+
]);
57+
}
58+
59+
#[Route('/dashboard/packages/statistics/{packageName}', name: 'dashboard_packages_statistics', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
60+
#[IsGrantedAccess]
61+
public function statistics(string $packageName): Response
62+
{
63+
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
64+
65+
$versionInstallationsData = [];
66+
67+
foreach ($package->getVersions() as $version) {
68+
$majorVersion = $version->getMajorVersion();
69+
70+
$versionInstallationsData[$majorVersion] ??= [];
71+
72+
foreach ($version->getInstallations()->getData() as $key => $installations) {
73+
$versionInstallationsData[$majorVersion][$key] ??= 0;
74+
$versionInstallationsData[$majorVersion][$key] += $installations;
75+
}
76+
}
77+
78+
$today = new \DateTimeImmutable();
79+
$todayKey = $today->format('Ymd');
80+
$installationsToday = $package->getInstallations()->getData()[$todayKey] ?? 0;
81+
82+
$installationsLast30Days = 0;
83+
$date = new \DateTimeImmutable('-30 days');
84+
85+
while ($date <= $today) {
86+
$dateKey = $date->format('Ymd');
87+
$installationsLast30Days += $package->getInstallations()->getData()[$dateKey] ?? 0;
88+
89+
$date = $date->modify('+1 day');
90+
}
91+
92+
return $this->render('dashboard/packages/package_statistics.html.twig', [
93+
'package' => $package,
94+
'versionInstallationsData' => $versionInstallationsData,
95+
'installationsTotal' => $package->getInstallations()->getTotal(),
96+
'installationsLast30Days' => $installationsLast30Days,
97+
'installationsToday' => $installationsToday,
98+
]);
99+
}
100+
}

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 testStatistics(): void
14+
{
15+
$client = static::createClient();
16+
$this->loginUser();
17+
18+
$client->request('GET', '/?routeName=dashboard_packages_statistics&routeParams[packageName]=psr/log');
19+
20+
$this->assertResponseStatusCodeSame(200);
21+
22+
/** @var PackageRepository $packageRepository */
23+
$packageRepository = $client->getContainer()->get(PackageRepository::class);
24+
25+
$package = $packageRepository->findOneByName('psr/log');
26+
27+
$this->assertAnySelectorTextSame('#total_all .display-6', number_format($package->getInstallations()->getTotal(), thousands_separator: ' '));
28+
29+
$todayKey = (new \DateTimeImmutable())->format('Ymd');
30+
$this->assertAnySelectorTextSame('#total_today .display-6', number_format($package->getInstallations()->getData()[$todayKey] ?? 0, thousands_separator: ' '));
31+
}
32+
}

0 commit comments

Comments
 (0)