CommentaireManager.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Commentaire;
  4. use App\Entity\Film;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\SecurityBundle\Security;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. /**
  9. * Class CommentaireManager
  10. */
  11. class CommentaireManager {
  12. protected ?UserInterface $user;
  13. public function __construct(
  14. protected EntityManagerInterface $em,
  15. protected UniciteCollections $uc,
  16. Security $security
  17. )
  18. {
  19. $this->user = $security->getUser();
  20. }
  21. public function addCommentaire (Commentaire $commentaire, Film $film): void
  22. {
  23. $commentaire->setUser($this->user);
  24. $commentaire->setFilm(($film));
  25. $film->addCommentaire($commentaire);
  26. $this->em->persist($commentaire);
  27. $this->em->flush();
  28. }
  29. public function editCommentaire(Commentaire $commentaire): void
  30. {
  31. $this->em->flush();
  32. }
  33. public function delCommentaire(Commentaire $commentaire): void
  34. {
  35. $commentaire->getFilm()->removeCommentaire($commentaire);
  36. $this->em->remove($commentaire);
  37. $this->em->flush();
  38. }
  39. private function isEmpty (Commentaire $commentaire): bool
  40. {
  41. return (
  42. is_null($commentaire->getContenu()) &&
  43. is_null($commentaire->getNote()));
  44. }
  45. public function delEditAdd(Commentaire $commentaire, Film $film): void
  46. {
  47. if (!$this->isEmpty($commentaire))
  48. {
  49. if (!is_null($commentaire->getId()))
  50. {
  51. $this->editCommentaire($commentaire);
  52. return;
  53. } else {
  54. $this->addCommentaire($commentaire, $film);
  55. return;
  56. }
  57. } else {
  58. if (!is_null($commentaire->getId()))
  59. {
  60. $this->delCommentaire($commentaire);
  61. return;
  62. }
  63. }
  64. /*if ($this->isEmpty($commentaire)) {
  65. if (!is_null($commentaire->getId())) {
  66. $this->delCommentaire($commentaire);
  67. return;
  68. } else {
  69. $this->em->flush();
  70. return;
  71. }
  72. } else {
  73. $cm->addCommentaire($commentaireUser, $film);
  74. return;
  75. }*/
  76. }
  77. }