<?php
namespace App\CmsBundle\Entity;
use App\ProfileBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(
* name="log",
* indexes={
* @ORM\Index(name="created", columns={"created"})
* }
* )
* @ORM\Entity(repositoryClass="App\CmsBundle\Repository\LogRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Log
{
use IdTrait;
/**
* @ORM\Column(
* type="string",
* length=255,
* nullable=false
* )
*/
private $action;
/**
* @ORM\Column(
* type="string",
* length=255,
* nullable=true
* )
*/
private $entity;
/**
* @ORM\ManyToOne(
* targetEntity="App\ProfileBundle\Entity\User",
* inversedBy="logs"
* )
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* @ORM\Column(
* name="context",
* type="text",
* nullable=true
* )
*/
private ?string $context = null;
/**
* @param User $user
* @param string $action
* @param $context
*/
public function __construct(User $user, string $action, string $entity, $context = null)
{
$this->setUser($user)->setAction($action)->setEntity($entity)->setContext($context);
}
public function __toString(): string
{
return $this->id;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getAction(): ?string
{
return $this->action;
}
/**
* @param string $action
* @return $this
*/
public function setAction(string $action): self
{
$this->action = $action;
return $this;
}
/**
* @return string|null
*/
public function getEntity(): ?string
{
return $this->entity;
}
/**
* @param string $entity
* @return $this
*/
public function setEntity(string $entity): self
{
$this->entity = $entity;
return $this;
}
/**
* @return User|null
*/
public function getUser(): ?User
{
return $this->user;
}
/**
* @param User|null $user
* @return $this
*/
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
/**
* @return string|null
*/
public function getContext(): ?string
{
return ($this->context)
? json_encode(json_decode($this->context, true), JSON_PRETTY_PRINT)
: '';
}
/**
* @param object|array|null $context
* @return $this
*/
public function setContext(object|array|null $context): self
{
$this->context = json_encode($context);
return $this;
}
/**
* Set initial value for created/updated values
*
* @ORM\PrePersist
*/
public function setCreated()
{
$this->created = new \DateTime();
}
/**
* @return \DateTime
*/
public function getCreated(): \DateTime
{
return $this->created;
}
}