app/Entity/Schema/ORM/GoogleToken.php line 17

Open in your IDE?
  1. <?php
  2. namespace Sq\Entity\Schema\ORM;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * GoogleToken.
  8.  */
  9. #[ORM\Entity]
  10. #[ORM\Table(name'google_token')]
  11. #[ORM\Index(name'got_token'columns: ['got_token'])]
  12. #[ORM\Index(name'got_m_id'columns: ['got_m_id'])]
  13. #[ORM\UniqueConstraint(name'got_account_id__m_id'columns: ['got_account_id''got_m_id'])]
  14. class GoogleToken
  15. {
  16.     /**
  17.      * @var int
  18.      */
  19.     #[ORM\Column(name'got_id'type'integer'nullablefalseoptions: ['unsigned' => true])]
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue(strategy'IDENTITY')]
  22.     private $id;
  23.     /**
  24.      * @var Member
  25.      */
  26.     #[ORM\ManyToOne(targetEntityMember::class)]
  27.     #[ORM\JoinColumn(name'got_m_id'referencedColumnName'm_id'nullabletrue)]
  28.     private $member;
  29.     /**
  30.      * @var Collection|User[]
  31.      */
  32.     #[ORM\ManyToMany(targetEntityUser::class, inversedBy'googleTokens')]
  33.     #[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')])]
  34.     private $users;
  35.     /**
  36.      * @deprecated Kept for Doctrine mapping purposes, use $users collection instead
  37.      *
  38.      * @var User|null
  39.      */
  40.     #[ORM\Column(name'got_u_id'type'integer'nullabletrueoptions: ['unsigned' => true])]
  41.     private $user;
  42.     /**
  43.      * @var string
  44.      */
  45.     #[ORM\Column(name'got_account_id'type'string'length32nullablefalse)]
  46.     private $accountId;
  47.     /**
  48.      * @var string
  49.      */
  50.     #[ORM\Column(name'got_token'type'text'length16777215nullablefalse)]
  51.     private $token;
  52.     /**
  53.      * @var string|null
  54.      */
  55.     #[ORM\Column(name'got_refresh_token'type'text'length16777215nullabletrue)]
  56.     private $refreshToken;
  57.     /**
  58.      * @var string|null
  59.      */
  60.     #[ORM\Column(name'got_token_owner_display_name'type'string'length255nullabletrue)]
  61.     private $tokenOwnerDisplayName;
  62.     /**
  63.      * @var string|null
  64.      */
  65.     #[ORM\Column(name'got_token_owner_email'type'binary_aes_encrypted'precision0scale0nullabletrueuniquefalse)]
  66.     private $tokenOwnerEmail;
  67.     /**
  68.      * @var \DateTime|null
  69.      */
  70.     #[ORM\Column(name'got_token_expiry'type'datetime'nullabletrue)]
  71.     private $tokenExpiry;
  72.     /**
  73.      * @var \DateTime|null
  74.      */
  75.     #[ORM\Column(name'got_token_last_checked'type'datetime'nullabletrue)]
  76.     private $tokenLastChecked;
  77.     /**
  78.      * @var bool|null
  79.      */
  80.     #[ORM\Column(name'got_error'type'boolean'nullabletrue)]
  81.     private $error false;
  82.     /**
  83.      * @var bool|null
  84.      */
  85.     #[ORM\Column(name'got_deleted'type'boolean'nullabletrue)]
  86.     private $deleted false;
  87.     /**
  88.      * @var bool|null
  89.      */
  90.     #[ORM\Column(name'got_login_enabled'type'boolean'nullabletrueoptions: ['default' => '1'])]
  91.     private $loginEnabled true;
  92.     #[ORM\OneToMany(targetEntityGoogle::class, mappedBy'googleToken')]
  93.     private $googleEntities;
  94.     #[ORM\OneToMany(targetEntityYouTube::class, mappedBy'googleToken')]
  95.     private $youtubeEntities;
  96.     public function __construct(?Member $memberstring $tokenstring $refreshTokenstring $tokenOwnerDisplayName, ?string $tokenOwnerEmail null, ?string $accountId null, ?User $user null)
  97.     {
  98.         $this->member $member;
  99.         $this->accountId $accountId;
  100.         $this->token $token;
  101.         $this->refreshToken $refreshToken;
  102.         $this->tokenOwnerDisplayName $tokenOwnerDisplayName;
  103.         $this->tokenOwnerEmail $tokenOwnerEmail;
  104.         $this->users = new ArrayCollection();
  105.         if ($user !== null)
  106.         {
  107.             $this->addUser($user);
  108.         }
  109.         $this->googleEntities = new ArrayCollection();
  110.         $this->youtubeEntities = new ArrayCollection();
  111.     }
  112.     public function getId(): ?int
  113.     {
  114.         return $this->id;
  115.     }
  116.     public function getMember(): ?Member
  117.     {
  118.         return $this->member;
  119.     }
  120.     // If a member has already been set, do not disassociate said member from the Google Token.
  121.     public function setMember(Member $member): self
  122.     {
  123.         $this->member $member;
  124.         return $this;
  125.     }
  126.     /**
  127.      * @return Collection|User[]
  128.      */
  129.     public function getUsers(): Collection
  130.     {
  131.         return $this->users;
  132.     }
  133.     public function addUser(User $user): self
  134.     {
  135.         if (!$this->users->contains($user))
  136.         {
  137.             $this->users->add($user);
  138.         }
  139.         return $this;
  140.     }
  141.     public function removeUser(User $user): self
  142.     {
  143.         $this->users->removeElement($user);
  144.         return $this;
  145.     }
  146.     public function getAccountId(): string
  147.     {
  148.         return $this->accountId;
  149.     }
  150.     public function setAccountId(string $accountId): self
  151.     {
  152.         $this->accountId $accountId;
  153.         return $this;
  154.     }
  155.     public function getToken(): string
  156.     {
  157.         return $this->token;
  158.     }
  159.     public function setToken(string $token): self
  160.     {
  161.         $this->token $token;
  162.         return $this;
  163.     }
  164.     public function getRefreshToken(): string
  165.     {
  166.         return $this->refreshToken;
  167.     }
  168.     public function setRefreshToken(string $refreshToken): self
  169.     {
  170.         $this->refreshToken $refreshToken;
  171.         return $this;
  172.     }
  173.     public function getTokenOwnerDisplayName(): ?string
  174.     {
  175.         return $this->tokenOwnerDisplayName;
  176.     }
  177.     public function setTokenOwnerDisplayName(?string $tokenOwnerDisplayName): self
  178.     {
  179.         $this->tokenOwnerDisplayName $tokenOwnerDisplayName;
  180.         return $this;
  181.     }
  182.     public function getTokenOwnerEmail(): ?string
  183.     {
  184.         return $this->tokenOwnerEmail;
  185.     }
  186.     public function setTokenOwnerEmail(?string $tokenOwnerEmail): self
  187.     {
  188.         $this->tokenOwnerEmail $tokenOwnerEmail;
  189.         return $this;
  190.     }
  191.     public function getTokenExpiry(): ?\DateTimeInterface
  192.     {
  193.         return $this->tokenExpiry;
  194.     }
  195.     public function setTokenExpiry(?\DateTimeInterface $tokenExpiry): self
  196.     {
  197.         $this->tokenExpiry $tokenExpiry;
  198.         return $this;
  199.     }
  200.     public function getTokenLastChecked(): ?\DateTimeInterface
  201.     {
  202.         return $this->tokenLastChecked;
  203.     }
  204.     public function setTokenLastChecked(?\DateTimeInterface $tokenLastChecked): self
  205.     {
  206.         $this->tokenLastChecked $tokenLastChecked;
  207.         return $this;
  208.     }
  209.     public function hasError(): bool
  210.     {
  211.         return $this->error;
  212.     }
  213.     public function setError(bool $error): self
  214.     {
  215.         $this->error $error;
  216.         return $this;
  217.     }
  218.     public function isDeleted(): bool
  219.     {
  220.         return $this->deleted;
  221.     }
  222.     public function setDeleted(bool $deleted): self
  223.     {
  224.         $this->deleted $deleted;
  225.         return $this;
  226.     }
  227.     public function isLoginEnabled(): bool
  228.     {
  229.         return $this->loginEnabled;
  230.     }
  231.     public function setLoginEnabled(bool $loginEnabled): self
  232.     {
  233.         $this->loginEnabled $loginEnabled;
  234.         return $this;
  235.     }
  236.     /**
  237.      * Get all connected Google social profiles for this google account/user token.
  238.      *
  239.      * @param bool $onlyActive
  240.      *   If it should filter out all deleted profiles.
  241.      *
  242.      * @return \Doctrine\Common\Collections\Collection<GoogleSocialProfile>
  243.      */
  244.     public function getGoogleSocialProfiles(bool $onlyActive false): Collection
  245.     {
  246.         return $this->googleEntities
  247.             ->filter(fn (Google $googleEntity): bool => !$onlyActive || !$googleEntity->isDeleted())
  248.             ->map(fn (Google $googleEntity): GoogleSocialProfile => $googleEntity->getSocialProfile());
  249.     }
  250.     /**
  251.      * Get all connected YouTube social profiles for this google account/user token.
  252.      *
  253.      * @param bool $onlyActive
  254.      *   If it should filter out all deleted profiles.
  255.      *
  256.      * @return \Doctrine\Common\Collections\Collection<YouTubeSocialProfile>
  257.      */
  258.     public function getYouTubeSocialProfiles(bool $onlyActive false): Collection
  259.     {
  260.         return $this->youtubeEntities
  261.             ->filter(fn (YouTube $youtubeEntity): bool => !$onlyActive || !$youtubeEntity->isDeleted())
  262.             ->map(fn (YouTube $youtubeEntity): YouTubeSocialProfile => $youtubeEntity->getSocialProfile());
  263.     }
  264.     public function addGoogleEntity(Google $accountEntity): self
  265.     {
  266.         if (!$this->googleEntities->contains($accountEntity))
  267.         {
  268.             $this->googleEntities->add($accountEntity);
  269.         }
  270.         return $this;
  271.     }
  272.     public function removeGoogleEntity(Google $accountEntity): self
  273.     {
  274.         $this->googleEntities->removeElement($accountEntity);
  275.         return $this;
  276.     }
  277.     public function addYouTubeEntity(YouTube $accountEntity): self
  278.     {
  279.         if (!$this->youtubeEntities->contains($accountEntity))
  280.         {
  281.             $this->youtubeEntities->add($accountEntity);
  282.         }
  283.         return $this;
  284.     }
  285.     public function removeYouTubeEntity(YouTube $accountEntity): self
  286.     {
  287.         $this->youtubeEntities->removeElement($accountEntity);
  288.         return $this;
  289.     }
  290.     /**
  291.      * Is the Google token expired? If so, we will need to request a new one using the refresh token.
  292.      *
  293.      * @return bool
  294.      */
  295.     public function isTokenExpired(): bool
  296.     {
  297.         return $this->tokenExpiry server_datetime();
  298.     }
  299. }