src/Entity/EntityLog.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\EntityLogRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. #[ORM\Entity(repositoryClassEntityLogRepository::class)]
  6. #[ORM\Table(name'entity_logs')]
  7. #[ORM\Index(name'idx_entity_class'columns: ['entity_class'])]
  8. #[ORM\Index(name'idx_entity_id'columns: ['entity_id'])]
  9. #[ORM\Index(name'idx_action'columns: ['action'])]
  10. #[ORM\Index(name'idx_username'columns: ['username'])]
  11. #[ORM\Index(name'idx_created_at'columns: ['created_at'])]
  12. class EntityLog
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column(type'integer')]
  17.     private ?int $id null;
  18.     #[ORM\Column(type'string'length255)]
  19.     private ?string $entityClass null;
  20.     #[ORM\Column(type'integer'nullabletrue)]
  21.     private ?int $entityId null;
  22.     #[ORM\Column(type'string'length50)]
  23.     private ?string $action null;
  24.     #[ORM\Column(type'string'length255nullabletrue)]
  25.     private ?string $username null;
  26.     #[ORM\Column(type'datetime')]
  27.     private ?\DateTimeInterface $createdAt null;
  28.     public function __construct()
  29.     {
  30.         $this->createdAt = new \DateTime();
  31.     }
  32.     public function getId(): ?int
  33.     {
  34.         return $this->id;
  35.     }
  36.     public function getEntityClass(): ?string
  37.     {
  38.         return $this->entityClass;
  39.     }
  40.     public function setEntityClass(string $entityClass): self
  41.     {
  42.         $this->entityClass $entityClass;
  43.         return $this;
  44.     }
  45.     public function getEntityId(): ?int
  46.     {
  47.         return $this->entityId;
  48.     }
  49.     public function setEntityId(?int $entityId): self
  50.     {
  51.         $this->entityId $entityId;
  52.         return $this;
  53.     }
  54.     public function getAction(): ?string
  55.     {
  56.         return $this->action;
  57.     }
  58.     public function setAction(string $action): self
  59.     {
  60.         $this->action $action;
  61.         return $this;
  62.     }
  63.     public function getUsername(): ?string
  64.     {
  65.         return $this->username;
  66.     }
  67.     public function setUsername(?string $username): self
  68.     {
  69.         $this->username $username;
  70.         return $this;
  71.     }
  72.     public function getCreatedAt(): ?\DateTimeInterface
  73.     {
  74.         return $this->createdAt;
  75.     }
  76.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  77.     {
  78.         $this->createdAt $createdAt;
  79.         return $this;
  80.     }
  81.     /**
  82.      * Get entity class short name
  83.      */
  84.     public function getEntityShortName(): string
  85.     {
  86.         $parts explode('\\'$this->entityClass ?? '');
  87.         return end($parts);
  88.     }
  89.     /**
  90.      * Get entity display name with ID
  91.      */
  92.     public function getEntityDisplayName(): string
  93.     {
  94.         $shortName $this->getEntityShortName();
  95.         if ($this->entityId) {
  96.             return $shortName ' #' $this->entityId;
  97.         }
  98.         return $shortName;
  99.     }
  100.     /**
  101.      * Get human-readable action description
  102.      */
  103.     public function getActionDescription(): string
  104.     {
  105.         return match($this->action) {
  106.             'create' => 'Created',
  107.             'update' => 'Updated',
  108.             'delete' => 'Deleted',
  109.             'view' => 'Viewed',
  110.             default => ucfirst($this->action ?? 'Unknown')
  111.         };
  112.     }
  113. }