pelican-panel-mirror/app/Services/Databases/Hosts/HostCreationService.php
Lance Pioch da195fd2fe
PHPstan updates (#1047)
* Not found property rule

* Make these “better”

* Day 1

* Day 2

* Day 3

* Dat 4

* Remove disabled check

* Day 4 continued

* Run pint

* Final changes hopefully

* Pint fixes

* Fix again

* Reset these

* Update app/Filament/Admin/Pages/Health.php

Co-authored-by: MartinOscar <40749467+rmartinoscar@users.noreply.github.com>

* Update app/Traits/CheckMigrationsTrait.php

Co-authored-by: MartinOscar <40749467+rmartinoscar@users.noreply.github.com>

---------

Co-authored-by: MartinOscar <40749467+rmartinoscar@users.noreply.github.com>
2025-03-03 14:41:19 -05:00

58 lines
1.6 KiB
PHP

<?php
namespace App\Services\Databases\Hosts;
use App\Models\DatabaseHost;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface;
use App\Extensions\DynamicDatabaseConnection;
class HostCreationService
{
/**
* HostCreationService constructor.
*/
public function __construct(
private ConnectionInterface $connection,
private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic,
) {}
/**
* Create a new database host on the Panel.
*
* @param array{
* password: string,
* name: string,
* host: string,
* port: int,
* username: string,
* max_databases: int,
* node_ids?: array<int>
* } $data
*
* @throws \Throwable
*/
public function handle(array $data): DatabaseHost
{
return $this->connection->transaction(function () use ($data) {
$host = DatabaseHost::query()->create([
'password' => array_get($data, 'password'),
'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'),
'port' => array_get($data, 'port'),
'username' => array_get($data, 'username'),
'max_databases' => array_get($data, 'max_databases'),
]);
$host->nodes()->sync(array_get($data, 'node_ids', []));
// Confirm access using the provided credentials before saving data.
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->getPdo();
return $host;
});
}
}