Address PHPStan issues
This commit is contained in:
parent
5821209e4d
commit
dc6c4a082d
4 changed files with 83 additions and 12 deletions
|
|
@ -4,6 +4,7 @@ namespace App\Controller\Api;
|
||||||
|
|
||||||
use App\Entity\Rating;
|
use App\Entity\Rating;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
|
use App\Presenter\RatingPresenter;
|
||||||
use App\Repository\RatingRepository;
|
use App\Repository\RatingRepository;
|
||||||
use App\Repository\SubjectRepository;
|
use App\Repository\SubjectRepository;
|
||||||
use App\Services\OutputService;
|
use App\Services\OutputService;
|
||||||
|
|
@ -20,19 +21,10 @@ class RatingController extends AbstractController
|
||||||
public function index(
|
public function index(
|
||||||
RatingRepository $repo,
|
RatingRepository $repo,
|
||||||
OutputService $output,
|
OutputService $output,
|
||||||
|
RatingPresenter $ratingPresenter,
|
||||||
): JsonResponse {
|
): JsonResponse {
|
||||||
return $this->json($output->success([
|
return $this->json($output->success([
|
||||||
'ratings' => array_map(
|
'ratings' => $ratingPresenter->presentMany($repo->findAll()),
|
||||||
fn(Rating $rating) => [
|
|
||||||
'id' => $rating->getId(),
|
|
||||||
'score' => $rating->getScore(),
|
|
||||||
'subject' => [
|
|
||||||
'id' => $rating->getSubject()->getId(),
|
|
||||||
'name' => $rating->getSubject()->getName(),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
$repo->findAll()
|
|
||||||
),
|
|
||||||
]), 200);
|
]), 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Controller\Api;
|
||||||
|
|
||||||
use App\Entity\Restaurant;
|
use App\Entity\Restaurant;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
|
use App\Presenter\SubjectPresenter;
|
||||||
use App\Repository\SubjectRepository;
|
use App\Repository\SubjectRepository;
|
||||||
use App\Services\OutputService;
|
use App\Services\OutputService;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
@ -19,8 +20,11 @@ class SubjectController extends AbstractController
|
||||||
public function index(
|
public function index(
|
||||||
SubjectRepository $repo,
|
SubjectRepository $repo,
|
||||||
OutputService $output,
|
OutputService $output,
|
||||||
|
SubjectPresenter $subjectPresenter,
|
||||||
): JsonResponse {
|
): JsonResponse {
|
||||||
return $this->json($output->success(['subjects' => $repo->findAll()]));
|
return $this->json($output->success([
|
||||||
|
'subjects' => $subjectPresenter->presentMany($repo->findAll()),
|
||||||
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/api/subjects', methods: ['POST'])]
|
#[Route('/api/subjects', methods: ['POST'])]
|
||||||
|
|
|
||||||
33
src/Presenter/RatingPresenter.php
Normal file
33
src/Presenter/RatingPresenter.php
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Presenter;
|
||||||
|
|
||||||
|
use App\Entity\Rating;
|
||||||
|
|
||||||
|
class RatingPresenter
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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<Rating> $ratings
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
public function presentMany(iterable $ratings): array
|
||||||
|
{
|
||||||
|
return array_map(fn(Rating $r) => $this->present($r), [...$ratings]);
|
||||||
|
}
|
||||||
|
}
|
||||||
42
src/Presenter/SubjectPresenter.php
Normal file
42
src/Presenter/SubjectPresenter.php
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Presenter;
|
||||||
|
|
||||||
|
use App\Entity\Subject;
|
||||||
|
|
||||||
|
class SubjectPresenter
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private RatingPresenter $ratingPresenter,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
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<Subject> $subjects
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
*/
|
||||||
|
public function presentMany(iterable $subjects): array
|
||||||
|
{
|
||||||
|
return array_map(fn($s) => $this->present($s), [...$subjects]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue