Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MovieController
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 browse
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 read
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 add
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Controller\Api;
4
5use App\Entity\Movie;
6use App\Repository\MovieRepository;
7use Doctrine\ORM\EntityNotFoundException;
8use Exception;
9use Symfony\Component\HttpFoundation\JsonResponse;
10use Symfony\Component\HttpFoundation\Request;
11use Symfony\Component\HttpFoundation\Response;
12use Symfony\Component\Routing\Annotation\Route;
13use Symfony\Component\Serializer\SerializerInterface;
14use Symfony\Component\Validator\Validator\ValidatorInterface;
15use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
16
17/**
18 * @Route("/api/movies", name="app_api_movie_")
19 */
20class MovieController extends CoreApiController
21{
22    /**
23     * list all movies
24     * 
25     * @Route("",name="browse", methods={"GET"})
26     *
27     * @param MovieRepository $movieRepository
28     * @return JsonResponse
29     */
30    public function browse(MovieRepository $movieRepository): JsonResponse
31    {
32        $allMovies = $movieRepository->findAll();
33        return $this->json200($allMovies, ["movie_browse"]);
34    }
35    /**
36     * @Route("/{id}", name="read", requirements={"id"="\d+"}, methods={"GET"})
37     */
38    // public function read(?Movie $movie,MovieRepository $movieRepository): JsonResponse
39    public function read($id,MovieRepository $movieRepository): JsonResponse
40    {
41        $movie = $movieRepository->find($id);
42        // gestion 404
43        if ($movie === null){
44            // ! on est dans une API donc pas de HTML
45            // throw $this->createNotFoundException();
46            return $this->json(
47                // on pense UX : on fournit un message
48                [
49                    "message" => "Ce film n'existe pas"
50                ],
51                // le code de status : 404
52                Response::HTTP_NOT_FOUND
53                // on a pas besoin de preciser les autres arguments
54            );
55        }
56        return $this->json($movie, 200, [], 
57            [
58                "groups" => 
59                [
60                    "movie_read"
61                ]
62            ]);
63    }
64
65    /**
66     * ajout de film
67     *
68     * @Route("",name="add", methods={"POST"})
69     * 
70     * @param Request $request
71     * @param SerializerInterface $serializer
72     * @param MovieRepository $movieRepository
73     * 
74     * @ IsGranted("ROLE_ADMIN")
75     */
76    public function add(
77        Request $request, 
78        SerializerInterface $serializer, 
79        MovieRepository $movieRepository,
80        ValidatorInterface $validatorInterface)
81    {
82        // Récupérer le contenu JSON
83        $jsonContent = $request->getContent();
84        
85        // Désérialiser (convertir) le JSON en entité Doctrine Movie
86        try { // on tente de désérialiser
87            $movie = $serializer->deserialize($jsonContent, Movie::class, 'json');
88        } catch (EntityNotFoundException $e){
89            // spécial pour DoctrineDenormalizer
90            return $this->json("Denormalisation : ". $e->getMessage(), Response::HTTP_BAD_REQUEST);
91        } catch (Exception $exception){
92            // Si on n'y arrive pas, on passe ici
93            // dd($exception);
94            // code 400 ou 422
95            return $this->json("JSON Invalide : " . $exception->getMessage(), Response::HTTP_BAD_REQUEST);
96        }
97        
98        // on valide les données de notre entité
99        // ? https://symfony.com/doc/5.4/validation.html#using-the-validator-service
100        $errors = $validatorInterface->validate($movie);
101        // Y'a-t-il des erreurs ?
102        if (count($errors) > 0) {
103            // TODO Retourner des erreurs de validation propres
104            return $this->json($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
105        }
106
107        // On sauvegarde l'entité
108        $movieRepository->add($movie, true);
109
110        return $this->json($movie, Response::HTTP_CREATED, [], ["groups"=>["movie_read"]]);
111    }
112}