src/Entity/LogEntry.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Exception\InvalidArgumentException;
  4. use DateTime;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8.  * @ORM\Entity(
  9.  *     repositoryClass="App\Repository\LogEntryRepository"
  10.  * )
  11.  * @ORM\Table(
  12.  *     name="log_entry",
  13.  *     options={"collate"="utf8_swedish_ci"}
  14.  * )
  15.  * @ORM\HasLifecycleCallbacks
  16.  */
  17. class LogEntry implements EntityInterface
  18. {
  19.     const TYPE_CREATE 'create';
  20.     const TYPE_STATUS 'status';
  21.     const TYPE_UPDATE 'update';
  22.     /**
  23.      * @ORM\Id
  24.      * @ORM\Column(
  25.      *     type="integer"
  26.      * )
  27.      * @ORM\GeneratedValue(
  28.      *     strategy="AUTO"
  29.      * )
  30.      *
  31.      * @var int|null
  32.      */
  33.     protected ?int $id null;
  34.     /**
  35.      * @ORM\Column(
  36.      *     type="string",
  37.      *     length=2000,
  38.      *     nullable=true
  39.      * )
  40.      *
  41.      * @var string|null
  42.      */
  43.     protected ?string $note null;
  44.     /**
  45.      * @ORM\Column(
  46.      *     type="datetime"
  47.      * )
  48.      * @Assert\NotNull
  49.      *
  50.      * @var DateTime
  51.      */
  52.     protected DateTime $time;
  53.     /**
  54.      * @ORM\Column(
  55.      *     type="string",
  56.      *     length=10
  57.      * )
  58.      *
  59.      * @var string
  60.      */
  61.     protected string $type self::TYPE_UPDATE;
  62.     /**
  63.      * @ORM\ManyToOne(
  64.      *     targetEntity="User"
  65.      * )
  66.      * @ORM\JoinColumn(
  67.      *     name="user_id",
  68.      *     referencedColumnName="id",
  69.      *     onDelete="SET NULL"
  70.      * )
  71.      *
  72.      * @var User|null
  73.      */
  74.     protected ?User $user null;
  75.     /**
  76.      * @ORM\Column(
  77.      *     name="user_name",
  78.      *     type="string",
  79.      *     length=200
  80.      * )
  81.      * @Assert\NotNull
  82.      *
  83.      * @var string
  84.      */
  85.     protected string $userName;
  86.     /**
  87.      * @param User        $user
  88.      * @param string      $type
  89.      * @param string|null $note
  90.      *
  91.      * @throws InvalidArgumentException
  92.      */
  93.     public function __construct(User $userstring $type self::TYPE_UPDATE, ?string $note null)
  94.     {
  95.         $this->setUser($user);
  96.         $this->setType($type);
  97.         $this->setNote($note);
  98.         $this->time = new DateTime();
  99.     }
  100.     /**
  101.      * @return string|null
  102.      */
  103.     public function getNote(): ?string
  104.     {
  105.         return $this->note;
  106.     }
  107.     /**
  108.      * @return int|null
  109.      */
  110.     public function getId(): ?int
  111.     {
  112.         return $this->id;
  113.     }
  114.     /**
  115.      * @return DateTime
  116.      */
  117.     public function getTime(): DateTime
  118.     {
  119.         return $this->time;
  120.     }
  121.     /**
  122.      * @return string
  123.      */
  124.     public function getType(): string
  125.     {
  126.         return $this->type;
  127.     }
  128.     /**
  129.      * @return User|null
  130.      */
  131.     public function getUser(): ?User
  132.     {
  133.         return $this->user;
  134.     }
  135.     /**
  136.      * @return string|null
  137.      */
  138.     public function getUserName(): ?string
  139.     {
  140.         if (null !== $this->user) {
  141.             return $this->user->getName();
  142.         }
  143.         return $this->userName;
  144.     }
  145.     /**
  146.      * @param string|null $note
  147.      */
  148.     public function setNote(?string $note)
  149.     {
  150.         $this->note $note;
  151.     }
  152.     /**
  153.      * @param DateTime $time
  154.      */
  155.     public function setTime(DateTime $time)
  156.     {
  157.         $this->time $time;
  158.     }
  159.     /**
  160.      * @param string $type
  161.      *
  162.      * @throws InvalidArgumentException
  163.      */
  164.     public function setType(string $type)
  165.     {
  166.         if ($type != self::TYPE_CREATE && $type != self::TYPE_STATUS && $type != self::TYPE_UPDATE) {
  167.             throw new InvalidArgumentException('Invalid log entry type.');
  168.         }
  169.         $this->type $type;
  170.     }
  171.     /**
  172.      * @param User $user
  173.      */
  174.     public function setUser(User $user)
  175.     {
  176.         $this->user $user;
  177.         $this->userName $user->getName();
  178.     }
  179. }