<?php
namespace App\Entity\OAuth2;
use App\Entity\Content\Common\DiscriminatorInterface;
use App\Entity\Content\Common\DiscriminatorTrait;
use App\Entity\OAuth2\Admin\AbstractAdminToken;
use App\Entity\OAuth2\Admin\AdminAccessToken;
use App\Entity\OAuth2\Admin\AdminRefreshToken;
use App\Entity\OAuth2\App\AbstractAppToken;
use App\Entity\OAuth2\App\AppAccessToken;
use App\Entity\OAuth2\App\AppRefreshToken;
use App\Entity\Shared\UlidIdentifiableInterface;
use App\Entity\Shared\UlidIdentifiableTrait;
use App\Service\OAuth2\AppScopeRepository;
use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableInterface;
use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableTrait;
use Cms\TenantBundle\Model\TenantableInterface;
use Cms\TenantBundle\Model\TenantableTrait;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
/**
* @ORM\Entity(
* repositoryClass = "App\Doctrine\Repository\OAuth2\TokenRepository",
* )
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(
* name = DiscriminatorInterface::COLUMN__NAME,
* type = DiscriminatorInterface::COLUMN__TYPE,
* length = 64,
* )
* @ORM\DiscriminatorMap(AbstractToken::DISCRS)
* @ORM\Table(
* name = "sys__oauth2__token",
* indexes = {
* @ORM\Index(
* name = "idx__discr",
* columns = {
* "discr",
* },
* ),
* },
* )
*/
abstract class AbstractToken
implements
TenantableInterface,
UlidIdentifiableInterface,
TimestampableInterface,
DiscriminatorInterface
{
public const DISCRS = [
//AbstractAdminToken::DISCR => AbstractAdminToken::class,
AdminAccessToken::DISCR => AdminAccessToken::class,
AdminRefreshToken::DISCR => AdminRefreshToken::class,
//AbstractAppToken::DISCR => AbstractAppToken::class,
AppAccessToken::DISCR => AppAccessToken::class,
AppRefreshToken::DISCR => AppRefreshToken::class,
];
public const DISCR = null;
use TenantableTrait;
use UlidIdentifiableTrait;
use TimestampableTrait;
use DiscriminatorTrait;
/**
* @var AbstractToken|null
*
* @ORM\ManyToOne(
* targetEntity = AbstractToken::class,
* )
* @ORM\JoinColumn(
* name = "parent",
* referencedColumnName = "id",
* nullable = true,
* onDelete = "CASCADE",
* )
*/
protected ?AbstractToken $parent = null;
/**
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $identifier = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $client = null;
/**
* @var array|string[]
*
* @ORM\Column(
* type = "simple_array",
* nullable = true,
* )
*/
protected ?array $scopes = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected ?string $userIdentifier = null;
/**
* @var DateTimeImmutable|null
*
* @ORM\Column(
* type = "datetime_immutable",
* nullable = false,
* )
*/
protected ?DateTimeImmutable $expiryDateTime = null;
/**
* {@inheritdoc}
*/
public function getIdentifier(): string
{
return $this->identifier;
}
/**
* {@inheritdoc}
*/
public function setIdentifier($identifier): void
{
$this->identifier = $identifier;
}
/**
* {@inheritdoc}
*/
public function setAccessToken(AccessTokenEntityInterface $accessToken): void
{
if ( ! $accessToken instanceof self) {
throw new \LogicException();
}
$this->parent = $accessToken;
}
/**
* {@inheritdoc}
*/
public function getAccessToken(): ?AccessTokenEntityInterface
{
if ($this->parent && ! $this->parent instanceof AccessTokenEntityInterface) {
throw new \LogicException();
}
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function getScopes(): array
{
return array_map(
static function (string $identifier) {
return AppScopeRepository::getInstance()->getScopeEntityByIdentifier($identifier);
},
$this->scopes,
);
}
/**
* {@inheritdoc}
*/
public function addScope(ScopeEntityInterface $scope): void
{
if ( ! is_array($this->scopes)) {
$this->scopes = [];
}
if ( ! in_array($scope, $this->scopes, true)) {
$this->scopes[] = $scope->getIdentifier();
}
}
/**
* {@inheritdoc}
*/
public function getExpiryDateTime(): DateTimeImmutable
{
return $this->expiryDateTime;
}
/**
* {@inheritdoc}
*/
public function setExpiryDateTime(DateTimeImmutable $dateTime): void
{
$this->expiryDateTime = $dateTime;
}
/**
* {@inheritdoc}
*/
public function getUserIdentifier(): ?string
{
return $this->userIdentifier;
}
/**
* {@inheritdoc}
*/
public function setUserIdentifier($identifier): void
{
$this->userIdentifier = $identifier;
}
/**
* {@inheritdoc}
*/
public function setClient(ClientEntityInterface $client): void
{
$this->client = $client->getIdentifier();
}
}