<?php
namespace Sq\Entity\Schema\ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* GoogleToken.
*/
#[ORM\Entity]
#[ORM\Table(name: 'google_token')]
#[ORM\Index(name: 'got_token', columns: ['got_token'])]
#[ORM\Index(name: 'got_m_id', columns: ['got_m_id'])]
#[ORM\UniqueConstraint(name: 'got_account_id__m_id', columns: ['got_account_id', 'got_m_id'])]
class GoogleToken
{
/**
* @var int
*/
#[ORM\Column(name: 'got_id', type: 'integer', nullable: false, options: ['unsigned' => true])]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
private $id;
/**
* @var Member
*/
#[ORM\ManyToOne(targetEntity: Member::class)]
#[ORM\JoinColumn(name: 'got_m_id', referencedColumnName: 'm_id', nullable: true)]
private $member;
/**
* @var Collection|User[]
*/
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'googleTokens')]
#[ORM\JoinTable(name: 'google_token_users', joinColumns: [new ORM\JoinColumn(name: 'gotu_got_id', referencedColumnName: 'got_id', onDelete: 'CASCADE')], inverseJoinColumns: [new ORM\JoinColumn(name: 'gotu_u_id', referencedColumnName: 'u_id', onDelete: 'CASCADE')])]
private $users;
/**
* @deprecated Kept for Doctrine mapping purposes, use $users collection instead
*
* @var User|null
*/
#[ORM\Column(name: 'got_u_id', type: 'integer', nullable: true, options: ['unsigned' => true])]
private $user;
/**
* @var string
*/
#[ORM\Column(name: 'got_account_id', type: 'string', length: 32, nullable: false)]
private $accountId;
/**
* @var string
*/
#[ORM\Column(name: 'got_token', type: 'text', length: 16777215, nullable: false)]
private $token;
/**
* @var string|null
*/
#[ORM\Column(name: 'got_refresh_token', type: 'text', length: 16777215, nullable: true)]
private $refreshToken;
/**
* @var string|null
*/
#[ORM\Column(name: 'got_token_owner_display_name', type: 'string', length: 255, nullable: true)]
private $tokenOwnerDisplayName;
/**
* @var string|null
*/
#[ORM\Column(name: 'got_token_owner_email', type: 'binary_aes_encrypted', precision: 0, scale: 0, nullable: true, unique: false)]
private $tokenOwnerEmail;
/**
* @var \DateTime|null
*/
#[ORM\Column(name: 'got_token_expiry', type: 'datetime', nullable: true)]
private $tokenExpiry;
/**
* @var \DateTime|null
*/
#[ORM\Column(name: 'got_token_last_checked', type: 'datetime', nullable: true)]
private $tokenLastChecked;
/**
* @var bool|null
*/
#[ORM\Column(name: 'got_error', type: 'boolean', nullable: true)]
private $error = false;
/**
* @var bool|null
*/
#[ORM\Column(name: 'got_deleted', type: 'boolean', nullable: true)]
private $deleted = false;
/**
* @var bool|null
*/
#[ORM\Column(name: 'got_login_enabled', type: 'boolean', nullable: true, options: ['default' => '1'])]
private $loginEnabled = true;
#[ORM\OneToMany(targetEntity: Google::class, mappedBy: 'googleToken')]
private $googleEntities;
#[ORM\OneToMany(targetEntity: YouTube::class, mappedBy: 'googleToken')]
private $youtubeEntities;
public function __construct(?Member $member, string $token, string $refreshToken, string $tokenOwnerDisplayName, ?string $tokenOwnerEmail = null, ?string $accountId = null, ?User $user = null)
{
$this->member = $member;
$this->accountId = $accountId;
$this->token = $token;
$this->refreshToken = $refreshToken;
$this->tokenOwnerDisplayName = $tokenOwnerDisplayName;
$this->tokenOwnerEmail = $tokenOwnerEmail;
$this->users = new ArrayCollection();
if ($user !== null)
{
$this->addUser($user);
}
$this->googleEntities = new ArrayCollection();
$this->youtubeEntities = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getMember(): ?Member
{
return $this->member;
}
// If a member has already been set, do not disassociate said member from the Google Token.
public function setMember(Member $member): self
{
$this->member = $member;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user))
{
$this->users->add($user);
}
return $this;
}
public function removeUser(User $user): self
{
$this->users->removeElement($user);
return $this;
}
public function getAccountId(): string
{
return $this->accountId;
}
public function setAccountId(string $accountId): self
{
$this->accountId = $accountId;
return $this;
}
public function getToken(): string
{
return $this->token;
}
public function setToken(string $token): self
{
$this->token = $token;
return $this;
}
public function getRefreshToken(): string
{
return $this->refreshToken;
}
public function setRefreshToken(string $refreshToken): self
{
$this->refreshToken = $refreshToken;
return $this;
}
public function getTokenOwnerDisplayName(): ?string
{
return $this->tokenOwnerDisplayName;
}
public function setTokenOwnerDisplayName(?string $tokenOwnerDisplayName): self
{
$this->tokenOwnerDisplayName = $tokenOwnerDisplayName;
return $this;
}
public function getTokenOwnerEmail(): ?string
{
return $this->tokenOwnerEmail;
}
public function setTokenOwnerEmail(?string $tokenOwnerEmail): self
{
$this->tokenOwnerEmail = $tokenOwnerEmail;
return $this;
}
public function getTokenExpiry(): ?\DateTimeInterface
{
return $this->tokenExpiry;
}
public function setTokenExpiry(?\DateTimeInterface $tokenExpiry): self
{
$this->tokenExpiry = $tokenExpiry;
return $this;
}
public function getTokenLastChecked(): ?\DateTimeInterface
{
return $this->tokenLastChecked;
}
public function setTokenLastChecked(?\DateTimeInterface $tokenLastChecked): self
{
$this->tokenLastChecked = $tokenLastChecked;
return $this;
}
public function hasError(): bool
{
return $this->error;
}
public function setError(bool $error): self
{
$this->error = $error;
return $this;
}
public function isDeleted(): bool
{
return $this->deleted;
}
public function setDeleted(bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
public function isLoginEnabled(): bool
{
return $this->loginEnabled;
}
public function setLoginEnabled(bool $loginEnabled): self
{
$this->loginEnabled = $loginEnabled;
return $this;
}
/**
* Get all connected Google social profiles for this google account/user token.
*
* @param bool $onlyActive
* If it should filter out all deleted profiles.
*
* @return \Doctrine\Common\Collections\Collection<GoogleSocialProfile>
*/
public function getGoogleSocialProfiles(bool $onlyActive = false): Collection
{
return $this->googleEntities
->filter(fn (Google $googleEntity): bool => !$onlyActive || !$googleEntity->isDeleted())
->map(fn (Google $googleEntity): GoogleSocialProfile => $googleEntity->getSocialProfile());
}
/**
* Get all connected YouTube social profiles for this google account/user token.
*
* @param bool $onlyActive
* If it should filter out all deleted profiles.
*
* @return \Doctrine\Common\Collections\Collection<YouTubeSocialProfile>
*/
public function getYouTubeSocialProfiles(bool $onlyActive = false): Collection
{
return $this->youtubeEntities
->filter(fn (YouTube $youtubeEntity): bool => !$onlyActive || !$youtubeEntity->isDeleted())
->map(fn (YouTube $youtubeEntity): YouTubeSocialProfile => $youtubeEntity->getSocialProfile());
}
public function addGoogleEntity(Google $accountEntity): self
{
if (!$this->googleEntities->contains($accountEntity))
{
$this->googleEntities->add($accountEntity);
}
return $this;
}
public function removeGoogleEntity(Google $accountEntity): self
{
$this->googleEntities->removeElement($accountEntity);
return $this;
}
public function addYouTubeEntity(YouTube $accountEntity): self
{
if (!$this->youtubeEntities->contains($accountEntity))
{
$this->youtubeEntities->add($accountEntity);
}
return $this;
}
public function removeYouTubeEntity(YouTube $accountEntity): self
{
$this->youtubeEntities->removeElement($accountEntity);
return $this;
}
/**
* Is the Google token expired? If so, we will need to request a new one using the refresh token.
*
* @return bool
*/
public function isTokenExpired(): bool
{
return $this->tokenExpiry < server_datetime();
}
}