<?php
namespace EADPlataforma\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use EADPlataforma\Validator\Constraints as EADAssert;
use EADPlataforma\Enum\SessionEnum;
use \DateTime;
/**
* Session
*
* @ORM\Table(name="session", indexes={
* @ORM\Index(name="fk_session_user_id", columns={"user_id"}),
* @ORM\Index(name="fk_session_user_origin_id", columns={"user_origin_id"})
* })
*
* @ORM\Entity(repositoryClass="EADPlataforma\Repository\SessionRepository")
*/
class Session
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @Assert\NotBlank(
* message = "Deleted not informed"
* )
*
* @Assert\Choice(
* choices = {
* SessionEnum::ITEM_NO_DELETED,
* SessionEnum::ITEM_ON_TRASH,
* SessionEnum::ITEM_DELETED
* },
* message = "Delete Option Invalid"
* )
*
* @var int
*
* @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
*/
private $deleted = SessionEnum::ITEM_NO_DELETED;
/**
* @Assert\NotBlank(
* message = "Token not informed"
* )
*
* @var string
*
* @ORM\Column(name="token", type="string", length=45, nullable=false)
*/
private $token;
/**
* @var string
*
* @ORM\Column(name="ip", type="string", length=45, nullable=true)
*/
private $ip;
/**
* @var string
*
* @ORM\Column(name="coordinate", type="string", length=60, nullable=true)
*/
private $coordinate;
/**
* @var string
*
* @ORM\Column(name="time_zone", type="string", length=60, nullable=true)
*/
private $timeZone;
/**
* @var string
*
* @ORM\Column(name="isp_name", type="string", length=150, nullable=true)
*/
private $ispName;
/**
* @Assert\NotBlank(
* message = "isMobile not informed"
* )
*
* @Assert\Choice(
* choices = { SessionEnum::NO, SessionEnum::YES },
* message = "isMobile Invalid"
* )
*
* @var int
*
* @ORM\Column(name="is_mobile", type="integer", nullable=false, options={"default"="0"})
*/
private $isMobile = SessionEnum::NO;
/**
* @Assert\NotBlank(
* message = "isAdmin not informed"
* )
*
* @Assert\Choice(
* choices = { SessionEnum::NO, SessionEnum::YES },
* message = "isAdmin Invalid"
* )
*
* @var int
*
* @ORM\Column(name="is_admin", type="integer", nullable=false, options={"default"="0"})
*/
private $isAdmin = SessionEnum::NO;
/**
* @Assert\NotBlank(
* message = "Date Register not informed"
* )
*
* @EADAssert\DateTimeEAD(
* message = "Date Register Invalid"
* )
*
* @var \DateTime
*
* @ORM\Column(name="date_register", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
*/
private $dateRegister;
/**
* @Assert\NotBlank(
* message = "Date Expire not informed"
* )
*
* @EADAssert\DateTimeEAD(
* message = "Date Expire Invalid"
* )
*
* @var \DateTime
*
* @ORM\Column(name="date_expire", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
*/
private $dateExpire;
/**
* @var string
*
* @ORM\Column(name="old_information", type="text", nullable=true)
*/
private $oldInformation;
/**
* @var string
*
* @ORM\Column(name="user_agent", type="text", nullable=true)
*/
private $userAgent;
/**
* @Assert\Json(
* message = "Permission is an invalid Json."
* )
*
* @var string
*
* @ORM\Column(name="permission", type="text", nullable=true)
*/
private $permission;
/**
* @Assert\NotBlank(
* message = "User not informed"
* )
*
* @Assert\Valid
*
* @var \User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
* })
*/
private $user;
/**
* @Assert\Valid
*
* @var \User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_origin_id", referencedColumnName="id", nullable=true)
* })
*/
private $userOrigin;
/**
* @Assert\Valid
*
* @var \City
*
* @ORM\ManyToOne(targetEntity="City")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="city_id", referencedColumnName="id", nullable=true)
* })
*/
private $city;
/**
* @Assert\Valid
*
* @var \State
*
* @ORM\ManyToOne(targetEntity="State")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="state_id", referencedColumnName="id", nullable=true)
* })
*/
private $state;
/**
* @Assert\Valid
*
* @var \Country
*
* @ORM\ManyToOne(targetEntity="Country")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
* })
*/
private $country;
/**
* Constructor
*/
public function __construct()
{
$this->setDateRegister(date('Y-m-d H:i:s'));
$this->setDateExpire(date('Y-m-d H:i:s', strtotime(' + 1 day ')));
}
public function getId(): ?int
{
return $this->id;
}
public function setToken(?string $token): ?string
{
$this->token = $token;
return $this->token;
}
public function getToken(): ?string
{
return $this->token;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(?string $ip): ?self
{
$this->ip = $ip;
return $this;
}
public function getCoordinate(): ?string
{
return $this->coordinate;
}
public function setCoordinate(?string $coordinate): ?self
{
$this->coordinate = $coordinate;
return $this;
}
public function getTimeZone(): ?string
{
return $this->timeZone;
}
public function setTimeZone(?string $timeZone): ?self
{
$this->timeZone = $timeZone;
return $this;
}
public function getIspName(): ?string
{
return $this->ispName;
}
public function setIspName(?string $ispName): ?self
{
$this->ispName = $ispName;
return $this;
}
public function getIsMobile(): ?int
{
return $this->isMobile;
}
public function setIsMobile(?int $isMobile): ?self
{
$this->isMobile = $isMobile;
return $this;
}
public function getIsAdmin(): ?int
{
return $this->isAdmin;
}
public function setIsAdmin(?int $isAdmin): ?self
{
$this->isAdmin = $isAdmin;
return $this;
}
public function getDateRegister($dateFormat = 'Y-m-d H:i:s')
{
if(!empty($this->dateRegister)){
return $this->dateRegister->format($dateFormat);
}
return $this->dateRegister;
}
public function setDateRegister($dateRegister): self
{
if(!empty($dateRegister)){
$dateRegister = DateTime::createFromFormat('Y-m-d H:i:s', $dateRegister);
}
$this->dateRegister = $dateRegister;
return $this;
}
public function getDateExpire($noFormat = false, $dateFormat = 'Y-m-d H:i:s')
{
if(!$noFormat){
if(!empty($this->dateExpire)){
return $this->dateExpire->format($dateFormat);
}
}
return $this->dateExpire;
}
public function setDateExpire($dateExpire): self
{
if(!empty($dateExpire)){
$dateExpire = DateTime::createFromFormat('Y-m-d H:i:s', $dateExpire);
}
$this->dateExpire = $dateExpire;
return $this;
}
public function getOldInformation(): ?string
{
return $this->oldInformation;
}
public function setOldInformation(?string $oldInformation): self
{
$this->oldInformation = $oldInformation;
return $this;
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function setUserAgent(?string $userAgent): self
{
$this->userAgent = $userAgent;
return $this;
}
public function getPermission(): ?string
{
return $this->permission;
}
public function setPermission(?string $permission): self
{
$this->permission = $permission;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
$this->token = md5(
$this->getDateRegister() . $this->getDateExpire() . $this->user->getId() . time()
);
return $this;
}
public function getUserOrigin(): ?User
{
return $this->userOrigin;
}
public function setUserOrigin(?User $userOrigin): self
{
$this->userOrigin = $userOrigin;
return $this;
}
public function getCity(): ?City
{
return $this->city;
}
public function setCity(?City $city): self
{
$this->city = $city;
return $this;
}
public function getState(): ?State
{
return $this->state;
}
public function setState(?State $state): self
{
$this->state = $state;
return $this;
}
public function getCountry(): ?Country
{
return $this->country;
}
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
public function isValid(): bool
{
$dateExpire = $this->getDateExpire(true);
$today = new DateTime();
if(!$this->isDeleted()){
return ($today <= $dateExpire);
}
return false;
}
public function isOnTrash(): bool
{
return ($this->deleted == SessionEnum::ITEM_ON_TRASH);
}
public function isDeleted(): bool
{
return ($this->deleted == SessionEnum::ITEM_DELETED);
}
public function restore(): self
{
$this->deleted = SessionEnum::ITEM_NO_DELETED;
return $this;
}
public function trash(): self
{
$this->deleted = SessionEnum::ITEM_ON_TRASH;
return $this;
}
public function delete(): self
{
$this->deleted = SessionEnum::ITEM_DELETED;
return $this;
}
public function toReturn()
{
$data = [
"id" => $this->id,
"token" => $this->token,
"ip" => $this->ip,
"coordinate" => $this->coordinate,
"timeZone" => $this->timeZone,
"ispName" => $this->ispName,
"ispName" => $this->ispName,
"isMobile" => $this->isMobile,
"isAdmin" => $this->isAdmin,
"dateRegister" => $this->getDateRegister(),
"dateExpire" => $this->getDateExpire(),
"oldInformation" => $this->oldInformation,
"userAgent" => $this->userAgent,
"permission" => $this->permission,
"user" => ($this->user ? $this->user->getId() : null),
"userOrigin" => ($this->userOrigin ? $this->userOrigin->getId() : null),
"city" => ($this->city ? $this->city->getId() : null),
"state" => ($this->state ? $this->state->getId() : null),
"country" => ($this->country ? $this->country->getId() : null),
"deleted" => $this->deleted,
];
return $data;
}
}