src/Entity/MemberOnline.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use JMS\Serializer\Annotation\Groups;
  5. /**
  6.  * @ORM\Table("MemberOnline", indexes={
  7.  *
  8.  *     @ORM\Index(name="order_score", fields={"order_score"}),
  9.  *     @ORM\Index(name="MemberOnline_last_is_online_at_idx", fields={"last_online_at", "is_online"})
  10.  * })
  11.  *
  12.  * @ORM\HasLifecycleCallbacks()
  13.  *
  14.  * @ORM\Entity(repositoryClass="App\Repository\MemberOnlineRepository")
  15.  */
  16. class MemberOnline
  17. {
  18.     /**
  19.      * @Groups({"adminindex", "memberonlineindex"})
  20.      *
  21.      * @ORM\Id
  22.      *
  23.      * @ORM\OneToOne(targetEntity="App\Entity\Member", inversedBy="member_online", cascade={"persist"})
  24.      *
  25.      * @ORM\JoinColumn(name="member_id", referencedColumnName="id", onDelete="CASCADE")
  26.      */
  27.     private $member;
  28.     /**
  29.      * @Groups({"adminindex", "memberonlineindex", "memberfrontendindex", "amateur_search"})
  30.      *
  31.      * @ORM\Column(type="boolean", nullable=false, options={"default" = 1})
  32.      */
  33.     private bool $is_online true;
  34.     /**
  35.      * @Groups({"adminindex", "memberonlineindex", "memberfrontendindex", "amateur_search"})
  36.      *
  37.      * @ORM\Column(type="decimal", precision=8, scale=7, nullable=true)
  38.      */
  39.     private ?float $order_score null;
  40.     /**
  41.      * @Groups({"memberonlineindex", "memberfrontendindex", "amateur_search"})
  42.      *
  43.      * @ORM\Column(type="datetime", nullable=false)
  44.      */
  45.     private $last_online_at;
  46.     /**
  47.      * Legacy field. Live DB differs from dev.
  48.      *
  49.      * @Groups({"memberonlineindex", "memberfrontendindex"})
  50.      *
  51.      * @ORM\Column(type="datetime", nullable=true)
  52.      *
  53.      * @var \DateTime
  54.      */
  55.     private $updated_at;
  56.     /**
  57.      * @ORM\PrePersist()
  58.      */
  59.     public function prePersist()
  60.     {
  61.         if (!$this->getOrderScore()) {
  62.             $this->setOrderScore($this->getMember()->getSex() + mt_rand() / mt_getrandmax());
  63.         }
  64.     }
  65.     public function getLastOnlineAt(): ?\DateTimeInterface
  66.     {
  67.         return $this->last_online_at;
  68.     }
  69.     /**
  70.      * @param \DateTimeInterface|null $dateTime
  71.      */
  72.     public function setLastOnlineAt(\DateTimeInterface $dateTime): self
  73.     {
  74.         $this->last_online_at $dateTime;
  75.         return $this;
  76.     }
  77.     public function getMember(): ?Member
  78.     {
  79.         return $this->member;
  80.     }
  81.     public function setMember(Member $member): self
  82.     {
  83.         $this->member $member;
  84.         return $this;
  85.     }
  86.     public function getIsOnline(): bool
  87.     {
  88.         return $this->is_online;
  89.     }
  90.     public function setIsOnline(bool $is_online): self
  91.     {
  92.         $this->is_online $is_online;
  93.         return $this;
  94.     }
  95.     public function getOrderScore(): ?float
  96.     {
  97.         return $this->order_score;
  98.     }
  99.     public function setOrderScore($order_score): self
  100.     {
  101.         $this->order_score = (float) $order_score;
  102.         return $this;
  103.     }
  104.     public function getIsOnlineWithBusinessLogic(): bool
  105.     {
  106.         if (!$this->getIsOnline() || !$this->getLastOnlineAt()) {
  107.             return false;
  108.         }
  109.         $last $this->getLastOnlineAt();
  110.         return $last self::getLastOnlineAgeDateTime();
  111.     }
  112.     public static function getLastOnlineAgeDateTime(): \DateTime
  113.     {
  114.         return new \DateTime('10 minutes ago');
  115.     }
  116. }