diff --git a/src/Controller/PackageController.php b/src/Controller/PackageController.php index d4caa0b5b..5d57dd755 100644 --- a/src/Controller/PackageController.php +++ b/src/Controller/PackageController.php @@ -14,6 +14,7 @@ use App\Entity\Dependent; use App\Entity\PackageFreezeReason; +use App\Entity\PackageRepository; use App\Entity\PhpStat; use App\Security\Voter\PackageActions; use App\SecurityAdvisory\GitHubSecurityAdvisoriesSource; @@ -54,6 +55,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\Attribute\Route; @@ -97,19 +99,21 @@ public function allAction(): RedirectResponse } #[Route(path: '/packages/list.json', name: 'list', defaults: ['_format' => 'json'], methods: ['GET'])] - public function listAction(Request $req): JsonResponse + public function listAction(Request $req, + PackageRepository $repo, + #[MapQueryParameter] ?string $type=null, + #[MapQueryParameter] ?string $vendor=null, + ): JsonResponse { - $repo = $this->getEM()->getRepository(Package::class); $queryParams = $req->query->all(); $fields = (array) ($queryParams['fields'] ?? []); // support single or multiple fields - /** @var string[] $fields */ $fields = array_intersect($fields, ['repository', 'type', 'abandoned']); if (count($fields) > 0) { $filters = array_filter([ - 'type' => $req->query->get('type'), - 'vendor' => $req->query->get('vendor'), - ]); + 'type' => $type, + 'vendor' => $vendor, + ], fn ($val) => $val !== null); $response = new JsonResponse(['packages' => $repo->getPackagesWithFields($filters, $fields)]); $response->setSharedMaxAge(300); @@ -118,10 +122,8 @@ public function listAction(Request $req): JsonResponse return $response; } - if ($req->query->get('type')) { - $names = $repo->getPackageNamesByType($req->query->get('type')); - } elseif ($req->query->get('vendor')) { - $names = $repo->getPackageNamesByVendor($req->query->get('vendor')); + if ($type !== null || $vendor !== null) { + $names = $repo->getPackageNamesByTypeAndVendor($type, $vendor); } else { $names = $this->providerManager->getPackageNames(); } diff --git a/src/DataFixtures/PackageFixtures.php b/src/DataFixtures/PackageFixtures.php index bcea26aca..2417f76c9 100644 --- a/src/DataFixtures/PackageFixtures.php +++ b/src/DataFixtures/PackageFixtures.php @@ -154,6 +154,8 @@ private function getPackages(): array ['https://github.com/thephpleague/flysystem', '2014-01-15T07:46:47+00:00'], ['https://github.com/twigphp/Twig', '2011-09-29T16:52:42+00:00'], ['https://github.com/webmozarts/assert', '2015-03-11T12:18:50+00:00'], + ['https://github.com/zenstruck/schedule-bundle', '2022-03-11T12:18:50+00:00'], + ['https://github.com/zenstruck/signed-url-bundle', '2022-03-11T12:18:50+00:00'], ]; } } diff --git a/src/Entity/PackageRepository.php b/src/Entity/PackageRepository.php index fa8511e42..b5a1f9263 100644 --- a/src/Entity/PackageRepository.php +++ b/src/Entity/PackageRepository.php @@ -99,25 +99,21 @@ public function getProvidedNames(): array /** * @return array */ - public function getPackageNamesByType(string $type): array - { - $query = $this->getEntityManager() - ->createQuery("SELECT p.name FROM App\Entity\Package p WHERE p.type = :type AND p.frozen IS NULL") - ->setParameters(['type' => $type]); - - return $this->getPackageNamesForQuery($query); - } - - /** - * @return array - */ - public function getPackageNamesByVendor(string $vendor): array - { - $query = $this->getEntityManager() - ->createQuery("SELECT p.name FROM App\Entity\Package p WHERE p.vendor = :vendor AND p.frozen IS NULL") - ->setParameters(['vendor' => $vendor]); + public function getPackageNamesByTypeAndVendor(?string $type, ?string $vendor): array + { + $qb = $this->getEntityManager()->getRepository(Package::class)->createQueryBuilder('p') + ->select('p.name') + ->where('p.frozen IS NULL'); + if ($type !== null) { + $qb->andWhere('p.type = :type') + ->setParameter('type', $type); + } + if ($vendor !== null) { + $qb->andWhere('p.vendor = :vendor') + ->setParameter('vendor', $vendor); + } - return $this->getPackageNamesForQuery($query); + return $this->getPackageNamesForQuery($qb->getQuery()); } /**