pelican-panel-mirror/app/Services/Databases/DeployServerDatabaseService.php
Lance Pioch ad1a9cd33f
Update phpstan to latest (#804)
* Fix these

* Update phpstan

* Transform these into their identifiers instead

* Fix custom rule

* License is wrong

* Update these

* Pint fixes

* Fix this

* Consolidate these

* Never supported PHP 7

* Better evaluation

* Fixes

* Don’t need ignore

* Replace trait with service

* Subusers are simply the many to many relationship between Servers and Users

* Adjust to remove ignores

* Use new query builder instead!

* wip

* Update composer

* Quick fixes

* Use realtime facade

* Small fixes

* Convert to static to avoid new

* Update to statics

* Don’t modify protected properties directly

* Run pint

* Change to correct method

* Give up and use the facade

* Make sure this route is available

* Filament hasn’t been loaded yet

* This can be readonly

* Typehint

* These are no longer used

* Quick fixes

* Need doc block help

* Always true

* We use caddy with docker

* Pint

* Fix phpstan issues

* Remove unused import

---------

Co-authored-by: MartinOscar <40749467+RMartinOscar@users.noreply.github.com>
2025-01-16 14:53:50 -05:00

40 lines
1.3 KiB
PHP

<?php
namespace App\Services\Databases;
use Webmozart\Assert\Assert;
use App\Models\Server;
use App\Models\Database;
use App\Models\DatabaseHost;
use App\Exceptions\Service\Database\NoSuitableDatabaseHostException;
readonly class DeployServerDatabaseService
{
public function __construct(private DatabaseManagementService $managementService) {}
public function handle(Server $server, array $data): Database
{
Assert::notEmpty($data['database'] ?? null);
Assert::notEmpty($data['remote'] ?? null);
$hosts = DatabaseHost::query()->get();
if ($hosts->isEmpty()) {
throw new NoSuitableDatabaseHostException();
}
$nodeHosts = $server->node->databaseHosts()->get();
// TODO: @areyouscared remove allow random feature for database hosts
if ($nodeHosts->isEmpty() && !config('panel.client_features.databases.allow_random')) {
throw new NoSuitableDatabaseHostException();
}
return $this->managementService->create($server, [
'database_host_id' => $nodeHosts->isEmpty()
? $hosts->random()->id
: $nodeHosts->random()->id,
'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id),
'remote' => $data['remote'],
]);
}
}