<?php
declare(strict_types=1);
namespace App\Entity;
use App\AuditLog\AuditLogMessage;
use App\Helpers\Utils;
use App\Repository\AuditLogRepository;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=AuditLogRepository::class)
* @ORM\Table(name="audit_log", indexes={
* @ORM\Index(name="idx_opdate", columns={"opdate"}),
* @ORM\Index(name="idx_resource", columns={"resource"}),
* @ORM\Index(name="idx_action", columns={"action"}),
* })
*/
class AuditLog
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
*/
private User $user;
/**
* @ORM\Column(type="string", length=255)
*/
private string $resource;
/**
* @ORM\Column(type="string", length=255)
*/
private string $action;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $message;
/**
* @ORM\Column(type="datetime")
*/
private DateTimeInterface $opdate;
/**
* @ORM\Column(type="string", length=255)
*/
private string $ip;
public function __construct(User $user, AuditLogMessage $message, string $ip = '')
{
$this->user = $user;
$this->resource = $message->getResourceName();
$this->action = $message->getActionName();
$this->message = $message->convertToDatabaseValue($message);
$this->opdate = new DateTimeImmutable();
$this->ip = $ip;
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
public function getResource(): ?string
{
return $this->resource;
}
public function setResource(string $resource): self
{
$this->resource = $resource;
return $this;
}
public function getAction(): ?string
{
return $this->action;
}
public function setAction(string $action): self
{
$this->action = $action;
return $this;
}
public function getMessage(): ?string
{
return $this->message;
}
public function setMessage(?string $message): self
{
$this->message = $message;
return $this;
}
public function getOpdate(): ?DateTimeInterface
{
return $this->opdate;
}
public function setOpdate(DateTimeInterface $opdate): self
{
$this->opdate = $opdate;
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(string $ip): self
{
$this->ip = $ip;
return $this;
}
}