src/Entity/UserSessionLog.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTimeInterface;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\UserSessionLogRepository")
  8.  * @ORM\Table(name="user_session_log",
  9.  *     indexes={
  10.  *         @ORM\Index(name="idx_session_user_id", columns={"user_id"}),
  11.  *         @ORM\Index(name="idx_session_username", columns={"username"}),
  12.  *         @ORM\Index(name="idx_session_login_at", columns={"login_at"}),
  13.  *         @ORM\Index(name="idx_session_logout_at", columns={"logout_at"}),
  14.  *         @ORM\Index(name="idx_session_active", columns={"logout_at", "last_activity_at"}),
  15.  *         @ORM\Index(name="idx_session_id", columns={"session_id"})
  16.  *     }
  17.  * )
  18.  */
  19. class UserSessionLog
  20. {
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\Column(type="bigint", options={"unsigned":true})
  24.      * @ORM\GeneratedValue(strategy="AUTO")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\ManyToOne(targetEntity="App\Entity\User")
  29.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  30.      */
  31.     private $user;
  32.     /**
  33.      * @ORM\Column(type="string", nullable=true)
  34.      */
  35.     private $username;
  36.     /**
  37.      * @ORM\Column(type="string", nullable=true)
  38.      */
  39.     private $email;
  40.     /**
  41.      * @ORM\Column(type="datetime", nullable=false, name="login_at")
  42.      */
  43.     private $loginAt;
  44.     /**
  45.      * @ORM\Column(type="datetime", nullable=true, name="logout_at")
  46.      */
  47.     private $logoutAt;
  48.     /**
  49.      * @ORM\Column(type="datetime", nullable=true, name="last_activity_at")
  50.      */
  51.     private $lastActivityAt;
  52.     /**
  53.      * @ORM\Column(type="integer", nullable=true)
  54.      */
  55.     private $duration;
  56.     /**
  57.      * @ORM\Column(type="string", nullable=true, name="ip_address")
  58.      */
  59.     private $ipAddress;
  60.     /**
  61.      * @ORM\Column(type="string", length=500, nullable=true, name="user_agent")
  62.      */
  63.     private $userAgent;
  64.     /**
  65.      * @ORM\Column(type="string", length=20, nullable=true, name="logout_type")
  66.      */
  67.     private $logoutType;
  68.     /**
  69.      * @ORM\Column(type="string", nullable=false, name="session_id")
  70.      */
  71.     private $sessionId;
  72.     /**
  73.      * @ORM\Column(type="datetime", nullable=true, name="deleted_at")
  74.      */
  75.     private $deletedAt;
  76.     /**
  77.      * @ORM\Column(type="datetime", nullable=false, name="updated_at", options={"default":"2022-01-01 00:00:00"})
  78.      * @Gedmo\Timestampable(on="update")
  79.      */
  80.     private $updatedAt;
  81.     /**
  82.      * @ORM\Column(type="datetime", nullable=false, name="created_at", options={"default":"2022-01-01 00:00:00"})
  83.      * @Gedmo\Timestampable(on="create")
  84.      */
  85.     private $createdAt;
  86.     public function getId(): ?string
  87.     {
  88.         return $this->id;
  89.     }
  90.     public function getUser(): ?User
  91.     {
  92.         return $this->user;
  93.     }
  94.     public function setUser(?User $user): static
  95.     {
  96.         $this->user $user;
  97.         return $this;
  98.     }
  99.     public function getUsername(): ?string
  100.     {
  101.         return $this->username;
  102.     }
  103.     public function setUsername(?string $username): static
  104.     {
  105.         $this->username $username;
  106.         return $this;
  107.     }
  108.     public function getEmail(): ?string
  109.     {
  110.         return $this->email;
  111.     }
  112.     public function setEmail(?string $email): static
  113.     {
  114.         $this->email $email;
  115.         return $this;
  116.     }
  117.     public function getLoginAt(): ?DateTimeInterface
  118.     {
  119.         return $this->loginAt;
  120.     }
  121.     public function setLoginAt(DateTimeInterface $loginAt): static
  122.     {
  123.         $this->loginAt $loginAt;
  124.         return $this;
  125.     }
  126.     public function getLogoutAt(): ?DateTimeInterface
  127.     {
  128.         return $this->logoutAt;
  129.     }
  130.     public function setLogoutAt(?DateTimeInterface $logoutAt): static
  131.     {
  132.         $this->logoutAt $logoutAt;
  133.         // Calculate duration if logout is set
  134.         if ($logoutAt && $this->loginAt) {
  135.             // Doctrine returns DateTime in UTC but DB stores in local time
  136.             // Re-create DateTimes with correct timezone to fix timestamp calculation
  137.             $loginLocal = new \DateTime(
  138.                 $this->loginAt->format('Y-m-d H:i:s'),
  139.                 new \DateTimeZone('Europe/Madrid')
  140.             );
  141.             $logoutLocal = new \DateTime(
  142.                 $logoutAt->format('Y-m-d H:i:s'),
  143.                 new \DateTimeZone('Europe/Madrid')
  144.             );
  145.             $this->duration $logoutLocal->getTimestamp() - $loginLocal->getTimestamp();
  146.         }
  147.         return $this;
  148.     }
  149.     public function getLastActivityAt(): ?DateTimeInterface
  150.     {
  151.         return $this->lastActivityAt;
  152.     }
  153.     public function setLastActivityAt(?DateTimeInterface $lastActivityAt): static
  154.     {
  155.         $this->lastActivityAt $lastActivityAt;
  156.         return $this;
  157.     }
  158.     public function getDuration(): ?int
  159.     {
  160.         return $this->duration;
  161.     }
  162.     public function setDuration(?int $duration): static
  163.     {
  164.         $this->duration $duration;
  165.         return $this;
  166.     }
  167.     public function getDurationFormatted(): ?string
  168.     {
  169.         if (!$this->duration) {
  170.             return null;
  171.         }
  172.         $hours floor($this->duration 3600);
  173.         $minutes floor(($this->duration 3600) / 60);
  174.         $seconds $this->duration 60;
  175.         if ($hours 0) {
  176.             return sprintf('%dh %dm'$hours$minutes);
  177.         } elseif ($minutes 0) {
  178.             return sprintf('%dm %ds'$minutes$seconds);
  179.         } else {
  180.             return sprintf('%ds'$seconds);
  181.         }
  182.     }
  183.     public function getIpAddress(): ?string
  184.     {
  185.         return $this->ipAddress;
  186.     }
  187.     public function setIpAddress(?string $ipAddress): static
  188.     {
  189.         $this->ipAddress $ipAddress;
  190.         return $this;
  191.     }
  192.     public function getUserAgent(): ?string
  193.     {
  194.         return $this->userAgent;
  195.     }
  196.     public function setUserAgent(?string $userAgent): static
  197.     {
  198.         $this->userAgent $userAgent;
  199.         return $this;
  200.     }
  201.     public function getSessionId(): ?string
  202.     {
  203.         return $this->sessionId;
  204.     }
  205.     public function setSessionId(string $sessionId): static
  206.     {
  207.         $this->sessionId $sessionId;
  208.         return $this;
  209.     }
  210.     public function getLogoutType(): ?string
  211.     {
  212.         return $this->logoutType;
  213.     }
  214.     public function setLogoutType(?string $logoutType): static
  215.     {
  216.         $this->logoutType $logoutType;
  217.         return $this;
  218.     }
  219.     public function getDeletedAt(): ?DateTimeInterface
  220.     {
  221.         return $this->deletedAt;
  222.     }
  223.     public function setDeletedAt(?DateTimeInterface $deletedAt): static
  224.     {
  225.         $this->deletedAt $deletedAt;
  226.         return $this;
  227.     }
  228.     public function getUpdatedAt(): ?DateTimeInterface
  229.     {
  230.         return $this->updatedAt;
  231.     }
  232.     public function setUpdatedAt(DateTimeInterface $updatedAt): static
  233.     {
  234.         $this->updatedAt $updatedAt;
  235.         return $this;
  236.     }
  237.     public function getCreatedAt(): ?DateTimeInterface
  238.     {
  239.         return $this->createdAt;
  240.     }
  241.     public function setCreatedAt(DateTimeInterface $createdAt): static
  242.     {
  243.         $this->createdAt $createdAt;
  244.         return $this;
  245.     }
  246.     public function isActive(): bool
  247.     {
  248.         return $this->logoutAt === null;
  249.     }
  250.     public function __toString(): string
  251.     {
  252.         return sprintf(
  253.             '%s - %s (%s)',
  254.             $this->username ?? 'Unknown',
  255.             $this->loginAt?->format('d/m/Y H:i:s') ?? '',
  256.             $this->isActive() ? 'Activo' 'Cerrado'
  257.         );
  258.     }
  259. }