src/Entity/Session.php line 21

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use EADPlataforma\Validator\Constraints as EADAssert;
  6. use EADPlataforma\Enum\SessionEnum;
  7. use \DateTime;
  8. /**
  9.  * Session
  10.  *
  11.  * @ORM\Table(name="session", indexes={
  12.  *      @ORM\Index(name="fk_session_user_id", columns={"user_id"}),
  13.  *      @ORM\Index(name="fk_session_user_origin_id", columns={"user_origin_id"})
  14.  * })
  15.  *
  16.  * @ORM\Entity(repositoryClass="EADPlataforma\Repository\SessionRepository")
  17.  */
  18. class Session
  19. {
  20.     /**
  21.      * @var int
  22.      *
  23.      * @ORM\Column(name="id", type="integer", nullable=false)
  24.      * @ORM\Id
  25.      * @ORM\GeneratedValue(strategy="IDENTITY")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @Assert\NotBlank(
  30.      *      message = "Deleted not informed"
  31.      * )
  32.      *
  33.      * @Assert\Choice(
  34.      *      choices = { 
  35.      *                      SessionEnum::ITEM_NO_DELETED, 
  36.      *                      SessionEnum::ITEM_ON_TRASH,
  37.      *                      SessionEnum::ITEM_DELETED
  38.      *                },
  39.      *      message = "Delete Option Invalid"
  40.      * )
  41.      *
  42.      * @var int
  43.      *
  44.      * @ORM\Column(name="deleted", type="integer", nullable=false, options={"default"="0"})
  45.      */
  46.     private $deleted SessionEnum::ITEM_NO_DELETED;
  47.     /**
  48.      * @Assert\NotBlank(
  49.      *      message = "Token not informed"
  50.      * )
  51.      *
  52.      * @var string
  53.      *
  54.      * @ORM\Column(name="token", type="string", length=45, nullable=false)
  55.      */
  56.     private $token;
  57.     /**
  58.      * @var string
  59.      *
  60.      * @ORM\Column(name="ip", type="string", length=45, nullable=true)
  61.      */
  62.     private $ip;
  63.     /**
  64.      * @var string
  65.      *
  66.      * @ORM\Column(name="coordinate", type="string", length=60, nullable=true)
  67.      */
  68.     private $coordinate;
  69.     /**
  70.      * @var string
  71.      *
  72.      * @ORM\Column(name="time_zone", type="string", length=60, nullable=true)
  73.      */
  74.     private $timeZone;
  75.     /**
  76.      * @var string
  77.      *
  78.      * @ORM\Column(name="isp_name", type="string", length=150, nullable=true)
  79.      */
  80.     private $ispName;
  81.     /**
  82.      * @Assert\NotBlank(
  83.      *      message = "isMobile not informed"
  84.      * )
  85.      *
  86.      * @Assert\Choice(
  87.      *      choices = { SessionEnum::NO, SessionEnum::YES },
  88.      *      message = "isMobile Invalid"
  89.      * )
  90.      *
  91.      * @var int
  92.      *
  93.      * @ORM\Column(name="is_mobile", type="integer", nullable=false, options={"default"="0"})
  94.      */
  95.     private $isMobile SessionEnum::NO;
  96.     /**
  97.      * @Assert\NotBlank(
  98.      *      message = "isAdmin not informed"
  99.      * )
  100.      *
  101.      * @Assert\Choice(
  102.      *      choices = { SessionEnum::NO, SessionEnum::YES },
  103.      *      message = "isAdmin Invalid"
  104.      * )
  105.      *
  106.      * @var int
  107.      *
  108.      * @ORM\Column(name="is_admin", type="integer", nullable=false, options={"default"="0"})
  109.      */
  110.     private $isAdmin SessionEnum::NO;
  111.     /**
  112.      * @Assert\NotBlank(
  113.      *      message = "Date Register not informed"
  114.      * )
  115.      *
  116.      * @EADAssert\DateTimeEAD(
  117.      *      message = "Date Register Invalid"
  118.      * )
  119.      *
  120.      * @var \DateTime
  121.      *
  122.      * @ORM\Column(name="date_register", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
  123.      */
  124.     private $dateRegister;
  125.     /**
  126.      * @Assert\NotBlank(
  127.      *      message = "Date Expire not informed"
  128.      * )
  129.      *
  130.      * @EADAssert\DateTimeEAD(
  131.      *      message = "Date Expire Invalid"
  132.      * )
  133.      *
  134.      * @var \DateTime
  135.      *
  136.      * @ORM\Column(name="date_expire", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
  137.      */
  138.     private $dateExpire;
  139.     /**
  140.      * @var string
  141.      *
  142.      * @ORM\Column(name="old_information", type="text", nullable=true)
  143.      */
  144.     private $oldInformation;
  145.     /**
  146.      * @var string
  147.      *
  148.      * @ORM\Column(name="user_agent", type="text", nullable=true)
  149.      */
  150.     private $userAgent;
  151.     /**
  152.      * @Assert\Json(
  153.      *     message = "Permission is an invalid Json."
  154.      * )
  155.      *
  156.      * @var string
  157.      *
  158.      * @ORM\Column(name="permission", type="text", nullable=true)
  159.      */
  160.     private $permission;
  161.     /**
  162.      * @Assert\NotBlank(
  163.      *      message = "User not informed"
  164.      * )
  165.      *
  166.      * @Assert\Valid
  167.      *
  168.      * @var \User
  169.      *
  170.      * @ORM\ManyToOne(targetEntity="User")
  171.      * @ORM\JoinColumns({
  172.      *   @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
  173.      * })
  174.      */
  175.     private $user;
  176.     /**
  177.      * @Assert\Valid
  178.      *
  179.      * @var \User
  180.      *
  181.      * @ORM\ManyToOne(targetEntity="User")
  182.      * @ORM\JoinColumns({
  183.      *   @ORM\JoinColumn(name="user_origin_id", referencedColumnName="id", nullable=true)
  184.      * })
  185.      */
  186.     private $userOrigin;
  187.     /**
  188.      * @Assert\Valid
  189.      *
  190.      * @var \City
  191.      *
  192.      * @ORM\ManyToOne(targetEntity="City")
  193.      * @ORM\JoinColumns({
  194.      *   @ORM\JoinColumn(name="city_id", referencedColumnName="id", nullable=true)
  195.      * })
  196.      */
  197.     private $city;
  198.     /**
  199.      * @Assert\Valid
  200.      *
  201.      * @var \State
  202.      *
  203.      * @ORM\ManyToOne(targetEntity="State")
  204.      * @ORM\JoinColumns({
  205.      *   @ORM\JoinColumn(name="state_id", referencedColumnName="id", nullable=true)
  206.      * })
  207.      */
  208.     private $state;
  209.     /**
  210.      * @Assert\Valid
  211.      *
  212.      * @var \Country
  213.      *
  214.      * @ORM\ManyToOne(targetEntity="Country")
  215.      * @ORM\JoinColumns({
  216.      *   @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
  217.      * })
  218.      */
  219.     private $country;
  220.     /**
  221.      * Constructor
  222.      */
  223.     public function __construct()
  224.     {
  225.         $this->setDateRegister(date('Y-m-d H:i:s'));
  226.         $this->setDateExpire(date('Y-m-d H:i:s'strtotime(' + 1 day ')));
  227.     }
  228.     public function getId(): ?int
  229.     {
  230.         return $this->id;
  231.     }
  232.     public function setToken(?string $token): ?string
  233.     {
  234.         $this->token $token
  235.         return $this->token;
  236.     }
  237.     public function getToken(): ?string
  238.     {
  239.         return $this->token;
  240.     }
  241.     public function getIp(): ?string
  242.     {
  243.         return $this->ip;
  244.     }
  245.     public function setIp(?string $ip): ?self
  246.     {
  247.         $this->ip $ip
  248.         return $this;
  249.     }
  250.     public function getCoordinate(): ?string
  251.     {
  252.         return $this->coordinate;
  253.     }
  254.     public function setCoordinate(?string $coordinate): ?self
  255.     {
  256.         $this->coordinate $coordinate
  257.         
  258.         return $this;
  259.     }
  260.     public function getTimeZone(): ?string
  261.     {
  262.         return $this->timeZone;
  263.     }
  264.     public function setTimeZone(?string $timeZone): ?self
  265.     {
  266.         $this->timeZone $timeZone
  267.         
  268.         return $this;
  269.     }
  270.     public function getIspName(): ?string
  271.     {
  272.         return $this->ispName;
  273.     }
  274.     public function setIspName(?string $ispName): ?self
  275.     {
  276.         $this->ispName $ispName
  277.         
  278.         return $this;
  279.     }
  280.     public function getIsMobile(): ?int
  281.     {
  282.         return $this->isMobile;
  283.     }
  284.     public function setIsMobile(?int $isMobile): ?self
  285.     {
  286.         $this->isMobile $isMobile
  287.         
  288.         return $this;
  289.     }
  290.     public function getIsAdmin(): ?int
  291.     {
  292.         return $this->isAdmin;
  293.     }
  294.     public function setIsAdmin(?int $isAdmin): ?self
  295.     {
  296.         $this->isAdmin $isAdmin
  297.         
  298.         return $this;
  299.     }
  300.     public function getDateRegister($dateFormat 'Y-m-d H:i:s')
  301.     {
  302.         if(!empty($this->dateRegister)){
  303.             return $this->dateRegister->format($dateFormat);
  304.         }
  305.         return $this->dateRegister;
  306.     }
  307.     public function setDateRegister($dateRegister): self
  308.     {
  309.         if(!empty($dateRegister)){
  310.             $dateRegister DateTime::createFromFormat('Y-m-d H:i:s'$dateRegister);
  311.         }
  312.         $this->dateRegister $dateRegister;
  313.         return $this;
  314.     }
  315.     public function getDateExpire($noFormat false$dateFormat 'Y-m-d H:i:s')
  316.     {   
  317.         if(!$noFormat){
  318.             if(!empty($this->dateExpire)){
  319.                 return $this->dateExpire->format($dateFormat);
  320.             }
  321.         }
  322.         
  323.         return $this->dateExpire;
  324.     }
  325.     public function setDateExpire($dateExpire): self
  326.     {
  327.         if(!empty($dateExpire)){
  328.             $dateExpire DateTime::createFromFormat('Y-m-d H:i:s'$dateExpire);
  329.         }
  330.         
  331.         $this->dateExpire $dateExpire;
  332.         return $this;
  333.     }
  334.     public function getOldInformation(): ?string
  335.     {   
  336.         return $this->oldInformation;
  337.     }
  338.     public function setOldInformation(?string $oldInformation): self
  339.     {
  340.         $this->oldInformation $oldInformation;
  341.         return $this;
  342.     }
  343.     
  344.     public function getUserAgent(): ?string
  345.     {   
  346.         return $this->userAgent;
  347.     }
  348.     public function setUserAgent(?string $userAgent): self
  349.     {
  350.         $this->userAgent $userAgent;
  351.         return $this;
  352.     }
  353.     public function getPermission(): ?string
  354.     {
  355.         return $this->permission;
  356.     }
  357.     public function setPermission(?string $permission): self
  358.     {
  359.         $this->permission $permission;
  360.         return $this;
  361.     }
  362.     public function getUser(): ?User
  363.     {
  364.         return $this->user;
  365.     }
  366.     public function setUser(?User $user): self
  367.     {
  368.         $this->user $user;
  369.         $this->token md5
  370.             $this->getDateRegister() . $this->getDateExpire() . $this->user->getId() . time()
  371.         );
  372.         return $this;
  373.     }
  374.     public function getUserOrigin(): ?User
  375.     {
  376.         return $this->userOrigin;
  377.     }
  378.     public function setUserOrigin(?User $userOrigin): self
  379.     {
  380.         $this->userOrigin $userOrigin;
  381.         return $this;
  382.     }
  383.     public function getCity(): ?City
  384.     {
  385.         return $this->city;
  386.     }
  387.     public function setCity(?City $city): self
  388.     {
  389.         $this->city $city;
  390.         return $this;
  391.     }
  392.     public function getState(): ?State
  393.     {
  394.         return $this->state;
  395.     }
  396.     public function setState(?State $state): self
  397.     {
  398.         $this->state $state;
  399.         return $this;
  400.     }
  401.     public function getCountry(): ?Country
  402.     {
  403.         return $this->country;
  404.     }
  405.     public function setCountry(?Country $country): self
  406.     {
  407.         $this->country $country;
  408.         return $this;
  409.     }
  410.     public function isValid(): bool
  411.     {
  412.         $dateExpire $this->getDateExpire(true);
  413.         $today = new DateTime();
  414.         if(!$this->isDeleted()){
  415.             return ($today <= $dateExpire);
  416.         }
  417.         return false;
  418.     }
  419.     public function isOnTrash(): bool
  420.     {
  421.         return ($this->deleted == SessionEnum::ITEM_ON_TRASH);
  422.     }
  423.     public function isDeleted(): bool
  424.     {
  425.         return ($this->deleted == SessionEnum::ITEM_DELETED);
  426.     }
  427.     public function restore(): self
  428.     {
  429.         $this->deleted SessionEnum::ITEM_NO_DELETED;
  430.         return $this;
  431.     }
  432.     public function trash(): self
  433.     {
  434.         $this->deleted SessionEnum::ITEM_ON_TRASH;
  435.         return $this;
  436.     }
  437.     public function delete(): self
  438.     {
  439.         $this->deleted SessionEnum::ITEM_DELETED;
  440.         return $this;
  441.     }
  442.     public function toReturn()
  443.     {
  444.         $data = [
  445.             "id" => $this->id,
  446.             "token" => $this->token,
  447.             "ip" => $this->ip,
  448.             "coordinate" => $this->coordinate,
  449.             "timeZone" => $this->timeZone,
  450.             "ispName" => $this->ispName,
  451.             "ispName" => $this->ispName,
  452.             "isMobile" => $this->isMobile,
  453.             "isAdmin" => $this->isAdmin,
  454.             "dateRegister" => $this->getDateRegister(),
  455.             "dateExpire" => $this->getDateExpire(),
  456.             "oldInformation" => $this->oldInformation,
  457.             "userAgent" => $this->userAgent,
  458.             "permission" => $this->permission,
  459.             "user" => ($this->user $this->user->getId() : null),
  460.             "userOrigin" => ($this->userOrigin $this->userOrigin->getId() : null),
  461.             "city" => ($this->city $this->city->getId() : null),
  462.             "state" => ($this->state $this->state->getId() : null),
  463.             "country" => ($this->country $this->country->getId() : null),
  464.             "deleted" => $this->deleted,
  465.         ];
  466.         return $data;
  467.     }
  468. }