FilmManager.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Film;
  4. use App\Entity\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. /**
  8. * Class FilmManager
  9. */
  10. class FilmManager {
  11. protected $user;
  12. public function __construct(
  13. protected EntityManagerInterface $em,
  14. protected UniciteCollections $uc,
  15. protected CommentaireManager $cm,
  16. Security $security
  17. )
  18. {
  19. $this->user = $security->getUser();
  20. }
  21. public function addFilm (Film $film): void
  22. {
  23. $film->setAuthered($this->user);
  24. $film = $this->uc->assureUniciteCollections($film);
  25. $this->em->persist($film);
  26. $this->em->flush();
  27. }
  28. public function editFilm(Film $film): void
  29. {
  30. $film = $this->uc->assureUniciteCollections($film);
  31. $this->em->flush();
  32. }
  33. public function delFilm(Film $film): void
  34. {
  35. $commentaires = $film->getCommentaires();
  36. foreach($commentaires as $commentaire)
  37. {
  38. $this->cm->delCommentaire($commentaire);
  39. }
  40. $this->em->remove($film);
  41. $this->em->flush();
  42. }
  43. public function inverseUserWhoSeen (Film $film): bool
  44. {
  45. if ($film->getUsersWhoSeen()->contains($this->user)) {
  46. $film->removeUserWhoSeen($this->user);
  47. } else {
  48. $film->addUserWhoSeen($this->user);
  49. }
  50. return $this->isSeen($film);
  51. }
  52. public function inverseUserWantToView (Film $film): bool
  53. {
  54. if ($film->getUsersWantToView()->contains($this->user)) {
  55. $film->removeUserWantToView($this->user);
  56. } else {
  57. $film->addUserWantToView($this->user);
  58. }
  59. return $this->isWantedToBeSeen($film);
  60. }
  61. public function isSeen(Film $film, User $user = null): bool
  62. {
  63. $user = $user ?: $this->user;
  64. return $film->getUsersWhoSeen()->contains($user);
  65. }
  66. public function isWantedToBeSeen(Film $film, User $user = null): bool
  67. {
  68. $user = $user ?: $this->user;
  69. return $film->getUsersWantToView()->contains($user);
  70. }
  71. }