From a98aa394ac8da823c91923ed6cc46095d4e093b4 Mon Sep 17 00:00:00 2001 From: PaulCombal Date: Fri, 1 Mar 2019 14:20:14 +0100 Subject: [PATCH] auto --- NotificationsBundle/Controller/MainController.php | 17 ++--- NotificationsBundle/Resources/config/routes.yaml | 4 +- .../Service/NotificationsService.php | 83 +++++++++++++++++++++- README.md | 41 +++++++++-- 4 files changed, 126 insertions(+), 19 deletions(-) diff --git a/NotificationsBundle/Controller/MainController.php b/NotificationsBundle/Controller/MainController.php index 4bca047..d5668d7 100644 --- a/NotificationsBundle/Controller/MainController.php +++ b/NotificationsBundle/Controller/MainController.php @@ -2,21 +2,18 @@ namespace Bluesquare\NotificationsBundle\Controller; -use Bluesquare\NotificationsBundle\Service\NotificationsService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; class MainController extends AbstractController { - /** - * @param Request $request - */ - public function index(Request $request) + public function myUser() { - // ON PEUT PAS INJECTER UN SERVICE DU BUNDLE EN QUESTION DANS LES ARGS DONC ON LE récupère ainsi : - /** @var NotificationsService $notifssrv */ + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv'); - $r = $notifssrv->notifyUsers(); - die($r); + $user = $this->getUser(); + + $notifs = $notifssrv->getForUser($user, true); + return $this->json($notifs); } } \ No newline at end of file diff --git a/NotificationsBundle/Resources/config/routes.yaml b/NotificationsBundle/Resources/config/routes.yaml index 75677f7..7bc7f22 100644 --- a/NotificationsBundle/Resources/config/routes.yaml +++ b/NotificationsBundle/Resources/config/routes.yaml @@ -1,3 +1,3 @@ bluesquare_notifications_bundle_notifications: - path: /test-notifs - controller: Bluesquare\NotificationsBundle\Controller\MainController::index + path: /my-user + controller: Bluesquare\NotificationsBundle\Controller\MainController::myUser diff --git a/NotificationsBundle/Service/NotificationsService.php b/NotificationsBundle/Service/NotificationsService.php index ee7722d..ddebc20 100644 --- a/NotificationsBundle/Service/NotificationsService.php +++ b/NotificationsBundle/Service/NotificationsService.php @@ -2,6 +2,7 @@ namespace Bluesquare\NotificationsBundle\Service; +use Bluesquare\NotificationsBundle\Entity\Notification; use Doctrine\ORM\EntityManagerInterface; class NotificationsService @@ -13,8 +14,86 @@ class NotificationsService $this->em = $em; } - public function notifyUsers() + /** + * Assigne une notification $notification à tous les users de $users + * La notification sera dupliquée pour chaque user + * + * @param iterable $users + * @param Notification $notification + */ + public function notifyUsers(iterable $users, Notification $notification) { - return 'bar'; + foreach ($users as $user) + { + $notif = $this->duplicateNotification($notification); + $notif->setUser($user); + $this->em->persist($notif); + } + + $this->em->flush(); + } + + /** + * Duplicates a notification object. Doesn't copy ID, deleted_at, and seen_at values. + * + * @param Notification $notification + * @return Notification + */ + public function duplicateNotification(Notification $notification) + { + $notif = new Notification(); + $notif->setTitle($notification->getTitle()); + $notif->setUser($notification->getUser()); + $notif->setDescription($notification->getDescription()); + $notif->setData($notification->getData()); + + return $notif; + } + + /** + * Little shortcut to get all notification for a user + * This way you don't need to have a mnager or import notification. + * + * If you want the return value to be associative arrays, set assoc to true + * + * @param $user + * @param bool $assoc + * @return object[] + */ + public function getForUser($user, $assoc = false) + { + $ret = $this->em->getRepository(Notification::class)->findBy(['user' => $user]); + + if ($assoc) + { + $arr = []; + foreach ($ret as $n) + { + $arr[] = $this->toArray($n); + } + + $ret = $arr; + } + + return $ret; + } + + /** + * Transforms entity to assoc array + * + * @param Notification $notification + * @return array + */ + public function toArray(Notification $notification) + { + return [ + 'title' => $notification->getTitle(), + 'description' => $notification->getDescription(), + 'data' => $notification->getData(), + 'user' => $notification->getUser()->getId(), + 'seen_at' => $notification->getSeenAt(), + 'deleted_at' => $notification->getDeletedAt(), + 'id' => $notification->getId() + ]; } } \ No newline at end of file diff --git a/README.md b/README.md index f8d234c..f8fe63f 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,11 @@ Notifications bundle Ajout de notifications pour les users (backend seulement) +## Restrictions + +Votre classe User doit absoluement être dans le namespace \App\Entity et s'appeler User, soit etre +\App\Entity\User. + ## Routes: Pour installer les routes, ajouter ceci: @@ -15,11 +20,37 @@ notifications: à votre config/routes.yaml -* **GET** /notifications/by-user/{id} +Pour créer des notifs, il faut utiliser le service. Effectivement on ne veut pas permettre +aux clients de spammer les routes de notifications ou lire les notifs de toute le monde. +Vous pouvez ajouter des routes dans votre application pour plus de flexibilité. -A faire -## Restrictions +* **GET** /notifications/my-user -Votre classe User doit absoluement être dans le namespace \App\Entity et s'appeler User, soit etre -\App\Entity\User. \ No newline at end of file +Récupère toutes les notifications de l'utilisateur courant. + +## Service + +La classe est `NotificationsService`. Toutes les méthodes sont commentées, veuillez vous +référer aux comentaires sur les méthodes plutot que ce README qui ne fait office que de +listing de features. + +Méthdodes: + +* notifyUsers: + * Ajoute des notifications aux users + +* getForUser + * Récupère toutes les notifications pour un user, sous format d'entité ou array + TODO: ajouter des flags pour filtrer + +* toArray + * Convertit un objet Notification en Array + +* duplicateNotification + * Clone une notif (pour notifier un batch d'user) + Normalement il n'y a aucun besoin d'utiliser cette méthode en dehors du bundle. + +## Entités + +* Notification: représente une notification pour un seul utilisateur \ No newline at end of file