good routes, good controllers
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
704f1272f7
commit
36051ebd83
Before Width: | Height: | Size: 507 B After Width: | Height: | Size: 507 B |
Before Width: | Height: | Size: 732 B After Width: | Height: | Size: 732 B |
@ -1,13 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Sub;
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Http\HttpRequest;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Http\ViewHttpResponse;
|
||||
use App\Model\AuthModel;
|
||||
use App\Session\MutableSessionHandle;
|
||||
use App\Validation\FieldValidationFail;
|
||||
use App\Validation\ValidationFail;
|
||||
use App\Validation\Validators;
|
||||
|
@ -1,18 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Sub;
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Connexion;
|
||||
use App\Controller\VisitorController;
|
||||
use App\Data\TacticInfo;
|
||||
use App\Gateway\TacticInfoGateway;
|
||||
use App\Http\HttpCodes;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Http\JsonHttpResponse;
|
||||
use App\Http\ViewHttpResponse;
|
||||
use App\Model\TacticModel;
|
||||
use App\Session\SessionHandle;
|
||||
use App\Validation\ValidationFail;
|
||||
use App\Validator\TacticValidator;
|
||||
|
||||
class EditorController {
|
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Route;
|
||||
|
||||
use App\Http\HttpResponse;
|
||||
use App\Session\SessionHandle;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* A Front controller action
|
||||
*/
|
||||
class Action {
|
||||
/**
|
||||
* @var callable(mixed[]): HttpResponse $action action to call
|
||||
*/
|
||||
private $action;
|
||||
|
||||
private bool $isAuthRequired;
|
||||
|
||||
/**
|
||||
* @param callable(mixed[]): HttpResponse $action
|
||||
*/
|
||||
private function __construct(callable $action, bool $isAuthRequired) {
|
||||
$this->action = $action;
|
||||
$this->isAuthRequired = $isAuthRequired;
|
||||
}
|
||||
|
||||
public function isAuthRequired(): bool {
|
||||
return $this->isAuthRequired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $params
|
||||
* @param SessionHandle $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, SessionHandle $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 Action an action that does not require to have an authorization.
|
||||
*/
|
||||
public static function noAuth(callable $action): Action {
|
||||
return new Action($action, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(mixed[]): HttpResponse $action
|
||||
* @return Action an action that does require to have an authorization.
|
||||
*/
|
||||
public static function auth(callable $action): Action {
|
||||
return new Action($action, true);
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Connexion;
|
||||
use App\Gateway\AccountGateway;
|
||||
use App\Http\HttpResponse;
|
||||
use App\Model\AuthModel;
|
||||
use App\Session\MutableSessionHandle;
|
||||
|
||||
class VisitorController {
|
||||
final public function register(MutableSessionHandle $session): HttpResponse {
|
||||
$model = new AuthModel(new AccountGateway(new Connexion(get_database())));
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
return (new Sub\AuthController($model))->displayRegister();
|
||||
}
|
||||
return (new Sub\AuthController($model))->confirmRegister($_POST, $session);
|
||||
}
|
||||
|
||||
final public function login(MutableSessionHandle $session): HttpResponse {
|
||||
$model = new AuthModel(new AccountGateway(new Connexion(get_database())));
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
return (new Sub\AuthController($model))->displayLogin();
|
||||
}
|
||||
return (new Sub\AuthController($model))->confirmLogin($_POST, $session);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue