src/Repository/UserSessionLogRepository.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\UserSessionLog;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @method UserSessionLog|null find($id, $lockMode = null, $lockVersion = null)
  8.  * @method UserSessionLog|null findOneBy(array $criteria, array $orderBy = null)
  9.  * @method UserSessionLog[]    findAll()
  10.  * @method UserSessionLog[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  11.  */
  12. class UserSessionLogRepository extends ServiceEntityRepository
  13. {
  14.     public function __construct(ManagerRegistry $registry)
  15.     {
  16.         parent::__construct($registryUserSessionLog::class);
  17.     }
  18.     /**
  19.      * Find active session by session ID
  20.      */
  21.     public function findActiveBySessionId(string $sessionId): ?UserSessionLog
  22.     {
  23.         return $this->createQueryBuilder('s')
  24.             ->andWhere('s.sessionId = :sessionId')
  25.             ->andWhere('s.logoutAt IS NULL')
  26.             ->setParameter('sessionId'$sessionId)
  27.             ->getQuery()
  28.             ->getOneOrNullResult();
  29.     }
  30.     /**
  31.      * Find all active sessions
  32.      */
  33.     public function findActiveSessions(): array
  34.     {
  35.         return $this->createQueryBuilder('s')
  36.             ->andWhere('s.logoutAt IS NULL')
  37.             ->orderBy('s.lastActivityAt''DESC')
  38.             ->getQuery()
  39.             ->getResult();
  40.     }
  41.     /**
  42.      * Close inactive sessions (no activity for more than X minutes)
  43.      */
  44.     public function closeInactiveSessions(int $inactiveMinutes 30): int
  45.     {
  46.         $threshold = new \DateTime();
  47.         $threshold->modify("-{$inactiveMinutes} minutes");
  48.         $qb $this->createQueryBuilder('s');
  49.         return $qb->update()
  50.             ->set('s.logoutAt'':now')
  51.             ->set('s.duration''TIMESTAMPDIFF(SECOND, s.loginAt, s.lastActivityAt)')
  52.             ->where('s.logoutAt IS NULL')
  53.             ->andWhere('s.lastActivityAt < :threshold')
  54.             ->setParameter('now', new \DateTime())
  55.             ->setParameter('threshold'$threshold)
  56.             ->getQuery()
  57.             ->execute();
  58.     }
  59.     /**
  60.      * Get user session statistics
  61.      */
  62.     public function getUserStats(int $userId\DateTimeInterface $from\DateTimeInterface $to): array
  63.     {
  64.         $qb $this->createQueryBuilder('s');
  65.         return $qb->select([
  66.                 'COUNT(s.id) as total_sessions',
  67.                 'SUM(s.duration) as total_duration',
  68.                 'AVG(s.duration) as avg_duration',
  69.                 'MAX(s.duration) as max_duration',
  70.                 'MIN(s.duration) as min_duration',
  71.             ])
  72.             ->where('s.user = :userId')
  73.             ->andWhere('s.loginAt >= :from')
  74.             ->andWhere('s.loginAt <= :to')
  75.             ->setParameter('userId'$userId)
  76.             ->setParameter('from'$from)
  77.             ->setParameter('to'$to)
  78.             ->getQuery()
  79.             ->getSingleResult();
  80.     }
  81. }