openvk/VKAPI/Handlers/VKAPIRequestHandler.php
Alexander Minkin def76226b7
feat(core): add phpstan for static analysis (#1223)
* feat: add phpstan for static analysis

* ci(actions): add phpstan action

* ci(actions): do analysing inside docker container

* fix(FetchToncoinTransactions): add var declaration

* fix(ServiceAPI/Wall): add var declaration

* fix(bootstrap): remove case-insensitive false vars

* fix(VKAPI/Handlers/Board): change parameters order

* fix(VKAPIRequestHandler): set fail's return type as never

* fix(VKAPI/Handlers/Groups): add array declaration

* fix(VKAPI/Handlers/Newsfeed): add return_banned declaration

* fix(VKAPI/Handlers/Notes): move $nodez declaration up

* fix(phpstan): most of the things and stupid lines of code

* fix(lint)

* fix(phpstan): less errors

* fix(lint): again. cuz i forgot about it

* fix(stan): all errors are gone now =3

---------

Co-authored-by: veselcraft <veselcraft@icloud.com>
2025-03-09 16:03:33 +03:00

64 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\VKAPI\Exceptions\APIErrorException;
use openvk\Web\Models\Entities\IP;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\IPs;
abstract class VKAPIRequestHandler
{
protected $user;
protected $platform;
public function __construct(?User $user = null, ?string $platform = null)
{
$this->user = $user;
$this->platform = $platform;
}
protected function fail(int $code, string $message): never
{
throw new APIErrorException($message, $code);
}
protected function getUser(): ?User
{
return $this->user;
}
protected function getPlatform(): ?string
{
return $this->platform ?? "";
}
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.");
}
}
protected function willExecuteWriteAction(): void
{
$ip = (new IPs())->get(CONNECTING_IP);
$res = $ip->rateLimit();
if (!($res === IP::RL_RESET || $res === IP::RL_CANEXEC)) {
if ($res === IP::RL_BANNED && OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]["autoban"]) {
$this->user->ban("User account has been suspended for breaking API terms of service", false);
$this->fail(18, "User account has been suspended due to repeated violation of API rate limits.");
}
$this->fail(29, "You have been rate limited.");
}
}
}