2017-10-18 22:32:19 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Services\Databases\Hosts;
|
2017-10-18 22:32:19 -05:00
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\DatabaseHost;
|
2017-10-18 22:32:19 -05:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Throwable;
|
2017-10-18 22:32:19 -05:00
|
|
|
|
|
|
|
|
class HostUpdateService
|
|
|
|
|
{
|
|
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* HostUpdateService constructor.
|
2017-10-18 22:32:19 -05:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2022-10-14 10:59:20 -06:00
|
|
|
private ConnectionInterface $connection,
|
2024-11-22 09:27:57 +01:00
|
|
|
) {}
|
2017-10-18 22:32:19 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update a database host and persist to the database.
|
|
|
|
|
*
|
2025-03-03 14:41:19 -05:00
|
|
|
* @param array<mixed> $data
|
|
|
|
|
*
|
2025-09-08 13:12:33 -04:00
|
|
|
* @throws Throwable
|
2017-10-18 22:32:19 -05:00
|
|
|
*/
|
2024-06-26 00:42:55 +02:00
|
|
|
public function handle(DatabaseHost|int $host, array $data): DatabaseHost
|
2017-10-18 22:32:19 -05:00
|
|
|
{
|
2024-06-26 00:42:55 +02:00
|
|
|
if (!$host instanceof DatabaseHost) {
|
|
|
|
|
$host = DatabaseHost::query()->findOrFail($host);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 15:24:20 +02:00
|
|
|
if (empty(array_get($data, 'password'))) {
|
2017-10-18 22:32:19 -05:00
|
|
|
unset($data['password']);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 00:42:55 +02:00
|
|
|
return $this->connection->transaction(function () use ($data, $host) {
|
2024-03-16 22:41:36 -04:00
|
|
|
$host->update($data);
|
2024-06-26 00:42:55 +02:00
|
|
|
|
2025-04-23 16:02:08 +02:00
|
|
|
// Confirm access using the provided credentials before saving data.
|
|
|
|
|
$host->buildConnection()->getPdo();
|
2017-10-18 22:32:19 -05:00
|
|
|
|
2019-08-03 12:33:28 -07:00
|
|
|
return $host;
|
|
|
|
|
});
|
2017-10-18 22:32:19 -05:00
|
|
|
}
|
|
|
|
|
}
|