48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use App\Service\RedisClientProvider;
|
|
use App\Entity\Notification;
|
|
|
|
class NotificationManager
|
|
{
|
|
protected $redis;
|
|
protected $redis_mqtt_key;
|
|
protected $em;
|
|
|
|
const NOTIF_KEY = 'user/{user_id}/notification';
|
|
|
|
public function __construct(RedisClientProvider $redis_prov, EntityManagerInterface $em, $redis_mqtt_key)
|
|
{
|
|
$this->redis = $redis_prov->getRedisClient();
|
|
$this->redis_mqtt_key = $redis_mqtt_key;
|
|
$this->em = $em;
|
|
}
|
|
|
|
// set user_id to 0 for all
|
|
public function sendNotification($user_id, $msg, $url)
|
|
{
|
|
// send mqtt
|
|
$chan = $this->getChannel($user_id);
|
|
$data = $chan . '|' . $msg;
|
|
$this->redis->lpush($this->redis_mqtt_key, $data);
|
|
|
|
// create notif
|
|
$notif = new Notification();
|
|
$notif->setUserID($user_id)
|
|
->setIcon('')
|
|
->setMessage($msg)
|
|
->setURL($url);
|
|
|
|
// save to db
|
|
$this->em->persist($notif);
|
|
$this->em->flush();
|
|
}
|
|
|
|
protected function getChannel($user_id)
|
|
{
|
|
return str_replace('{user_id}', $user_id, self::NOTIF_KEY );
|
|
}
|
|
}
|