2019-03-01 13:12:17 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Bluesquare\NotificationsBundle\Service;
|
|
|
|
|
2019-03-01 14:20:14 +01:00
|
|
|
use Bluesquare\NotificationsBundle\Entity\Notification;
|
2019-03-01 13:12:17 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
|
|
|
class NotificationsService
|
|
|
|
{
|
|
|
|
protected $em;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:20:14 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
2019-03-01 13:12:17 +01:00
|
|
|
{
|
2019-03-01 14:20:14 +01:00
|
|
|
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()
|
|
|
|
];
|
2019-03-01 13:12:17 +01:00
|
|
|
}
|
|
|
|
}
|