Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
docjyJ committed Nov 24, 2024
1 parent 4967072 commit 6402c0f
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 114 deletions.
51 changes: 15 additions & 36 deletions lib/Controller/ApiServersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace OCA\Nextmail\Controller;

use OCA\Nextmail\Db\Transaction;
use OCA\Nextmail\Db\ServerManager;
use OCA\Nextmail\Models\ServerEntity;
use OCA\Nextmail\ResponseDefinitions;
use OCA\Nextmail\Services\StalwartApiService;
use OCA\Nextmail\Settings\Admin;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
Expand All @@ -29,16 +28,15 @@ class ApiServersController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private readonly Transaction $tr,
private readonly StalwartApiService $apiService,
private readonly ServerManager $sm,
private readonly LoggerInterface $logger,
) {
parent::__construct($appName, $request);
}

/**
* List all available servers
* @return DataResponse<Http::STATUS_OK, NextmailServer[], array{}>
* @return DataResponse<Http::STATUS_OK, list<NextmailServer>, array{}>
* @throws OCSException If an error occurs
*
* 200: Returns the list of available servers
Expand All @@ -48,8 +46,7 @@ public function __construct(
#[ApiRoute(verb: 'GET', url: '/servers')]
public function list(): DataResponse {
try {
$servers = array_map(fn (mixed $row) => ServerEntity::parse($row)->jsonSerialize(), $this->tr->selectServer());
return new DataResponse($servers);
return new DataResponse(array_map(fn (ServerEntity $x) => $x->jsonSerialize(), $this->sm->listAll()));
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
Expand All @@ -68,16 +65,9 @@ public function list(): DataResponse {
#[ApiRoute(verb: 'POST', url: '/servers')]
public function create(): DataResponse {
try {
$server = ServerEntity::newEmpty();
$this->tr->insertServer(
$server->id,
$server->endpoint,
$server->username,
$server->password,
$server->health
);
$this->tr->commit();
return new DataResponse($server->jsonSerialize());
$data = $this->sm->create();
$this->sm->commit();
return new DataResponse($data->jsonSerialize());
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
Expand All @@ -101,19 +91,10 @@ public function create(): DataResponse {
#[ApiRoute(verb: 'PUT', url: '/servers/{srv}')]
public function setServer(string $srv, string $endpoint, string $username, string $password): DataResponse {
try {
$servers = $this->tr->selectServer($srv);
if (count($servers) === 1) {
$server = ServerEntity::parse($servers[0]);
$server = $this->apiService->challenge($server->updateCredential($endpoint, $username, $password));
$this->tr->updateServer(
$server->id,
$server->endpoint,
$server->username,
$server->password,
$server->health
);
$this->tr->commit();
return new DataResponse($server->jsonSerialize());
$data = $this->sm->update($srv, $endpoint, $username, $password);
$this->sm->commit();
if ($data !== null) {
return new DataResponse($data->jsonSerialize());
} else {
throw new OCSNotFoundException();
}
Expand All @@ -137,12 +118,10 @@ public function setServer(string $srv, string $endpoint, string $username, strin
#[ApiRoute(verb: 'DELETE', url: '/servers/{srv}')]
public function delServer(string $srv): DataResponse {
try {
$servers = $this->tr->selectServer($srv);
if (count($servers) === 1) {
$server = ServerEntity::parse($servers[0]);
$this->tr->deleteServer($server->id);
$this->tr->commit();
return new DataResponse($server->jsonSerialize());
$data = $this->sm->delete($srv);
$this->sm->commit();
if ($data !== null) {
return new DataResponse($data->jsonSerialize());
} else {
throw new OCSNotFoundException();
}
Expand Down
13 changes: 5 additions & 8 deletions lib/Controller/ApiUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OCA\Nextmail\Controller;

use OCA\Nextmail\Db\Transaction;
use OCA\Nextmail\Db\UsersManager;
use OCA\Nextmail\Models\UserEntity;
use OCA\Nextmail\ResponseDefinitions;
Expand All @@ -29,7 +28,6 @@ class ApiUsersController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private readonly Transaction $tr,
private readonly UsersManager $um,
private readonly LoggerInterface $logger,
) {
Expand All @@ -38,7 +36,7 @@ public function __construct(

/**
* List all available users
* @return DataResponse<Http::STATUS_OK, NextmailUser[], array{}>
* @return DataResponse<Http::STATUS_OK, list<NextmailUser>, array{}>
* @throws OCSException if an error occurs
*
* 200: Returns the list of users
Expand All @@ -48,8 +46,7 @@ public function __construct(
#[ApiRoute(verb: 'GET', url: '/users')]
public function getUsers(): DataResponse {
try {
$users = array_map(fn (mixed $row) => UserEntity::parse($row)->jsonSerialize(), $this->tr->selectAccount());
return new DataResponse($users);
return new DataResponse(array_map(fn (UserEntity $x) => $x->jsonSerialize(), $this->um->listAll()));
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR);
Expand All @@ -73,10 +70,10 @@ public function getUsers(): DataResponse {
#[ApiRoute(verb: 'PUT', url: '/users/{usr}')]
public function updateUser(string $usr, ?string $server_id, bool $admin, ?int $quota): DataResponse {
try {
$user = $this->um->updateFromStarch($usr, $server_id, $admin, $quota);
$data = $this->um->update($usr, $server_id, $admin, $quota);
$this->um->commit();
if ($user !== null) {
return new DataResponse($user->jsonSerialize());
if ($data !== null) {
return new DataResponse($data->jsonSerialize());
} else {
throw new OCSNotFoundException();
}
Expand Down
7 changes: 5 additions & 2 deletions lib/Db/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public function notWhere(string $name, array $values): self {
return $this;
}

/** @throws Exception */
/**
* @throws Exception
* @return list<mixed>
*/
public function fetchAll(): array {
if (count($this->cond) > 0) {
$this->q->where(...$this->cond);
}
return $this->q->executeQuery()->fetchAll();
return array_values($this->q->executeQuery()->fetchAll());
}

}
95 changes: 95 additions & 0 deletions lib/Db/ServerManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace OCA\Nextmail\Db;

use OCA\Nextmail\Models\ServerEntity;
use OCA\Nextmail\Services\StalwartApiService;
use OCP\DB\Exception;

readonly class ServerManager {

public function __construct(
private Transaction $tr,
private StalwartApiService $api,
) {
}

/** @throws Exception */
private function loadEntity(string $srv): ?ServerEntity {
$data = $this->tr->selectServer($srv);
return isset($data[0]) ? ServerEntity::parse($data[0]) : null;
}

/**
* @return list<ServerEntity>
* @throws Exception
*/
public function listAll(): array {
return array_map(fn ($x) => ServerEntity::parse($x), $this->tr->selectServer());
}


/** @throws Exception */
private function processQuery(ServerEntity $data, bool $create): ServerEntity {
$data = $this->api->challenge($data);
if ($create) {
$this->tr->insertServer(
$data->id,
$data->endpoint,
$data->username,
$data->password,
$data->health
);
} else {
$this->tr->updateServer(
$data->id,
$data->endpoint,
$data->username,
$data->password,
$data->health
);
}
return $data;
}

/** @throws Exception */
public function syncAll(): void {
foreach ($this->listAll() as $data) {
$this->syncOne($data);
}
}

/** @throws Exception */
public function syncOne(ServerEntity $data): void {
$this->processQuery($data, false);
}

/** @throws Exception */
public function update(string $srv, string $endpoint, string $username, string $password): ?ServerEntity {
$data = $this->loadEntity($srv);
return $data !== null
? $this->processQuery($data->update($endpoint, $username, $password), false)
: null;

}

/** @throws Exception */
public function create(): ServerEntity {
return $this->processQuery(ServerEntity::newEmpty(), true);
}

/** @throws Exception */
public function delete(string $srv): ?ServerEntity {
$data = $this->loadEntity($srv);
$this->tr->deleteServer($srv);
$this->tr->commit();
return $data;
}

/** @throws Exception */
public function commit(): void {
$this->tr->commit();
}


}
9 changes: 7 additions & 2 deletions lib/Db/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function commit(): void {
}
}

/** @throws Exception */
/**
* @throws Exception
* @return list<mixed>
*/
public function selectServer(?string $id = null): array {
$q = new SelectQuery($this->db, SchServer::TABLE);
$q->where(SchServer::ID, $id);
Expand Down Expand Up @@ -96,8 +99,10 @@ public function deleteServer(string $id): void {
}

/**
* @param string|null $id
* @param list<AccountRole> $role
* @return list<mixed>
* @throws Exception
* @param AccountRole[] $role
*/
public function selectAccount(?string $id = null, array $role = []): array {
return (new SelectQuery($this->db, SchAccount::TABLE))
Expand Down
Loading

0 comments on commit 6402c0f

Please sign in to comment.