Skip to content

Commit

Permalink
refactor list.json to allow for both type and vendor. add 2 packages… (
Browse files Browse the repository at this point in the history
…#1488)

* refactor list.json to allow for both type and vendor.  add 2 packages for testing

* revert  in arguments to make phpstan happy

* Clean ups

* Remove dead code and weak checks

* Fix weak checks

---------

Co-authored-by: Jordi Boggiano <[email protected]>
  • Loading branch information
tacman and Seldaek authored Nov 26, 2024
1 parent 348eb31 commit 5099cb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
22 changes: 12 additions & 10 deletions src/Controller/PackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/DataFixtures/PackageFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}
32 changes: 14 additions & 18 deletions src/Entity/PackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,21 @@ public function getProvidedNames(): array
/**
* @return array<string>
*/
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<string>
*/
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());
}

/**
Expand Down

0 comments on commit 5099cb5

Please sign in to comment.