- Added services (`RoleService`, `AppRoleService`, `AppsPermissionService`) to handle role creation, app assignments, and permissions. - Introduced new Livewire forms for creating roles and assigning apps (`CreateRoleForm`, `AssignAppToRoleForm`). - Built dynamic Blade views for role and app management, including modals and reusable components. - Removed outdated `fluxui-development` skill documentation.
102 lines
2.9 KiB
PHP
102 lines
2.9 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\Enums\ConnectedAppPermissonEnum;
|
|
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
|
|
{
|
|
public function __construct(
|
|
private readonly AppsPermissionService $permissionService,
|
|
) {}
|
|
|
|
/**
|
|
* Create a new connected app.
|
|
*
|
|
* @throws Throwable when an error occurs during the creation.
|
|
*/
|
|
public function create(ConnectAppRequest $data): void
|
|
{
|
|
DB::transaction(
|
|
function () use ($data): void {
|
|
$connectedApp = ConnectedApp::query()->create(
|
|
$data->toArray()
|
|
);
|
|
|
|
$this->permissionService->add(ConnectedAppData::from($connectedApp), ConnectedAppPermissonEnum::Use);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
}
|