openvk/VKAPI/Handlers/VKAPIRequestHandler.php

43 lines
990 B
PHP
Raw Normal View History

2020-08-12 14:36:18 +03:00
<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\VKAPI\Exceptions\APIErrorException;
use openvk\Web\Models\Entities\User;
abstract class VKAPIRequestHandler
{
protected $user;
2022-12-17 02:03:02 +03:00
protected $platform;
2020-08-12 14:36:18 +03:00
2022-12-17 02:03:02 +03:00
function __construct(?User $user = NULL, ?string $platform = NULL)
2020-08-12 14:36:18 +03:00
{
2022-12-17 02:03:02 +03:00
$this->user = $user;
$this->platform = $platform;
2020-08-12 14:36:18 +03:00
}
protected function fail(int $code, string $message): void
{
throw new APIErrorException($message, $code);
}
protected function getUser(): ?User
{
return $this->user;
}
2022-12-17 02:03:02 +03:00
protected function getPlatform(): ?string
{
return $this->platform;
}
2020-08-12 14:36:18 +03:00
protected function userAuthorized(): bool
{
return !is_null($this->getUser());
}
protected function requireUser(): void
{
if(!$this->userAuthorized())
$this->fail(5, "User authorization failed: no access_token passed.");
}
}