<?php
namespace App\Entity;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserSessionLogRepository")
* @ORM\Table(name="user_session_log",
* indexes={
* @ORM\Index(name="idx_session_user_id", columns={"user_id"}),
* @ORM\Index(name="idx_session_username", columns={"username"}),
* @ORM\Index(name="idx_session_login_at", columns={"login_at"}),
* @ORM\Index(name="idx_session_logout_at", columns={"logout_at"}),
* @ORM\Index(name="idx_session_active", columns={"logout_at", "last_activity_at"}),
* @ORM\Index(name="idx_session_id", columns={"session_id"})
* }
* )
*/
class UserSessionLog
{
/**
* @ORM\Id
* @ORM\Column(type="bigint", options={"unsigned":true})
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private $user;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $username;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $email;
/**
* @ORM\Column(type="datetime", nullable=false, name="login_at")
*/
private $loginAt;
/**
* @ORM\Column(type="datetime", nullable=true, name="logout_at")
*/
private $logoutAt;
/**
* @ORM\Column(type="datetime", nullable=true, name="last_activity_at")
*/
private $lastActivityAt;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $duration;
/**
* @ORM\Column(type="string", nullable=true, name="ip_address")
*/
private $ipAddress;
/**
* @ORM\Column(type="string", length=500, nullable=true, name="user_agent")
*/
private $userAgent;
/**
* @ORM\Column(type="string", length=20, nullable=true, name="logout_type")
*/
private $logoutType;
/**
* @ORM\Column(type="string", nullable=false, name="session_id")
*/
private $sessionId;
/**
* @ORM\Column(type="datetime", nullable=true, name="deleted_at")
*/
private $deletedAt;
/**
* @ORM\Column(type="datetime", nullable=false, name="updated_at", options={"default":"2022-01-01 00:00:00"})
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime", nullable=false, name="created_at", options={"default":"2022-01-01 00:00:00"})
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
public function getId(): ?string
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(?string $username): static
{
$this->username = $username;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): static
{
$this->email = $email;
return $this;
}
public function getLoginAt(): ?DateTimeInterface
{
return $this->loginAt;
}
public function setLoginAt(DateTimeInterface $loginAt): static
{
$this->loginAt = $loginAt;
return $this;
}
public function getLogoutAt(): ?DateTimeInterface
{
return $this->logoutAt;
}
public function setLogoutAt(?DateTimeInterface $logoutAt): static
{
$this->logoutAt = $logoutAt;
// Calculate duration if logout is set
if ($logoutAt && $this->loginAt) {
// Doctrine returns DateTime in UTC but DB stores in local time
// Re-create DateTimes with correct timezone to fix timestamp calculation
$loginLocal = new \DateTime(
$this->loginAt->format('Y-m-d H:i:s'),
new \DateTimeZone('Europe/Madrid')
);
$logoutLocal = new \DateTime(
$logoutAt->format('Y-m-d H:i:s'),
new \DateTimeZone('Europe/Madrid')
);
$this->duration = $logoutLocal->getTimestamp() - $loginLocal->getTimestamp();
}
return $this;
}
public function getLastActivityAt(): ?DateTimeInterface
{
return $this->lastActivityAt;
}
public function setLastActivityAt(?DateTimeInterface $lastActivityAt): static
{
$this->lastActivityAt = $lastActivityAt;
return $this;
}
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration): static
{
$this->duration = $duration;
return $this;
}
public function getDurationFormatted(): ?string
{
if (!$this->duration) {
return null;
}
$hours = floor($this->duration / 3600);
$minutes = floor(($this->duration % 3600) / 60);
$seconds = $this->duration % 60;
if ($hours > 0) {
return sprintf('%dh %dm', $hours, $minutes);
} elseif ($minutes > 0) {
return sprintf('%dm %ds', $minutes, $seconds);
} else {
return sprintf('%ds', $seconds);
}
}
public function getIpAddress(): ?string
{
return $this->ipAddress;
}
public function setIpAddress(?string $ipAddress): static
{
$this->ipAddress = $ipAddress;
return $this;
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function setUserAgent(?string $userAgent): static
{
$this->userAgent = $userAgent;
return $this;
}
public function getSessionId(): ?string
{
return $this->sessionId;
}
public function setSessionId(string $sessionId): static
{
$this->sessionId = $sessionId;
return $this;
}
public function getLogoutType(): ?string
{
return $this->logoutType;
}
public function setLogoutType(?string $logoutType): static
{
$this->logoutType = $logoutType;
return $this;
}
public function getDeletedAt(): ?DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?DateTimeInterface $deletedAt): static
{
$this->deletedAt = $deletedAt;
return $this;
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(DateTimeInterface $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getCreatedAt(): ?DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function isActive(): bool
{
return $this->logoutAt === null;
}
public function __toString(): string
{
return sprintf(
'%s - %s (%s)',
$this->username ?? 'Unknown',
$this->loginAt?->format('d/m/Y H:i:s') ?? '',
$this->isActive() ? 'Activo' : 'Cerrado'
);
}
}