60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Repositories;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\RepositoryConnection;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class StoreProjectRepositoryConnection
|
|
{
|
|
public function __construct(
|
|
private readonly ResolveRepositorySelectionAttributes $resolveRepositorySelectionAttributes,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{provider_repository_id: string, user_git_connection_id: int} $validated
|
|
*/
|
|
public function handle(Project $project, User $user, array $validated): RepositoryConnection
|
|
{
|
|
$repositoryAttributes = $this->resolveRepositorySelectionAttributes->handle(
|
|
$user,
|
|
(int) Arr::get($validated, 'user_git_connection_id'),
|
|
(string) Arr::get($validated, 'provider_repository_id'),
|
|
);
|
|
|
|
$this->ensureRepositoryConnectionIsUnique($project, $repositoryAttributes);
|
|
|
|
return $project->repositoryConnections()->create($repositoryAttributes);
|
|
}
|
|
|
|
/**
|
|
* @param array{
|
|
* base_url: string,
|
|
* owner_name: string,
|
|
* provider: mixed,
|
|
* provider_repository_id: string,
|
|
* repository_full_name: string,
|
|
* repository_name: string,
|
|
* repository_url: string
|
|
* } $repositoryAttributes
|
|
*/
|
|
private function ensureRepositoryConnectionIsUnique(
|
|
Project $project,
|
|
array $repositoryAttributes,
|
|
): void {
|
|
$query = $project->repositoryConnections()
|
|
->where('provider', $repositoryAttributes['provider'])
|
|
->where('base_url', $repositoryAttributes['base_url'])
|
|
->where('provider_repository_id', $repositoryAttributes['provider_repository_id']);
|
|
|
|
if ($query->exists()) {
|
|
throw ValidationException::withMessages([
|
|
'provider_repository_id' => 'This repository is already linked to the project.',
|
|
]);
|
|
}
|
|
}
|
|
}
|