Refactoring

This commit is contained in:
Lexi / Zoe 2021-09-12 22:16:06 +02:00
parent 87928dbbc9
commit 23127dd193
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
4 changed files with 58 additions and 36 deletions

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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,
];
}
}

View File

@ -39,12 +39,12 @@
</tr>
<tr>
<td><label for="edit_username">New username:</label></td>
<td><input id="edit_username" name="username" value="{{ editData['username'] | default('') }}"/></td>
<td><input id="edit_username" name="username" value="{{ formData['username'] | default('') }}"/></td>
</tr>
<tr>
<td colspan="2">
<label>
<input type="checkbox" name="username_create_alias" {{ editData['username_create_alias'] | default('') ? 'checked' : '' }}/>
<input type="checkbox" name="username_create_alias" {{ formData['username_create_alias'] | default('') ? 'checked' : '' }}/>
Create alias for old username
</label>
</td>
@ -52,7 +52,7 @@
<tr>
<td colspan="2">
<label>
<input type="checkbox" name="username_replace_alias" {{ editData['username_replace_alias'] | default('') ? 'checked' : '' }}/>
<input type="checkbox" name="username_replace_alias" {{ formData['username_replace_alias'] | default('') ? 'checked' : '' }}/>
Replace existing alias
</label>
</td>
@ -66,11 +66,11 @@
<table>
<tr>
<td><label for="edit_password">New password:</label></td>
<td><input type="password" id="edit_password" name="password" value="{{ editData['password'] | default('') }}"/></td>
<td><input type="password" id="edit_password" name="password" value="{{ formData['password'] | default('') }}"/></td>
</tr>
<tr>
<td><label for="edit_password_repeat">Repeat password:</label></td>
<td><input type="password" id="edit_password_repeat" name="password_repeat" value="{{ editData['password_repeat'] | default('') }}"/></td>
<td><input type="password" id="edit_password_repeat" name="password_repeat" value="{{ formData['password_repeat'] | default('') }}"/></td>
</tr>
</table>
</div>
@ -87,8 +87,8 @@
<td>
<label>
<input type="checkbox" name="is_active"
{%- if editData | default() -%}
{{ editData['is_active'] ? ' checked' : '' }}
{%- if formData | default() -%}
{{ formData['is_active'] | default() ? ' checked' : '' }}
{%- else -%}
{{ account.isActive() ? ' checked' : '' }}
{%- endif -%}
@ -111,7 +111,7 @@
<tr>
<td><label for="edit_home_dir">New home directory:</label></td>
<td>
<span class="gray">/srv/vmail/</span><input id="edit_home_dir" name="home_dir" value="{{ editData['home_dir'] | default('') }}"/>
<span class="gray">/srv/vmail/</span><input id="edit_home_dir" name="home_dir" value="{{ formData['home_dir'] | default('') }}"/>
</td>
</tr>
</table>
@ -123,7 +123,7 @@
<table>
<tr>
<td><label for="edit_memo">Admin memo:</label></td>
<td><textarea id="edit_memo" name="memo" style="min-width: 40em;">{{ editData | default() ? editData['memo'] : account.getMemo() }}</textarea></td>
<td><textarea id="edit_memo" name="memo" style="min-width: 40em;">{{ formData | default() ? formData['memo'] : account.getMemo() }}</textarea></td>
</tr>
</table>
</div>