From 23127dd193d3b1456199ec555d7349b411068c96 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Sun, 12 Sep 2021 22:16:06 +0200 Subject: [PATCH] Refactoring --- src/Common/ActionResult.php | 14 +++++++++ src/Frontend/Accounts/AccountController.php | 32 ++++----------------- src/Frontend/Accounts/AccountHandler.php | 30 +++++++++++++++++++ templates/account_edit.html.twig | 18 ++++++------ 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/Common/ActionResult.php b/src/Common/ActionResult.php index 924c489..d9adf77 100644 --- a/src/Common/ActionResult.php +++ b/src/Common/ActionResult.php @@ -77,4 +77,18 @@ class ActionResult { return $this->inputData; } + + /** + * Returns an array that can be merged with other template render data, containing either a field "success" or "error" with the + * result message and in case of an error result a field "formData" containing the input data. + */ + public function getRenderData(): array + { + $messageKey = $this->isSuccess() ? 'success' : 'error'; + $renderData = [$messageKey => $this->message]; + if (!empty($this->inputData)) { + $renderData['formData'] = $this->inputData; + } + return $renderData; + } } diff --git a/src/Frontend/Accounts/AccountController.php b/src/Frontend/Accounts/AccountController.php index 0617e56..79a8bf4 100644 --- a/src/Frontend/Accounts/AccountController.php +++ b/src/Frontend/Accounts/AccountController.php @@ -8,8 +8,6 @@ use MailAccountAdmin\Common\SessionHelper; use MailAccountAdmin\Common\UserHelper; use MailAccountAdmin\Exceptions\InputValidationError; use MailAccountAdmin\Frontend\BaseController; -use MailAccountAdmin\Repositories\AccountRepository; -use MailAccountAdmin\Repositories\AliasRepository; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Views\Twig; @@ -17,15 +15,11 @@ use Slim\Views\Twig; class AccountController extends BaseController { private AccountHandler $accountHandler; - private AccountRepository $accountRepository; - private AliasRepository $aliasRepository; - public function __construct(Twig $view, SessionHelper $sessionHelper, UserHelper $userHelper, AccountHandler $accountHandler, AccountRepository $accountRepository, AliasRepository $aliasRepository) + public function __construct(Twig $view, SessionHelper $sessionHelper, UserHelper $userHelper, AccountHandler $accountHandler) { parent::__construct($view, $sessionHelper, $userHelper); $this->accountHandler = $accountHandler; - $this->accountRepository = $accountRepository; - $this->aliasRepository = $aliasRepository; } @@ -71,21 +65,12 @@ class AccountController extends BaseController $accountId = (int)$args['id']; // Get account data from database - $account = $this->accountRepository->fetchAccountById($accountId); - - $renderData = [ - 'id' => $account->getId(), - 'accountUsername' => $account->getUsername(), - 'account' => $account, - ]; + $renderData = $this->accountHandler->getAccountDataForEdit($accountId); + // If the form has been submitted, add the result message and form input data to the render data array $lastActionResult = $this->sessionHelper->getLastActionResult(); if ($lastActionResult !== null) { - $resultData = $lastActionResult->isSuccess() - ? ['success' => $lastActionResult->getMessage()] - : ['error' => $lastActionResult->getMessage()]; - $resultData['editData'] = $lastActionResult->getInputData(); - $renderData = array_merge($renderData, $resultData); + $renderData = array_merge($renderData, $lastActionResult->getRenderData()); } return $this->view->render($response, 'account_edit.html.twig', $renderData); @@ -127,14 +112,7 @@ class AccountController extends BaseController $accountId = (int)$args['id']; // Get account data and list of aliases from database - $account = $this->accountRepository->fetchAccountById($accountId); - $aliases = $this->aliasRepository->fetchAliasesForUserId($accountId); - - $renderData = [ - 'id' => $accountId, - 'accountUsername' => $account->getUsername(), - 'aliases' => $aliases, - ]; + $renderData = $this->accountHandler->getAccountDataForDelete($accountId); return $this->view->render($response, 'account_delete.html.twig', $renderData); } diff --git a/src/Frontend/Accounts/AccountHandler.php b/src/Frontend/Accounts/AccountHandler.php index 26de4fd..c9c72d1 100644 --- a/src/Frontend/Accounts/AccountHandler.php +++ b/src/Frontend/Accounts/AccountHandler.php @@ -61,6 +61,18 @@ class AccountHandler // -- /accounts/{id}/edit - Edit account data + public function getAccountDataForEdit(int $accountId): array + { + // Get account data from database + $account = $this->accountRepository->fetchAccountById($accountId); + + return [ + 'id' => $accountId, + 'accountUsername' => $account->getUsername(), + 'account' => $account, + ]; + } + public function editAccountData(int $accountId, AccountEditData $editData): void { // Check if account exists @@ -144,4 +156,22 @@ class AccountHandler // Commit database transaction $this->accountRepository->commitTransaction(); } + + + // -- /accounts/{id}/delete - Delete account + + public function getAccountDataForDelete(int $accountId): array + { + // Get account data from database + $account = $this->accountRepository->fetchAccountById($accountId); + + // Get list of aliases for this account + $aliases = $this->aliasRepository->fetchAliasesForUserId($accountId); + + return [ + 'id' => $accountId, + 'accountUsername' => $account->getUsername(), + 'aliases' => $aliases, + ]; + } } diff --git a/templates/account_edit.html.twig b/templates/account_edit.html.twig index ff1ec87..f4985ab 100644 --- a/templates/account_edit.html.twig +++ b/templates/account_edit.html.twig @@ -39,12 +39,12 @@ - + @@ -52,7 +52,7 @@ @@ -66,11 +66,11 @@ - + - +
@@ -87,8 +87,8 @@