symfony-notifications/NotificationsBundle/Controller/MainController.php

85 lines
2.5 KiB
PHP
Raw Normal View History

2019-03-01 12:18:06 +01:00
<?php
2019-03-01 12:49:26 +01:00
namespace Bluesquare\NotificationsBundle\Controller;
2019-03-01 12:18:06 +01:00
2019-03-01 16:02:12 +01:00
use Bluesquare\NotificationsBundle\Entity\Notification;
2019-03-01 12:18:06 +01:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2019-03-01 16:02:12 +01:00
/**
* Oui, je throw des exceptions sans catch, c'est comme ça qu'on utilise ce framework proprement
* https://symfony.com/doc/current/controller.html#managing-errors-and-404-pages
*
* Class MainController
* @package Bluesquare\NotificationsBundle\Controller
*/
2019-03-01 12:18:06 +01:00
class MainController extends AbstractController
{
2019-03-01 16:02:12 +01:00
public function getAll()
2019-03-01 12:18:06 +01:00
{
2019-03-01 14:20:14 +01:00
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
2019-03-01 13:19:12 +01:00
$notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv');
2019-03-01 14:20:14 +01:00
$user = $this->getUser();
$notifs = $notifssrv->getForUser($user, true);
return $this->json($notifs);
2019-03-01 12:18:06 +01:00
}
2019-03-01 16:02:12 +01:00
public function delete($id)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$notif = $em->getRepository(Notification::class)->find($id);
if (!$notif)
throw $this->createNotFoundException('Notification not found');
if ($notif->getUser() != $user)
throw $this->createAccessDeniedException();
$em->remove($notif);
$em->flush();
return $this->json(["message" => 'OK']);
}
public function markSeen($id)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$notif = $em->getRepository(Notification::class)->find($id);
if (!$notif)
throw $this->createNotFoundException('Notification not found');
if ($notif->getUser() != $user)
throw $this->createAccessDeniedException();
$notif->setSeenAt(new \DateTime());
$em->persist($notif);
$em->flush();
return $this->json(["message" => 'OK']);
}
public function test()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->getUser();
$notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv');
$notif = new Notification();
$notif->setTitle("Bonjour");
$notif->setDescription("Skippy vous attend dans son antre");
$notif->setData("le_nom_de_la_route_en_react");
$users = [$user]; // Peut contenir plein d'users
$notifssrv->notifyUsers($users, $notif);
return $this->json(["message" => 'OK']);
}
2019-03-01 12:18:06 +01:00
}