You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Application-Web/src/App/AppAction.php

56 lines
1.7 KiB

<?php
namespace IQBall\App;
use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Session\MutableSessionHandle;
use Exception;
use IQBall\Core\Route\AbstractAction;
/**
* A Front controller action
* @extends AbstractAction<MutableSessionHandle>
*/
class AppAction extends AbstractAction {
/**
* @param mixed[] $params
* @param MutableSessionHandle $session
* @return HttpResponse
* @throws Exception <p>
* thrown if this action is required to be authenticated, but the given session does not contain a logged-in account.
* </p>
* <p>
* Caller is supposed to ensure that the user is logged-in before, if `$this->isAuthRequired()` is true before
* running this action.
* </p>
*/
public function run(array $params, $session): HttpResponse {
$params = array_values($params);
if ($this->isAuthRequired()) {
if ($session->getAccount() == null) {
throw new Exception("action requires authorization.");
}
}
$params[] = $session;
return call_user_func_array($this->action, $params);
}
/**
* @param callable(mixed[]): HttpResponse $action
* @return AppAction an action that does not require to have an authorization.
*/
public static function noAuth(callable $action): AppAction {
return new AppAction($action, false);
}
/**
* @param callable(mixed[]): HttpResponse $action
* @return AppAction an action that does require to have an authorization.
*/
public static function auth(callable $action): AppAction {
return new AppAction($action, true);
}
}