openvk/Web/Models/Entities/APIToken.php

64 lines
1.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-08-12 14:36:18 +03:00
namespace openvk\Web\Models\Entities;
2020-08-12 14:36:18 +03:00
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Repositories\Users;
use Nette\InvalidStateException as ISE;
class APIToken extends RowModel
{
protected $tableName = "api_tokens";
public function getUser(): User
2020-08-12 14:36:18 +03:00
{
return (new Users())->get($this->getRecord()->user);
2020-08-12 14:36:18 +03:00
}
public function getSecret(): string
2020-08-12 14:36:18 +03:00
{
return $this->getRecord()->secret;
}
public function getFormattedToken(): string
2020-08-12 14:36:18 +03:00
{
return $this->getId() . "-" . chunk_split($this->getSecret(), 8, "-") . "jill";
}
2022-12-17 02:03:02 +03:00
public function getPlatform(): ?string
2022-12-17 02:03:02 +03:00
{
return $this->getRecord()->platform;
}
public function isRevoked(): bool
2020-08-12 14:36:18 +03:00
{
return $this->isDeleted();
}
public function setUser(User $user): void
2020-08-12 14:36:18 +03:00
{
$this->stateChanges("user", $user->getId());
}
public function setSecret(string $secret): void
2020-08-12 14:36:18 +03:00
{
throw new ISE("Setting secret manually is prohbited");
}
public function revoke(): void
2020-08-12 14:36:18 +03:00
{
$this->delete();
}
public function save(?bool $log = false): void
2020-08-12 14:36:18 +03:00
{
if (is_null($this->getRecord())) {
2020-08-12 14:36:18 +03:00
$this->stateChanges("secret", bin2hex(openssl_random_pseudo_bytes(36)));
}
2020-08-12 14:36:18 +03:00
parent::save();
}
}