Skip to content

Instantly share code, notes, and snippets.

@Zayon
Last active December 31, 2018 13:36
Show Gist options
  • Save Zayon/62b0b44d11f72f9ea1a9cd9b33b91371 to your computer and use it in GitHub Desktop.
Save Zayon/62b0b44d11f72f9ea1a9cd9b33b91371 to your computer and use it in GitHub Desktop.
Fixtures - ListProductsAction
<?php
namespace App\Action;
// use statements omitted for concision
class ListProductsAction
{
// Properties & constructor omitted for concision
public function __invoke(Request $request): JsonResponse
{
$filter = [];
$categoryName = $request->query->get('category');
if (null !== $categoryName) {
$category = $this->productCategoryRepository->findOneBy(['name' => $categoryName]);
$filter = ['category' => $category];
}
$products = $this->productRepository->findBy($filter);
$response = [
'type' => 'list:Product',
'items' => $this->normalizer->normalize($products, 'json'),
'total' => \count($products),
];
return new JsonResponse($response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment