-
app/Services/CreateRepository/WithGithubApi.php
Open in GitHubuse App\Models\User; use App\Support\GitRepository; use Github\AuthMethod; use Github\Client; class WithGithubApi implements CreateRepositoryContract { public function __construct( protected readonly Client $github ) { } public function handle(GitRepository $target): array { if (null === $target->isOrganisation) { throw new \InvalidArgumentException('isOrganisation parameter is required.'); } if (null === $target->public) { throw new \InvalidArgumentException('public parameter is required.'); } return $this->github ->api('repo') ->create( $target->name, 'This is the description of a repo', public: $target->public, organization: $target->isOrganisation ? $target->owner : null, ); } public function authorize(User $user): void { $this->github->authenticate($user->token, AuthMethod::ACCESS_TOKEN); } }
-
app/Services/CreateRepository/CreateRepositoryContract.php
Open in GitHubuse App\Models\User; use App\Support\GitRepository; interface CreateRepositoryContract { public function handle(GitRepository $target): array; public function authorize(User $user): void; }
-
app/Providers/AppServiceProvider.php
Open in GitHubuse App\Services\CreateRepository\CreateRepositoryContract; class AppServiceProvider extends ServiceProvider { public function register(): void { $this->registerStrategies(); } // ... private function registerStrategies(): void { when( $this->app->environment('local'), function () { // ... }, // Bind production strategies function () { // ... $this->app->bind(CreateRepositoryContract::class, \App\Services\CreateRepository\WithGithubApi::class); } ); // ... } }
-
app/Http/Controllers/Projects/StoreController.php
Open in GitHubuse App\Services\CreateRepository\CreateRepositoryContract; class StoreController extends Controller { public function __invoke( StarterKit $kit, StoreRequest $request, CreateRepositoryContract $createRepository, CloneRepositoryContract $clone, PushRepositoryContract $push, ): RedirectResponse { // ... $createRepository->authorize($request->user()); // ... $createdRepository = $createRepository->handle($target); // ... } }