singleloginsystem/app/Services/ConnectedAppService.php
= 9b6af8135d refactor: remove unused UI components and factories for cleanup
- Deleted obsolete components (`Choices`, `Input`, `ListItem`, `Table`) and trait (`HasAttributeHelpers`) from the `App\View\Components` namespace.
- Removed `ConnectedAppFactory` as it is no longer utilized in the current workflow.
2026-05-16 09:44:54 +00:00

93 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Data\ConnectedApp\{ConnectAppRequest, ConnectedAppData};
use App\Data\Ui\ConnectedApps\ConnectedAppData as UiConnectedAppData;
use App\Models\ConnectedApp;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Spatie\LaravelData\PaginatedDataCollection;
use Throwable;
final class ConnectedAppService
{
/**
* Create a new connected app.
*
* @throws Throwable when an error occurs during the creation.
*/
public function create(ConnectAppRequest $data): void
{
DB::transaction(
fn () => ConnectedApp::query()->create(
$data->toArray()
)
);
}
/**
* @throws InvalidArgumentException when id is invalid
* @throws Throwable when an error occurs during the update.
*/
public function update(int $id, ConnectAppRequest $data): void
{
if ($id <= 0) {
throw new InvalidArgumentException('Invalid id');
}
DB::transaction(
function () use ($id, $data): void {
$app = ConnectedApp::query()->findOrFail($id);
$app->update($data->toArray());
}
);
}
/**
* @throws Throwable when an error occurs during the deletion.
* @throws InvalidArgumentException when id is invalid
*/
public function delete(int $id): void
{
if ($id <= 0) {
throw new InvalidArgumentException('Invalid id');
}
DB::transaction(
function () use ($id): void {
$app = ConnectedApp::query()->findOrFail($id);
$app->delete();
}
);
}
/**
* @return PaginatedDataCollection<int, UiConnectedAppData>
*/
public function getAll(): PaginatedDataCollection
{
$data = ConnectedApp::with('provider:name,id', 'protocol:id,name', 'status:id,name')
->paginate();
return UiConnectedAppData::collect($data, PaginatedDataCollection::class);
}
/**
* @throws ModelNotFoundException<ConnectedApp> when the connected app is not found.
* @throws InvalidArgumentException when id is invalid.
*/
public function getConnectedApp(int $id): ConnectedAppData
{
if ($id <= 0) {
throw new InvalidArgumentException('Invalid id');
}
$data = ConnectedApp::with('provider:id', 'protocol:id', 'status:id')
->findOrFail($id);
return ConnectedAppData::from($data);
}
}