Skip to content

Commit 2984aa0

Browse files
committed
Split DashboardPackagesController
1 parent 6490798 commit 2984aa0

File tree

4 files changed

+132
-102
lines changed

4 files changed

+132
-102
lines changed

src/Controller/Dashboard/DashboardPackagesController.php

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
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;
1716
use EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDto;
1817
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
@@ -57,87 +56,6 @@ public function list(Request $request): Response
5756
]);
5857
}
5958

60-
#[Route('/dashboard/packages/info/{packageName}/{packageVersion}', name: 'dashboard_packages_info', requirements: ['packageName' => '[a-z0-9_.-]+/[a-z0-9_.-]+'])]
61-
#[IsGrantedAccess]
62-
public function info(string $packageName, ?string $packageVersion = null): Response
63-
{
64-
$package = $this->packageRepository->findOneBy(['name' => $packageName]);
65-
66-
$versions = $package->getVersions()->toArray();
67-
$latestVersion = $package->getDefaultVersion();
68-
69-
usort($versions, Package::class . '::sortVersions');
70-
71-
if (null !== $packageVersion) {
72-
$version = $package->getVersion((new VersionParser())->normalize($packageVersion));
73-
} else {
74-
$version = $package->getLatestVersion();
75-
}
76-
77-
return $this->render('dashboard/packages/package_info.html.twig', [
78-
'package' => $package,
79-
'latestVersion' => $latestVersion,
80-
'version' => $version,
81-
]);
82-
}
83-
84-
#[Route('/dashboard/packages/versions/{packageName}', 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('/dashboard/packages/statistics/{packageName}', 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-
14159
#[Route('/dashboard/packages/add-mirroring', name: 'dashboard_packages_add_mirroring')]
14260
#[IsGranted('ROLE_ADMIN')]
14361
public function addMirroring(Request $request): Response
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_.-]+'])]
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+
}

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', '/?routeName=dashboard_packages_statistics&routeParams[packageName]=psr/log');
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)