src/Entity/UserSessionLog.php line 23

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