src/Entity/Device.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\DeviceRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=DeviceRepository::class)
  9.  */
  10. class Device
  11. {
  12.     const SYNC_INTERVAL_SECONDS 10;
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="devices")
  21.      * @ORM\JoinColumn(nullable=false)
  22.      */
  23.     private $user;
  24.     /**
  25.      * @ORM\Column(type="text", nullable=true)
  26.      */
  27.     private $userAgent;
  28.     /**
  29.      * @ORM\Column(type="datetime")
  30.      */
  31.     private $createdAt;
  32.     /**
  33.      * @ORM\Column(type="datetime", nullable=true)
  34.      */
  35.     private $lastOnline;
  36.     /**
  37.      * @ORM\Column(type="string", length=255, nullable=true)
  38.      */
  39.     private $name;
  40.     /**
  41.      * @ORM\Column(type="string", length=255, nullable=true)
  42.      */
  43.     private $platform;
  44.     /**
  45.      * @ORM\OneToMany(targetEntity=AccessToken::class, mappedBy="device")
  46.      */
  47.     private $accessTokens;
  48.     /**
  49.      * @ORM\Column(type="string", length=255, unique=true)
  50.      */
  51.     private $deviceId;
  52.     /**
  53.      * @ORM\Column(type="datetime", nullable=true)
  54.      */
  55.     private $lastSyncedAt;
  56.     /**
  57.      * @ORM\OneToOne(targetEntity=Cursor::class, inversedBy="device", cascade={"persist", "remove"})
  58.      */
  59.     private $syncCursor;
  60.     public function __construct()
  61.     {
  62.         $this->accessTokens = new ArrayCollection();
  63.         $this->createdAt = new \DateTime();
  64.     }
  65.     public function getId(): ?int
  66.     {
  67.         return $this->id;
  68.     }
  69.     public function getUser(): ?User
  70.     {
  71.         return $this->user;
  72.     }
  73.     public function setUser(?User $user): self
  74.     {
  75.         $this->user $user;
  76.         return $this;
  77.     }
  78.     public function getUserAgent(): ?string
  79.     {
  80.         return $this->userAgent;
  81.     }
  82.     public function setUserAgent(?string $userAgent): self
  83.     {
  84.         $this->userAgent $userAgent;
  85.         return $this;
  86.     }
  87.     public function getCreatedAt(): ?\DateTimeInterface
  88.     {
  89.         return $this->createdAt;
  90.     }
  91.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  92.     {
  93.         $this->createdAt $createdAt;
  94.         return $this;
  95.     }
  96.     public function getLastOnline(): ?\DateTimeInterface
  97.     {
  98.         return $this->lastOnline;
  99.     }
  100.     public function setLastOnline(?\DateTimeInterface $lastOnline): self
  101.     {
  102.         $this->lastOnline $lastOnline;
  103.         return $this;
  104.     }
  105.     public function getName(): ?string
  106.     {
  107.         return $this->name;
  108.     }
  109.     public function setName(?string $name): self
  110.     {
  111.         $this->name $name;
  112.         return $this;
  113.     }
  114.     public function getPlatform(): ?string
  115.     {
  116.         return $this->platform;
  117.     }
  118.     public function setPlatform(?string $platform): self
  119.     {
  120.         $this->platform $platform;
  121.         return $this;
  122.     }
  123.     /**
  124.      * @return Collection<int, AccessToken>
  125.      */
  126.     public function getAccessTokens(): Collection
  127.     {
  128.         return $this->accessTokens;
  129.     }
  130.     public function addAccessToken(AccessToken $accessToken): self
  131.     {
  132.         if (!$this->accessTokens->contains($accessToken)) {
  133.             $this->accessTokens[] = $accessToken;
  134.             $accessToken->setDevice($this);
  135.         }
  136.         return $this;
  137.     }
  138.     public function removeAccessToken(AccessToken $accessToken): self
  139.     {
  140.         if ($this->accessTokens->removeElement($accessToken)) {
  141.             // set the owning side to null (unless already changed)
  142.             if ($accessToken->getDevice() === $this) {
  143.                 $accessToken->setDevice(null);
  144.             }
  145.         }
  146.         return $this;
  147.     }
  148.     public function getDeviceId(): ?string
  149.     {
  150.         return $this->deviceId;
  151.     }
  152.     public function setDeviceId(string $deviceId): self
  153.     {
  154.         $this->deviceId $deviceId;
  155.         return $this;
  156.     }
  157.     public function getLastSyncedAt(): ?\DateTimeInterface
  158.     {
  159.         return $this->lastSyncedAt;
  160.     }
  161.     public function setLastSyncedAt(?\DateTimeInterface $lastSyncedAt): self
  162.     {
  163.         $this->lastSyncedAt $lastSyncedAt;
  164.         return $this;
  165.     }
  166.     public function shouldSync(): bool
  167.     {
  168.         if (!$this->lastSyncedAt) {
  169.             return true;
  170.         }
  171.         return $this->lastSyncedAt < new \DateTime('-' self::SYNC_INTERVAL_SECONDS ' seconds');
  172.     }
  173.     public function getCursor(): ?Cursor
  174.     {
  175.         return $this->syncCursor;
  176.     }
  177.     public function setCursor(?Cursor $syncCursor): self
  178.     {
  179.         $this->syncCursor $syncCursor;
  180.         return $this;
  181.     }
  182. }