Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

laravelstart/laravelstart

20 stars
4 code files
View laravelstart/laravelstart on GitHub

app/Services/CreateRepository/WithGithubApi.php

Open in GitHub
use 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 GitHub
use 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 GitHub
use 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 GitHub
use 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);
// ...
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.