vendor/roothirsch/notification-bundle/src/Controller/FetchController.php line 66

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\NotificationBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Roothirsch\DamBundle\Entity\Category;
  5. use Roothirsch\DamBundle\Repository\CategoryRepository;
  6. use Roothirsch\NotificationBundle\Entity\NotificationReadOn;
  7. use Roothirsch\NotificationBundle\Repository\NotificationReadOnRepository;
  8. use Roothirsch\NotificationBundle\Repository\NotificationRepository;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. /**
  16. * @Route("/api/notifications", name="api_notifications_")
  17. */
  18. class FetchController extends AbstractController
  19. {
  20. /**
  21. * @var TokenStorageInterface
  22. */
  23. private $tokenStorage;
  24. /**
  25. * @var NotificationRepository
  26. */
  27. private $notificationRepository;
  28. /**
  29. * @var EntityManagerInterface
  30. */
  31. private $entityManager;
  32. /**
  33. * @var NotificationReadOnRepository
  34. */
  35. private $readOnRepository;
  36. /** @var SerializerInterface */
  37. private $serializer;
  38. /**
  39. * @param TokenStorageInterface $tokenStorage
  40. * @param NotificationRepository $notificationRepository
  41. * @param EntityManagerInterface $entityManager
  42. * @param NotificationReadOnRepository $readOnRepository
  43. */
  44. public function __construct(TokenStorageInterface $tokenStorage, NotificationRepository $notificationRepository, EntityManagerInterface $entityManager, NotificationReadOnRepository $readOnRepository, SerializerInterface $serializer)
  45. {
  46. $this->tokenStorage = $tokenStorage;
  47. $this->notificationRepository = $notificationRepository;
  48. $this->entityManager = $entityManager;
  49. $this->readOnRepository = $readOnRepository;
  50. $this->serializer = $serializer;
  51. }
  52. /**
  53. * @Route("/unread", name="read", methods={"GET"})
  54. */
  55. public function read()
  56. {
  57. $readOn = $this->readOnRepository->findOneBy(["user" => $this->tokenStorage->getToken()->getUser()]);
  58. if(!$readOn) {
  59. $readOn = new NotificationReadOn();
  60. $readOn->setDate(new \DateTime());
  61. }
  62. $unread = $this->notificationRepository->findUnreadNotifications($readOn);
  63. $read = $this->notificationRepository->findReadNotifications($readOn);
  64. return new JsonResponse([
  65. 'read' => json_decode($this->serializer->serialize($read, 'json'), TRUE),
  66. 'unread' => json_decode($this->serializer->serialize($unread, 'json'), TRUE)
  67. ]);
  68. }
  69. /**
  70. * @Route("/read", name="check", methods={"POST"})
  71. */
  72. public function check()
  73. {
  74. $user = $this->tokenStorage->getToken()->getUser();
  75. /** @var NotificationReadOn $readOne */
  76. $readOne = $this->readOnRepository->findOneBy([
  77. "user" => $user
  78. ]);
  79. if(!$readOne){
  80. $readOne = new NotificationReadOn();
  81. $readOne->setUser($user);
  82. }
  83. $readOne->setDate(new \DateTime());
  84. $this->entityManager->persist($readOne);
  85. $this->entityManager->flush();
  86. return $readOne;
  87. }
  88. }