mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
type hint all the things
This commit is contained in:
parent
28589d505d
commit
3f9e613e77
34 changed files with 287 additions and 309 deletions
|
|
@ -13,20 +13,10 @@ use flight\net\Response;
|
|||
use SteamId;
|
||||
|
||||
class AuthController extends BaseController {
|
||||
/**
|
||||
* @var UserProvider
|
||||
*/
|
||||
private $userProvider;
|
||||
|
||||
/**
|
||||
* @var AuthProvider
|
||||
*/
|
||||
private $authProvider;
|
||||
|
||||
/** @var string */
|
||||
private $host;
|
||||
|
||||
private $apiRoot;
|
||||
private UserProvider $userProvider;
|
||||
private AuthProvider $authProvider;
|
||||
private string $host;
|
||||
private string $apiRoot;
|
||||
|
||||
public function __construct(
|
||||
Request $request,
|
||||
|
|
@ -43,11 +33,11 @@ class AuthController extends BaseController {
|
|||
$this->apiRoot = $apiRoot;
|
||||
}
|
||||
|
||||
public function token() {
|
||||
public function token(): void {
|
||||
echo $this->authProvider->generateToken();
|
||||
}
|
||||
|
||||
public function get($token) {
|
||||
public function get(string $token): void {
|
||||
$userData = $this->authProvider->getUser($token);
|
||||
Flight::json([
|
||||
'token' => $token,
|
||||
|
|
@ -57,14 +47,14 @@ class AuthController extends BaseController {
|
|||
]);
|
||||
}
|
||||
|
||||
public function login($token) {
|
||||
public function login(string $token): void {
|
||||
$_SESSION['return'] = $this->query('return', 'https://' . $this->host);
|
||||
$steam = new SteamLogin();
|
||||
$url = $steam->url($this->apiRoot . '/auth/handle/' . urlencode($token), $this->apiRoot);
|
||||
Flight::redirect(str_replace('&', '&', $url)); // headers make no sense
|
||||
}
|
||||
|
||||
public function logout($token) {
|
||||
public function logout(string $token): void {
|
||||
$this->authProvider->logout($token);
|
||||
Flight::json([
|
||||
'token' => $token,
|
||||
|
|
@ -74,7 +64,7 @@ class AuthController extends BaseController {
|
|||
]);
|
||||
}
|
||||
|
||||
public function handle($token) {
|
||||
public function handle(string $token): void {
|
||||
$return = $_SESSION['return'] ?? 'https://' . $this->host;
|
||||
unset($_SESSION['return']);
|
||||
$steam = new SteamLogin();
|
||||
|
|
|
|||
|
|
@ -8,32 +8,48 @@ use flight\net\Request;
|
|||
use flight\net\Response;
|
||||
|
||||
class BaseController {
|
||||
private $request;
|
||||
private $response;
|
||||
private Request $request;
|
||||
private Response $response;
|
||||
|
||||
public function __construct(Request $request, Response $response) {
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
protected function query($name, $default) {
|
||||
/**
|
||||
* @return string[]|string
|
||||
*/
|
||||
protected function query(string $name, string $default) {
|
||||
return $this->request->query[$name] ?? $default;
|
||||
}
|
||||
|
||||
protected function file($name) {
|
||||
/**
|
||||
* @return string[]|null
|
||||
*/
|
||||
protected function file(string $name): ?array {
|
||||
return $this->request->files[$name];
|
||||
}
|
||||
|
||||
protected function post($name, $default = null) {
|
||||
protected function post(string $name, string $default): string {
|
||||
return $this->request->data[$name] ?? $default;
|
||||
}
|
||||
|
||||
protected function json($data, $code = 200, $encode = true, $charset = 'utf-8', $option = 0) {
|
||||
/**
|
||||
* @param mixed $data
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function json(
|
||||
$data,
|
||||
int $code = 200,
|
||||
bool $encode = true,
|
||||
string $charset = 'utf-8',
|
||||
int $option = 0
|
||||
): void {
|
||||
$json = ($encode) ? json_encode($data, $option) : $data;
|
||||
|
||||
$this->response
|
||||
->status($code)
|
||||
->header('Content-Type', 'application/json; charset=' . $charset)
|
||||
$this->response->status($code);
|
||||
$this->response->header('Content-Type', 'application/json; charset=' . $charset)
|
||||
->write($json)
|
||||
->send();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,17 +14,11 @@ use flight\net\Request;
|
|||
use flight\net\Response;
|
||||
|
||||
class DemoController extends BaseController {
|
||||
/** @var DemoProvider */
|
||||
private $demoProvider;
|
||||
|
||||
/** @var ChatProvider */
|
||||
private $chatProvider;
|
||||
|
||||
private $demoListProvider;
|
||||
|
||||
private $editKey;
|
||||
|
||||
private $store;
|
||||
private DemoProvider $demoProvider;
|
||||
private ChatProvider $chatProvider;
|
||||
private DemoListProvider $demoListProvider;
|
||||
private string $editKey;
|
||||
private DemoStore $store;
|
||||
|
||||
public function __construct(
|
||||
Request $request,
|
||||
|
|
@ -43,14 +37,14 @@ class DemoController extends BaseController {
|
|||
$this->editKey = $editKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*/
|
||||
public function get($id) {
|
||||
public function get(string $id): void {
|
||||
$this->json($this->demoProvider->get(\intval($id, 10)));
|
||||
}
|
||||
|
||||
protected function getFilter() {
|
||||
/**
|
||||
* @return array<mixed>
|
||||
*/
|
||||
protected function getFilter(): array {
|
||||
$map = $this->query('map', '');
|
||||
$players = $this->query('players', '');
|
||||
$type = $this->query('type', '');
|
||||
|
|
@ -110,29 +104,29 @@ class DemoController extends BaseController {
|
|||
return $filter;
|
||||
}
|
||||
|
||||
public function listDemos() {
|
||||
$page = $this->query('page', 1);
|
||||
public function listDemos(): void {
|
||||
$page = (int) $this->query('page', '1');
|
||||
$order = 'ASC' === $this->query('order', 'DESC') ? 'ASC' : 'DESC';
|
||||
$this->json($this->demoListProvider->listDemos((int) $page, $this->getFilter(), $order));
|
||||
}
|
||||
|
||||
public function listProfile($steamId) {
|
||||
$page = $this->query('page', 1);
|
||||
public function listProfile(string $steamId): void {
|
||||
$page = (int) $this->query('page', '1');
|
||||
$where = $this->getFilter();
|
||||
$where['players'][] = $steamId;
|
||||
$this->json($this->demoListProvider->listProfile((int) $page, $where));
|
||||
}
|
||||
|
||||
public function listUploads($steamId) {
|
||||
$page = $this->query('page', 1);
|
||||
public function listUploads(string $steamId): void {
|
||||
$page = (int) $this->query('page', '1');
|
||||
$this->json($this->demoListProvider->listUploads($steamId, (int) $page, $this->getFilter()));
|
||||
}
|
||||
|
||||
public function chat($demoId) {
|
||||
public function chat(string $demoId): void {
|
||||
$this->json($this->chatProvider->getChat((int) $demoId));
|
||||
}
|
||||
|
||||
public function setDemoUrl($id) {
|
||||
public function setDemoUrl(string $id): void {
|
||||
$hash = (string) $this->post('hash', '');
|
||||
$backend = (string) $this->post('backend', '');
|
||||
$path = (string) $this->post('path', '');
|
||||
|
|
|
|||
|
|
@ -10,19 +10,18 @@ use flight\net\Request;
|
|||
use flight\net\Response;
|
||||
|
||||
class InfoController extends BaseController {
|
||||
/** @var InfoProvider */
|
||||
private $infoProvider;
|
||||
private InfoProvider $infoProvider;
|
||||
|
||||
public function __construct(Request $request, Response $response, InfoProvider $infoProvider) {
|
||||
parent::__construct($request, $response);
|
||||
$this->infoProvider = $infoProvider;
|
||||
}
|
||||
|
||||
public function listMaps() {
|
||||
public function listMaps(): void {
|
||||
Flight::json($this->infoProvider->listMaps());
|
||||
}
|
||||
|
||||
public function stats() {
|
||||
public function stats(): void {
|
||||
Flight::json($this->infoProvider->getStats());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,22 +4,19 @@ declare(strict_types=1);
|
|||
|
||||
namespace Demostf\API\Controllers;
|
||||
|
||||
use Demostf\API\Error\InvalidKeyException;
|
||||
use Demostf\API\Providers\UploadProvider;
|
||||
use Exception;
|
||||
use Flight;
|
||||
use flight\net\Request;
|
||||
use flight\net\Response;
|
||||
|
||||
class UploadController extends BaseController {
|
||||
private $uploadProvider;
|
||||
private UploadProvider $uploadProvider;
|
||||
|
||||
public function __construct(Request $request, Response $response, UploadProvider $uploadProvider) {
|
||||
parent::__construct($request, $response);
|
||||
$this->uploadProvider = $uploadProvider;
|
||||
}
|
||||
|
||||
public function upload() {
|
||||
public function upload(): void {
|
||||
$key = (string) $this->post('key', '');
|
||||
$red = (string) $this->post('red', 'RED');
|
||||
$blu = (string) $this->post('blu', 'BLU');
|
||||
|
|
|
|||
|
|
@ -12,17 +12,14 @@ use flight\net\Response;
|
|||
use InvalidArgumentException;
|
||||
|
||||
class UserController extends BaseController {
|
||||
/**
|
||||
* @var UserProvider
|
||||
*/
|
||||
private $userProvider;
|
||||
private UserProvider $userProvider;
|
||||
|
||||
public function __construct(Request $request, Response $response, UserProvider $userProvider) {
|
||||
parent::__construct($request, $response);
|
||||
$this->userProvider = $userProvider;
|
||||
}
|
||||
|
||||
public function get($steamId) {
|
||||
public function get(string $steamId): void {
|
||||
if (!is_numeric($steamId)) {
|
||||
try {
|
||||
$steamId = Parser::convertSteamIdToCommunityId($steamId);
|
||||
|
|
@ -36,7 +33,7 @@ class UserController extends BaseController {
|
|||
Flight::json($this->userProvider->get($steamId));
|
||||
}
|
||||
|
||||
public function search() {
|
||||
public function search(): void {
|
||||
$query = $this->query('query', '');
|
||||
Flight::json($this->userProvider->search($query));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue