Last active
December 31, 2018 13:36
-
-
Save Zayon/62b0b44d11f72f9ea1a9cd9b33b91371 to your computer and use it in GitHub Desktop.
Fixtures - ListProductsAction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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