-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
270 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.