| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- declare(strict_types=1);
- namespace App\State\Provider\Freemium;
- use ApiPlatform\Metadata\GetCollection;
- use ApiPlatform\Metadata\Operation;
- use ApiPlatform\State\Pagination\TraversablePaginator;
- use ApiPlatform\State\ProviderInterface;
- use App\ApiResources\Freemium\FreemiumPlace;
- use App\Entity\Place\Place;
- use App\Repository\Place\PlaceRepository;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\ObjectMapper\ObjectMapperInterface;
- /**
- * Class FreemiumPlaceProvider : custom provider pour assurer l'alimentation de la réponse du GET freemium/places/{id}.
- */
- final class FreemiumPlaceProvider implements ProviderInterface
- {
- public function __construct(
- private ObjectMapperInterface $objectMapper,
- private PlaceRepository $placeRepository
- ) {
- }
- /**
- * @param array<mixed> $uriVariables
- * @param array<mixed> $context
- *
- * @throws \Doctrine\ORM\Exception\ORMException
- * @throws \Doctrine\ORM\OptimisticLockException
- */
- public function provide(Operation $operation, array $uriVariables = [], array $context = []): TraversablePaginator|FreemiumPlace|null
- {
- if ($operation instanceof GetCollection) {
- throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
- }
- return $this->provideItem($uriVariables, $context);
- }
- /**
- * @param array<mixed> $uriVariables
- * @param array<mixed> $context
- *
- * @throws \Doctrine\ORM\Exception\ORMException
- * @throws \Doctrine\ORM\OptimisticLockException
- */
- private function provideItem(array $uriVariables, array $context): ?FreemiumPlace
- {
- /** @var Place $place */
- $place = $this->placeRepository->find($uriVariables['id']);
- if(empty($place)){
- throw new NotFoundHttpException('place not found');
- }
- return $this->objectMapper->map($place, FreemiumPlace::class);
- }
- }
|