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; 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\Common\UserHelper;
use MailAccountAdmin\Exceptions\InputValidationError; use MailAccountAdmin\Exceptions\InputValidationError;
use MailAccountAdmin\Frontend\BaseController; use MailAccountAdmin\Frontend\BaseController;
use MailAccountAdmin\Repositories\AccountRepository;
use MailAccountAdmin\Repositories\AliasRepository;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig; use Slim\Views\Twig;
@ -17,15 +15,11 @@ use Slim\Views\Twig;
class AccountController extends BaseController class AccountController extends BaseController
{ {
private AccountHandler $accountHandler; 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); parent::__construct($view, $sessionHelper, $userHelper);
$this->accountHandler = $accountHandler; $this->accountHandler = $accountHandler;
$this->accountRepository = $accountRepository;
$this->aliasRepository = $aliasRepository;
} }
@ -71,21 +65,12 @@ class AccountController extends BaseController
$accountId = (int)$args['id']; $accountId = (int)$args['id'];
// Get account data from database // Get account data from database
$account = $this->accountRepository->fetchAccountById($accountId); $renderData = $this->accountHandler->getAccountDataForEdit($accountId);
$renderData = [
'id' => $account->getId(),
'accountUsername' => $account->getUsername(),
'account' => $account,
];
// If the form has been submitted, add the result message and form input data to the render data array
$lastActionResult = $this->sessionHelper->getLastActionResult(); $lastActionResult = $this->sessionHelper->getLastActionResult();
if ($lastActionResult !== null) { if ($lastActionResult !== null) {
$resultData = $lastActionResult->isSuccess() $renderData = array_merge($renderData, $lastActionResult->getRenderData());
? ['success' => $lastActionResult->getMessage()]
: ['error' => $lastActionResult->getMessage()];
$resultData['editData'] = $lastActionResult->getInputData();
$renderData = array_merge($renderData, $resultData);
} }
return $this->view->render($response, 'account_edit.html.twig', $renderData); return $this->view->render($response, 'account_edit.html.twig', $renderData);
@ -127,14 +112,7 @@ class AccountController extends BaseController
$accountId = (int)$args['id']; $accountId = (int)$args['id'];
// Get account data and list of aliases from database // Get account data and list of aliases from database
$account = $this->accountRepository->fetchAccountById($accountId); $renderData = $this->accountHandler->getAccountDataForDelete($accountId);
$aliases = $this->aliasRepository->fetchAliasesForUserId($accountId);
$renderData = [
'id' => $accountId,
'accountUsername' => $account->getUsername(),
'aliases' => $aliases,
];
return $this->view->render($response, 'account_delete.html.twig', $renderData); return $this->view->render($response, 'account_delete.html.twig', $renderData);
} }

View File

@ -61,6 +61,18 @@ class AccountHandler
// -- /accounts/{id}/edit - Edit account data // -- /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 public function editAccountData(int $accountId, AccountEditData $editData): void
{ {
// Check if account exists // Check if account exists
@ -144,4 +156,22 @@ class AccountHandler
// Commit database transaction // Commit database transaction
$this->accountRepository->commitTransaction(); $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>
<tr> <tr>
<td><label for="edit_username">New username:</label></td> <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>
<tr> <tr>
<td colspan="2"> <td colspan="2">
<label> <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 Create alias for old username
</label> </label>
</td> </td>
@ -52,7 +52,7 @@
<tr> <tr>
<td colspan="2"> <td colspan="2">
<label> <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 Replace existing alias
</label> </label>
</td> </td>
@ -66,11 +66,11 @@
<table> <table>
<tr> <tr>
<td><label for="edit_password">New password:</label></td> <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>
<tr> <tr>
<td><label for="edit_password_repeat">Repeat password:</label></td> <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> </tr>
</table> </table>
</div> </div>
@ -87,8 +87,8 @@
<td> <td>
<label> <label>
<input type="checkbox" name="is_active" <input type="checkbox" name="is_active"
{%- if editData | default() -%} {%- if formData | default() -%}
{{ editData['is_active'] ? ' checked' : '' }} {{ formData['is_active'] | default() ? ' checked' : '' }}
{%- else -%} {%- else -%}
{{ account.isActive() ? ' checked' : '' }} {{ account.isActive() ? ' checked' : '' }}
{%- endif -%} {%- endif -%}
@ -111,7 +111,7 @@
<tr> <tr>
<td><label for="edit_home_dir">New home directory:</label></td> <td><label for="edit_home_dir">New home directory:</label></td>
<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> </td>
</tr> </tr>
</table> </table>
@ -123,7 +123,7 @@
<table> <table>
<tr> <tr>
<td><label for="edit_memo">Admin memo:</label></td> <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> </tr>
</table> </table>
</div> </div>