<?php
namespace App\Entity;
use App\Services\Instagram\Dto\InstagramAuthDto;
use DateTimeImmutable;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\PrePersist;
use Doctrine\ORM\Mapping\PreUpdate;
#[Entity]
#[HasLifecycleCallbacks]
class InstagramAuth
{
/**
* Connected Instagram account for Facebook user.
* Unique ID of the Instagram business account
*/
#[Id]
#[Column(type: 'bigint', unique: true)]
private int $id;
/** Instagram ID */
#[Column(type: 'bigint', unique: true)]
private int $igId;
#[Column(type: 'string')]
private string $accessToken;
#[Column(
type: 'datetime_immutable',
nullable: false,
columnDefinition: 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL',
)]
private DateTimeImmutable $refreshedAt;
#[Column(
type: 'datetime_immutable',
nullable: true,
columnDefinition: 'TIMESTAMP NULL',
)]
private DateTimeImmutable $expiresAt;
#[Column('created_at', 'datetime_immutable')]
private ?DateTimeImmutable $createdAt = null;
#[PrePersist]
public function prePersist(): void
{
$this->createdAt = new DateTimeImmutable();
$this->refreshedAt = new DateTimeImmutable();
}
#[PreUpdate]
public function preUpdate(): void
{
$this->refreshedAt = new DateTimeImmutable();
}
public function getId(): int
{
return $this->id;
}
public function setId(int $id): static
{
$this->id = $id;
return $this;
}
public function getIgId(): int
{
return $this->igId;
}
public function setIgId(int $igId): static
{
$this->igId = $igId;
return $this;
}
public function getAccessToken(): string
{
return $this->accessToken;
}
public function setAccessToken(string $accessToken): static
{
$this->accessToken = $accessToken;
return $this;
}
public function getRefreshedAt(): DateTimeImmutable
{
return $this->refreshedAt;
}
public function setRefreshedAt(DateTimeImmutable $refreshedAt): static
{
$this->refreshedAt = $refreshedAt;
return $this;
}
public function setRefreshedAtNow(): static
{
$this->refreshedAt = new DateTimeImmutable;
return $this;
}
public function getExpiresAt(): DateTimeImmutable
{
return $this->expiresAt;
}
public function setExpiresAt(DateTimeImmutable $expiresAt): static
{
$this->expiresAt = $expiresAt;
return $this;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
}
public function toDto(): InstagramAuthDto
{
return new InstagramAuthDto(
id: $this->getId(),
igId: $this->getIgId(),
accessToken: $this->getAccessToken(),
refreshedAt: $this->getRefreshedAt(),
expiersAt: $this->getExpiresAt(),
createdAt: $this->getCreatedAt()
);
}
}