Refactoring
This commit is contained in:
parent
87928dbbc9
commit
23127dd193
4 changed files with 58 additions and 36 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue