/**
  * Note last ID read for user in channel
  *
  * @param User $userId
  * @param Channel $channelId
  * @param $lastId
  */
 public function markRead(User $user, Channel $channel, $lastId)
 {
     $qb = $this->_em->createQueryBuilder();
     $qb->select('s')->from('MauticChatBundle:ChannelStat', 's')->where($qb->expr()->eq('s.channel', ':channel'))->setParameter('channel', $channel)->andWhere($qb->expr()->eq('s.user', ':user'))->setParameter('user', $user);
     $exists = $qb->getQuery()->getResult();
     $now = new DateTimeHelper();
     if (!empty($exists)) {
         //update the record
         $exists[0]->setLastRead($lastId);
         $exists[0]->setDateRead($now->getDateTime());
         $this->_em->persist($exists[0]);
     } else {
         $stat = new ChannelStat();
         $stat->setChannel($channel);
         $stat->setUser($user);
         $stat->setLastRead($lastId);
         $stat->setDateRead($now->getDateTime());
         $stat->setDateJoined($now->getDateTime());
         $this->_em->persist($stat);
     }
     $this->_em->flush();
 }
 /**
  * Subscribe user to channel
  *
  * @param Channel $channel
  * @param User    $user
  */
 public function subscribeToChannel(Channel $channel, User $user = null)
 {
     if ($user == null) {
         $user = $this->factory->getUser();
     }
     $stat = $this->getUserChannelStats($channel);
     if (empty($stat)) {
         $now = new DateTimeHelper();
         //get the last read id
         $lastId = $this->getRepository()->getLastChatId($channel->getId());
         $stat = new ChannelStat();
         $stat->setChannel($channel);
         $stat->setUser($user);
         $stat->setLastRead($lastId);
         $stat->setDateRead($now->getDateTime());
         $stat->setDateJoined($now->getDateTime());
         $this->getRepository()->saveEntity($stat);
     }
 }