Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| FavoritesController | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| add | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| remove | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| removeAll | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Controller\Front; |
| 4 | |
| 5 | use App\Entity\Movie; |
| 6 | use App\Models\MovieModel; |
| 7 | use App\Repository\MovieRepository; |
| 8 | use App\Services\FavoritesService; |
| 9 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 10 | use Symfony\Component\HttpFoundation\Request; |
| 11 | use Symfony\Component\HttpFoundation\Response; |
| 12 | use Symfony\Component\Routing\Annotation\Route; |
| 13 | |
| 14 | class FavoritesController extends AbstractController |
| 15 | { |
| 16 | /** |
| 17 | * afficher le(s) film favoris |
| 18 | * |
| 19 | * @Route("/favorites", name="app_front_favorites") |
| 20 | */ |
| 21 | public function index(FavoritesService $favoritesService): Response |
| 22 | { |
| 23 | |
| 24 | // TODO utiliser le service |
| 25 | $favorisList = $favoritesService->list(); |
| 26 | |
| 27 | return $this->render('front/favorites/index.html.twig', |
| 28 | [ |
| 29 | // je donne les films favoris à ma vue pour l'affichage |
| 30 | "movies" => $favorisList |
| 31 | ]); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * ajout un film en favoris |
| 36 | * |
| 37 | * @Route("/favorites/add/{id}", name="app_front_favorites_add", requirements={"id"="\d+"}) |
| 38 | * |
| 39 | * @return Response |
| 40 | */ |
| 41 | public function add($id, MovieRepository $movieRepository, FavoritesService $favoritesService): Response |
| 42 | { |
| 43 | // TODO : j'ai besoin de l'identifiant du film à mettre en favoris |
| 44 | // ? comment l'utilisateur me fournit l'ID ? |
| 45 | // avec un paramètre de route : {id} |
| 46 | // dd($id); |
| 47 | |
| 48 | // TODO : j'ai besoin des informations du film en question |
| 49 | $movie = $movieRepository->find($id); |
| 50 | |
| 51 | if ($movie === null){ throw $this->createNotFoundException("ce film n'existe pas.");} |
| 52 | |
| 53 | // TODO utiliser le service |
| 54 | $favoritesService->add($movie); |
| 55 | |
| 56 | // ? j'ai fini le traitement, je n'ai rien à afficher de particulier |
| 57 | // je vais donc rediriger mon utilisateur vers l'affichage des favoris |
| 58 | // càd vers une autre route |
| 59 | // la méthode redirectToRoute() me fournit une Response |
| 60 | // je renvois de suite cette response |
| 61 | return $this->redirectToRoute('app_front_favorites'); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * supression d'un favoris |
| 66 | * |
| 67 | * @Route("/favorites/remove/{id}", name="app_front_favorites_remove", requirements={"id"="\d+"}) |
| 68 | * |
| 69 | * @return Response |
| 70 | */ |
| 71 | public function remove($id, FavoritesService $favoritesService, MovieRepository $movieRepository):Response |
| 72 | { |
| 73 | $movie = $movieRepository->find($id); |
| 74 | if ($movie === null){ throw $this->createNotFoundException("ce film n'existe pas.");} |
| 75 | |
| 76 | $favoritesService->remove($movie); |
| 77 | |
| 78 | // on redirige pour l'affichage |
| 79 | return $this->redirectToRoute('app_front_favorites'); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * supprime tout les favoris |
| 84 | * |
| 85 | * @Route("favorites/clear", name="app_front_favorites_clear") |
| 86 | * |
| 87 | * @param FavoritesService $favoritesService |
| 88 | * @return Response |
| 89 | */ |
| 90 | public function removeAll(FavoritesService $favoritesService): Response |
| 91 | { |
| 92 | $favoritesService->removeAll(); |
| 93 | |
| 94 | // on redirige pour l'affichage |
| 95 | return $this->redirectToRoute('app_front_favorites'); |
| 96 | |
| 97 | } |
| 98 | } |