You've already forked symfony-notifications
auto
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user