<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Table("MemberOnline", indexes={
*
* @ORM\Index(name="order_score", fields={"order_score"}),
* @ORM\Index(name="MemberOnline_last_is_online_at_idx", fields={"last_online_at", "is_online"})
* })
*
* @ORM\HasLifecycleCallbacks()
*
* @ORM\Entity(repositoryClass="App\Repository\MemberOnlineRepository")
*/
class MemberOnline
{
/**
* @Groups({"adminindex", "memberonlineindex"})
*
* @ORM\Id
*
* @ORM\OneToOne(targetEntity="App\Entity\Member", inversedBy="member_online", cascade={"persist"})
*
* @ORM\JoinColumn(name="member_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $member;
/**
* @Groups({"adminindex", "memberonlineindex", "memberfrontendindex", "amateur_search"})
*
* @ORM\Column(type="boolean", nullable=false, options={"default" = 1})
*/
private bool $is_online = true;
/**
* @Groups({"adminindex", "memberonlineindex", "memberfrontendindex", "amateur_search"})
*
* @ORM\Column(type="decimal", precision=8, scale=7, nullable=true)
*/
private ?float $order_score = null;
/**
* @Groups({"memberonlineindex", "memberfrontendindex", "amateur_search"})
*
* @ORM\Column(type="datetime", nullable=false)
*/
private $last_online_at;
/**
* Legacy field. Live DB differs from dev.
*
* @Groups({"memberonlineindex", "memberfrontendindex"})
*
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime
*/
private $updated_at;
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
if (!$this->getOrderScore()) {
$this->setOrderScore($this->getMember()->getSex() + mt_rand() / mt_getrandmax());
}
}
public function getLastOnlineAt(): ?\DateTimeInterface
{
return $this->last_online_at;
}
/**
* @param \DateTimeInterface|null $dateTime
*/
public function setLastOnlineAt(\DateTimeInterface $dateTime): self
{
$this->last_online_at = $dateTime;
return $this;
}
public function getMember(): ?Member
{
return $this->member;
}
public function setMember(Member $member): self
{
$this->member = $member;
return $this;
}
public function getIsOnline(): bool
{
return $this->is_online;
}
public function setIsOnline(bool $is_online): self
{
$this->is_online = $is_online;
return $this;
}
public function getOrderScore(): ?float
{
return $this->order_score;
}
public function setOrderScore($order_score): self
{
$this->order_score = (float) $order_score;
return $this;
}
public function getIsOnlineWithBusinessLogic(): bool
{
if (!$this->getIsOnline() || !$this->getLastOnlineAt()) {
return false;
}
$last = $this->getLastOnlineAt();
return $last > self::getLastOnlineAgeDateTime();
}
public static function getLastOnlineAgeDateTime(): \DateTime
{
return new \DateTime('10 minutes ago');
}
}