From dc6c4a082d5aa93c26194b495d207ade07ff557c Mon Sep 17 00:00:00 2001 From: myrmidex Date: Fri, 5 Jun 2026 22:51:24 +0000 Subject: [PATCH] Address PHPStan issues --- src/Controller/Api/RatingController.php | 14 ++------ src/Controller/Api/SubjectController.php | 6 +++- src/Presenter/RatingPresenter.php | 33 +++++++++++++++++++ src/Presenter/SubjectPresenter.php | 42 ++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 src/Presenter/RatingPresenter.php create mode 100644 src/Presenter/SubjectPresenter.php diff --git a/src/Controller/Api/RatingController.php b/src/Controller/Api/RatingController.php index 3623f43..b53c8e9 100644 --- a/src/Controller/Api/RatingController.php +++ b/src/Controller/Api/RatingController.php @@ -4,6 +4,7 @@ namespace App\Controller\Api; use App\Entity\Rating; use App\Entity\User; +use App\Presenter\RatingPresenter; use App\Repository\RatingRepository; use App\Repository\SubjectRepository; use App\Services\OutputService; @@ -20,19 +21,10 @@ class RatingController extends AbstractController public function index( RatingRepository $repo, OutputService $output, + RatingPresenter $ratingPresenter, ): JsonResponse { return $this->json($output->success([ - 'ratings' => array_map( - fn(Rating $rating) => [ - 'id' => $rating->getId(), - 'score' => $rating->getScore(), - 'subject' => [ - 'id' => $rating->getSubject()->getId(), - 'name' => $rating->getSubject()->getName(), - ], - ], - $repo->findAll() - ), + 'ratings' => $ratingPresenter->presentMany($repo->findAll()), ]), 200); } diff --git a/src/Controller/Api/SubjectController.php b/src/Controller/Api/SubjectController.php index fdf4b66..ec09876 100644 --- a/src/Controller/Api/SubjectController.php +++ b/src/Controller/Api/SubjectController.php @@ -4,6 +4,7 @@ namespace App\Controller\Api; use App\Entity\Restaurant; use App\Entity\User; +use App\Presenter\SubjectPresenter; use App\Repository\SubjectRepository; use App\Services\OutputService; use Doctrine\ORM\EntityManagerInterface; @@ -19,8 +20,11 @@ class SubjectController extends AbstractController public function index( SubjectRepository $repo, OutputService $output, + SubjectPresenter $subjectPresenter, ): JsonResponse { - return $this->json($output->success(['subjects' => $repo->findAll()])); + return $this->json($output->success([ + 'subjects' => $subjectPresenter->presentMany($repo->findAll()), + ])); } #[Route('/api/subjects', methods: ['POST'])] diff --git a/src/Presenter/RatingPresenter.php b/src/Presenter/RatingPresenter.php new file mode 100644 index 0000000..4fcac20 --- /dev/null +++ b/src/Presenter/RatingPresenter.php @@ -0,0 +1,33 @@ + + */ + public function present(Rating $rating): array + { + return [ + 'id' => $rating->getId(), + 'score' => $rating->getScore(), + 'note' => $rating->getNote(), + 'createdAt' => $rating->getCreatedAt()?->format(\DateTimeInterface::ATOM), + 'user' => [ + 'id' => $rating->getUser()->getId(), + ], + ]; + } + + /** + * @param iterable $ratings + * @return array> + */ + public function presentMany(iterable $ratings): array + { + return array_map(fn(Rating $r) => $this->present($r), [...$ratings]); + } +} diff --git a/src/Presenter/SubjectPresenter.php b/src/Presenter/SubjectPresenter.php new file mode 100644 index 0000000..30e9bb3 --- /dev/null +++ b/src/Presenter/SubjectPresenter.php @@ -0,0 +1,42 @@ + + */ + public function present(Subject $subject): array + { + return [ + 'id' => $subject->getId(), + 'name' => $subject->getName(), + 'latitude' => $subject->getLatitude(), + 'longitude' => $subject->getLongitude(), + 'createdBy' => [ + 'id' => $subject->getCreatedBy()->getId(), + 'email' => $subject->getCreatedBy()->getEmail(), + ], + 'ratings' => array_map( + fn($r) => $this->ratingPresenter->present($r), + $subject->getRatings()->toArray() + ), + ]; + } + + /** + * @param iterable $subjects + * @return array> + */ + public function presentMany(iterable $subjects): array + { + return array_map(fn($s) => $this->present($s), [...$subjects]); + } +}