<?php
namespace App\Entity;
use App\Exception\InvalidArgumentException;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(
* repositoryClass="App\Repository\LogEntryRepository"
* )
* @ORM\Table(
* name="log_entry",
* options={"collate"="utf8_swedish_ci"}
* )
* @ORM\HasLifecycleCallbacks
*/
class LogEntry implements EntityInterface
{
const TYPE_CREATE = 'create';
const TYPE_STATUS = 'status';
const TYPE_UPDATE = 'update';
/**
* @ORM\Id
* @ORM\Column(
* type="integer"
* )
* @ORM\GeneratedValue(
* strategy="AUTO"
* )
*
* @var int|null
*/
protected ?int $id = null;
/**
* @ORM\Column(
* type="string",
* length=2000,
* nullable=true
* )
*
* @var string|null
*/
protected ?string $note = null;
/**
* @ORM\Column(
* type="datetime"
* )
* @Assert\NotNull
*
* @var DateTime
*/
protected DateTime $time;
/**
* @ORM\Column(
* type="string",
* length=10
* )
*
* @var string
*/
protected string $type = self::TYPE_UPDATE;
/**
* @ORM\ManyToOne(
* targetEntity="User"
* )
* @ORM\JoinColumn(
* name="user_id",
* referencedColumnName="id",
* onDelete="SET NULL"
* )
*
* @var User|null
*/
protected ?User $user = null;
/**
* @ORM\Column(
* name="user_name",
* type="string",
* length=200
* )
* @Assert\NotNull
*
* @var string
*/
protected string $userName;
/**
* @param User $user
* @param string $type
* @param string|null $note
*
* @throws InvalidArgumentException
*/
public function __construct(User $user, string $type = self::TYPE_UPDATE, ?string $note = null)
{
$this->setUser($user);
$this->setType($type);
$this->setNote($note);
$this->time = new DateTime();
}
/**
* @return string|null
*/
public function getNote(): ?string
{
return $this->note;
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return DateTime
*/
public function getTime(): DateTime
{
return $this->time;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @return User|null
*/
public function getUser(): ?User
{
return $this->user;
}
/**
* @return string|null
*/
public function getUserName(): ?string
{
if (null !== $this->user) {
return $this->user->getName();
}
return $this->userName;
}
/**
* @param string|null $note
*/
public function setNote(?string $note)
{
$this->note = $note;
}
/**
* @param DateTime $time
*/
public function setTime(DateTime $time)
{
$this->time = $time;
}
/**
* @param string $type
*
* @throws InvalidArgumentException
*/
public function setType(string $type)
{
if ($type != self::TYPE_CREATE && $type != self::TYPE_STATUS && $type != self::TYPE_UPDATE) {
throw new InvalidArgumentException('Invalid log entry type.');
}
$this->type = $type;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->userName = $user->getName();
}
}